Luogu2783 有机化学之神偶尔会做作弊 (树链剖分,缩点)

当联通块size<=2时不管

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 10007;
const int M = 50007;

int n, m;
struct Edge{
    int nxt, pre, from;
}e[M << 1], e2[M << 1];
int head[N], head2[N], cntEdge, cntEdge2;
inline void add(int u, int v){
    e[++cntEdge] = (Edge){head[u], v, u}, head[u] = cntEdge;
}
inline void add2(int u, int v){
    e2[++cntEdge2] = (Edge){head2[u], v}, head2[u] = cntEdge2; // !
}

namespace Tarjan{
int dfn[N], dfnIndex, low[N], vis[N];
int sta[N], top;
int scc[N], sccIndex;
inline void Tarjan(int u, int fa){
    dfn[u] = low[u] = ++dfnIndex;
    sta[++top] = u;
    vis[u] = true;
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(v == fa) continue; // size <= 2
        if(!dfn[v]){
            Tarjan(v, u);
            low[u] = Min(low[u], low[v]);
        }
        else if(vis[v]){
            low[u] = Min(low[u], dfn[v]);
        }
    }   
    if(dfn[u] == low[u]){
        ++sccIndex;
        do{
            vis[sta[top]] = false;
            scc[sta[top]] = sccIndex;
        }while(sta[top--] != u); // !
    }
}
inline void Rebuild(){
    R(i,1,cntEdge){
        if(scc[e[i].pre] != scc[e[i].from]){
            add2(scc[e[i].from], scc[e[i].pre]); //!
        }
        
    }
}
}

namespace Tree{ // ! e, e2
int dep[N], fa[N], son[N], siz[N];
inline void DFS_First(int u, int father){
    dep[u] = dep[father] + 1, fa[u] = father, siz[u] = 1;
    for(register int i = head2[u]; i; i = e2[i].nxt){
        int v = e2[i].pre;
        if(v == father) continue;
        DFS_First(v, u);
        siz[u] += siz[v];
        if(!son[u] || siz[v] > siz[son[u]]){
            son[u] = v;
        }
    }
}
int dfn[N], dfnIndex, top[N];
inline void DFS_Second(int u, int ancester){
    dfn[u] = ++dfnIndex, top[u] = ancester;
    if(!son[u]) return;
    DFS_Second(son[u], ancester);
    for(register int i = head2[u]; i; i = e2[i].nxt){
        int v = e2[i].pre;
        if(v != son[u] && v != fa[u]){
            DFS_Second(v, v);
        }
    }
}
inline int Query(int x, int y){
    int sum = 0;
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]) Swap(x, y);
        sum += dep[x] - dep[top[x]] + 1;
        x = fa[top[x]];
    }
    if(dep[x] < dep[y]) Swap(x, y);
    return sum + dep[x] - dep[y] + 1;
}
}

inline string Calc(int x){
    string ans = "";
    while(x){
        ans += (x & 1) ? "1" : "0";
        x >>= 1;
    }
    return ans;
}
int main(){
    io >> n >> m;
    R(i,1,m){
        int u, v;
        io >> u >> v;
        add(u, v);
        add(v, u);
    }
    
    R(i,1,n){
        if(!Tarjan::dfn[i]){
            Tarjan::Tarjan(i ,0);
        }
    }
    
    Tarjan::Rebuild();
    
    Tree::DFS_First(1, 0);
    Tree::DFS_Second(1, 1);
    
    int Ques;
    io >> Ques;
    while(Ques--){
        int x, y;
        io >> x >> y;
        string ans = Calc(Tree::Query(Tarjan::scc[x], Tarjan::scc[y]));
        int len = ans.size();
        nR(i,len - 1, 0){
            cout << ans[i];
        }
        putchar('\n');
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/bingoyes/p/11275211.html