https://codeforces.com/problemset/problem/1131/F
アイデア:
それぞれの番号について、その偶数側を彼の息子と見なし、最後に息子のステータスを自分自身に更新します。
彼の息子を木の構造と考えてください。これが上司から借りた写真です
1 3 1 2 4 5 4 6 1 4
G [1] = {3,2,4}の場合、各サブツリーは順序が実行可能であることを保証するため、結合された順序も実行可能です。
ご覧のとおり、この構造は合法でなければなりません。問題は、n ^ 2ではなく各ポイントをどのように更新するかです。ヒューリスティックなマージでは、長男は削除されず、次男のコレクションステータスが長男のコレクションに追加されて去り、その後、次の息子が削除されます。O(nlogn)
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+100;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL fa[maxn];
vector<LL>g[maxn];
LL find(LL x){
if(x!=fa[x]) return fa[x]=find(fa[x]);
return fa[x];
}
LL dsu(LL x,LL y){
LL px=find(x);LL py=find(y);///找到根节点
if(g[px].size()>g[py].size()){
swap(px,py);///令包含元素少的为x节点
}
fa[px]=py;
g[py].insert(g[py].end(),g[px].begin(),g[px].end());///插入大元素的儿子集合的右边
g[px].clear();
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n;cin>>n;
for(LL i=1;i<=n;i++) fa[i]=i,g[i].push_back(i);
for(LL i=1;i<n;i++){
LL u,v;cin>>u>>v;
dsu(u,v);
}
LL id=find(1);
for(auto i:g[id]){
cout<<i<<" ";
}
cout<<"\n";
return 0;
}