HDU 5409 CRB and Graph【dfs序+RMQ】

时间:2021-08-25 11:38:17

先用trajan缩环变成了一棵树
然后删除了一条边就将树分成了两个部分,一个是删除的那边下面的子树,一个是剩余部分。那么要查询的是两个部分中最大的点的值,和不大于它的最小的点的值。

这样一想就有点像树链剖分啊,树形DP一样求出一颗子树的某个最大值。 又想到是分离出一颗子树,那么就是想到一些dfs序可以对子树进行区间求值。

那么就想到可以预处理出dfs序列,将树的值转化为一个区间值。去掉一颗子树,就是将区间分成三段(可能是两段)。

|____(A)____|____子树(B)____|___(C)____|

求出A,C段最大值中小的那个,和B中最大值。求出最大值。就是答案中的 ansfirst
那么 anssecond 就是 ansfirst+1 ,因为假设 ansfirst+1 ansfirst 在同一个区间内,那么一定取不到 ansfirst ,所以 ansfirst+1 一定是在被分离的区间内,就是答案。

至于区间最大值,因为不会修改就用RMQ求~树上每个节点的最大值就是连通块里的最大节点值。

// whn6325689
// Mr.Phoebe
// http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")


using namespace std;

#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
#define speed std::ios::sync_with_stdio(false);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define CPY(x,y) memcpy(x,y,sizeof(x))
#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))
#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }

#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))

#define MID(x,y) (x+((y-x)>>1))
#define ls (idx<<1)
#define rs (idx<<1|1)
#define lson ls,l,mid
#define rson rs,mid+1,r

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------



const int MAXN = 2e5 + 5;

int head_Tu[MAXN], si_Tu;
int head_G[MAXN], si_G;
int par[MAXN], rmax[MAXN];
int pre[MAXN], dfs_clock;
int ans_u[MAXN], ans_v[MAXN];
int in[MAXN], out[MAXN];
int A[MAXN], dmax[MAXN][30];
int n, m;

struct edge
{
    int to, next;
}Tu[MAXN<<1], G[MAXN<<1];

struct Node
{
    int u, v;
} E[MAXN];

void add_Tu(int st, int ed)
{
    Tu[si_Tu].to = ed;
    Tu[si_Tu].next = head_Tu[st];
    head_Tu[st] = si_Tu++;
}
void add_G(int st, int ed)
{
    G[si_G].to = ed;
    G[si_G].next = head_G[st];
    head_G[st] = si_G++;
}

void init(int _n)
{
    for (int i = 0; i <= _n; i++)
        par[i] = rmax[i] = i;
}
int Find(int x)
{
    if (x == par[x]) return x;
    return par[x] = Find(par[x]);
}
void unite(int x, int y)
{
    x = Find(x);
    y = Find(y);
    if (x == y) return ;
    par[x] = y;
    rmax[y] = max(rmax[x], rmax[y]);
}

int Tarjan(int u, int fa)
{
    int lowu = pre[u] = ++dfs_clock;
    for (int i = head_Tu[u]; i != -1; i = Tu[i].next)
    {
        int v = Tu[i].to;
        if (v == fa) continue;
        if (pre[v] == 0)
        {
            int lowv = Tarjan(v, u);
            lowu = min(lowu, lowv);
            if (lowv <= pre[u]) unite(u, v);
        }
        else lowu = min(lowu, pre[v]);
    }
    return lowu;
}

void find_bridge(int _n)
{
    CLR(pre, 0); dfs_clock = 0;
    for (int i = 1; i <= n; i++)
        if (!pre[i]) Tarjan(i, -1);
}

void dfs(int u, int fa)
{
    in[u] = dfs_clock++;
    A[in[u]] = rmax[u];
    for (int i = head_G[u]; i != -1; i = G[i].next)
    {
        int v = G[i].to;
        if (v == fa) continue;
        dfs(v, u);
    }
    out[u] = dfs_clock-1;
}

void RMQ_init(int _n)
{
    int N = _n;
    for (int i = 0; i < N; i++) dmax[i][0] = A[i];
    for (int j = 1; (1<<j) <= N; j++)
        for (int i = 0; i + (1<<j) - 1 < N; i++)
            dmax[i][j] = max(dmax[i][j-1], dmax[i + (1<<(j-1))][j-1]);
}
int RMQ(int L, int R)
{
    int k = 0;
    while((1<<(k+1)) <= R-L+1) k++;
    return max(dmax[L][k], dmax[R-(1<<k)+1][k]);
}

int main()
{
    //freopen("input.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        CLR(head_Tu, -1); si_Tu = 0;
        CLR(head_G, -1); si_G = 0;
        CLR(ans_u, -1); CLR(ans_v, -1);
        CLR(in, -1); CLR(out, -1);

        init(n);
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &E[i].u, &E[i].v);
            add_Tu(E[i].u, E[i].v);
            add_Tu(E[i].v, E[i].u);
        }
        find_bridge(n);
        for (int i = 0; i < m; i++)
        {
            int u = Find(E[i].u);
            int v = Find(E[i].v);
            if (u == v)
            {
                ans_u[i] = ans_v[i] = 0;
            }
            else
                add_G(u, v),add_G(v, u);
        }
        int root = Find(1);
        dfs_clock = 0;
        dfs(root, -1);
        int len = dfs_clock;
        RMQ_init(len);
        //debug(si_G);
        for (int i = 0; i < m; i++)
        {
            int u = Find(E[i].u);
            int v = Find(E[i].v);
            if (in[u] < in[v]) swap(u, v);
            if (u != v)
            {
                //if(i==8){ cout<<" "; debug(in[u]);debug(out[u]);}
                int x = RMQ(in[u], out[u]);
                int y = 0;
                if (in[u] > 0) y = max(y, RMQ(0, in[u]-1));
                if (out[u]+1 < len) y = max(y, RMQ(out[u]+1, len-1));
                //if(i==8){cout<<x<<" "<<y<<endl;}
                ans_u[i] = min(x, y);
                ans_v[i] = ans_u[i] + 1;
            }
        }
        for (int i = 0; i < m; i++)
            printf("%d %d\n", ans_u[i], ans_v[i]);
    }
    return 0;
}