https://www.luogu.org/problemnew/show/P1653

题解:逆序并查集+DFS

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=400000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans[N],cnt,flag,temp,sum;
int a[N][2];
bool b[N][2];
int c[N][2];
int vis[N];
vector<int>G[N];
char str;
struct node{};
int pre[N];
int find(int x){return (pre[x]==x)?x:pre[x]=find(pre[x]);}
void add(int x,int y){

}
void dfs(int u,int time){
    vis[u]=1;
    ans[u]=time;
    for(int i=0,j=G[u].size();i<j;i++){
        int v=G[u][i];
        if(vis[v]==0){
            vis[v]=1;
            dfs(v,time);
            vis[v]=0;
        }
    }
    vis[u]=0;
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        pre[i]=i;
    }
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i][0],&a[i][1]);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&c[i][0],&c[i][1]);
        b[c[i][0]][c[i][1]-1]=1;
    }
    memset(ans,-1,sizeof(ans));
    for(int i=1;i<=n;i++){
        for(int j=0;j<=1;j++){
            if(!b[i][j]&&a[i][j]!=-1){
                int tx=find(i);
                int ty=find(a[i][j]);
                if(tx!=ty){
                    if(tx==1){
                        pre[ty]=tx;
                        G[tx].push_back(ty);
                    }else if(ty==1){
                        pre[tx]=ty;
                        G[ty].push_back(tx);
                    }else{
                        pre[tx]=ty;
                        G[tx].push_back(ty);
                        G[ty].push_back(tx);
                    }
                }
            }
        }
    }vis[1]=1;
    for(int i=m;i>=1;i--){
        int u=c[i][0];
        int v=a[c[i][0]][c[i][1]-1];
        int tx=find(u);
        int ty=find(v);
        if(tx!=ty){
            if(tx==1){
                pre[ty]=tx;
                G[tx].push_back(ty);
                dfs(ty,i-1);
            }else if(ty==1){
                pre[tx]=ty;
                G[ty].push_back(tx);
                dfs(tx,i-1);
            }else{
                G[tx].push_back(ty);
                G[ty].push_back(tx);
                pre[tx]=ty;
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d\n",ans[i]);
    }
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}