正文
增加 10 行删除 10 行
31 行未改动323334353637383940156 行未改动全部展开19719819920020120220320420518 行未改动2242252262272282292302312322332342352362372382392402412422438 行未改动25225325425525625725825926026126226326426526626742 行未改动31031131231331431531631731826 行未改动34534634734834935035135235335435535635735835936036136236315 行未改动31 行未改动if(d[v]==-1){d[v]=d[u]+1;q.push(v);}}}int V=0;for(int i=1;i<=n;i++) if(d[V]<d[i]) V=i;return V;156 行未改动全部展开### [S9 例题-1 [BOI2003]团伙 (种类并查集)](https://candyoj.com/web/#/SubjectDesc?zid=225&sid=4949&cid=49)多开几个并查集。```cpp#include<bits/stdc++.h>#define int long long#define x first#define y secondusing namespace std;const int N=2e5+10,inf=0x3f3f3f3f,mod=1e9+7;18 行未改动cin>>op>>x>>y;if(op=='F') merge(x,y);else merge(x,y+n),merge(x+n,y);}for(int i=1;i<=n;i++)if(!vis[find(i)]) vis[find(i)]=1,ans++;cout<<ans;return 0;}```### [S9 例题-2 [NOI2002] 银河英雄传说](https://candyoj.com/web/#/SubjectDesc?zid=225&sid=5411&cid=49)带权并查集。```cpp#include<bits/stdc++.h>#define int long long#define x first#define y secondusing namespace std;const int N=2e5+10,inf=0x3f3f3f3f,mod=1e9+7;8 行未改动int find(int x){if(x!=f[x]){int fx=f[x];f[x]=find(f[x]);d[x]+=d[fx];}return f[x];}void merge(int u,int v){u=find(u),v=find(v);if(u==v) return;f[u]=v;d[u]=sz[v];sz[v]+=sz[u];}42 行未改动}void undo(){pii top=stk.top(); stk.pop();int u=top.x,v=top.y;f[u]=u;sz[v]-=sz[u];}signed main(){26 行未改动*/int n,m,cnt,ans,f[N];struct Node{int u,v,w;bool operator < (const Node&x)const{ return x.w>w;}}nt[N];int find(int x) {return x==f[x]?x:f[x]=find(f[x]);}void kruskal(){sort(nt+1,nt+m+1);for(int i=1;i<=m;i++){if(cnt==n-1) return;int u=find(nt[i].u),v=find(nt[i].v),w=nt[i].w;if(u==v) continue;ans +=w;f[u]=v;cnt++;15 行未改动31 行未改动323334353637383940156 行未改动全部展开19719819920020120220320420518 行未改动2242252262272282292302312322332342352362372382392402412422438 行未改动25225325425525625725825926026126226326426526626742 行未改动31031131231331431531631731826 行未改动34534634734834935035135235335435535635735835936036136236315 行未改动31 行未改动if(d[v]==-1){d[v]=d[u]+1;q.push(v);}}}int V=0;for(int i=1;i<=n;i++) if(d[V]<d[i]) V=i;return V;156 行未改动全部展开### [S9 例题-1 [BOI2003]团伙 (种类并查集)](https://candyoj.com/web/#/SubjectDesc?zid=225&sid=4949&cid=49)多开几个并查集。```cpp#include<bits/stdc++.h>#define int long long#define x first#define y secondusing namespace std;const int N=2e5+10,inf=0x3f3f3f3f,mod=1e9+7;18 行未改动cin>>op>>x>>y;if(op=='F') merge(x,y);else merge(x,y+n),merge(x+n,y);}for(int i=1;i<=n;i++)if(!vis[find(i)]) vis[find(i)]=1,ans++;cout<<ans;return 0;}```### [S9 例题-2 [NOI2002] 银河英雄传说](https://candyoj.com/web/#/SubjectDesc?zid=225&sid=5411&cid=49)带权并查集。```cpp#include<bits/stdc++.h>#define int long long#define x first#define y secondusing namespace std;const int N=2e5+10,inf=0x3f3f3f3f,mod=1e9+7;8 行未改动int find(int x){if(x!=f[x]){int fx=f[x];f[x]=find(f[x]);d[x]+=d[fx];}return f[x];}void merge(int u,int v){u=find(u),v=find(v);if(u==v) return;f[u]=v;d[u]=sz[v];sz[v]+=sz[u];}42 行未改动}void undo(){pii top=stk.top(); stk.pop();int u=top.x,v=top.y;f[u]=u;sz[v]-=sz[u];}signed main(){26 行未改动*/int n,m,cnt,ans,f[N];struct Node{int u,v,w;bool operator < (const Node&x)const{ return x.w>w;}}nt[N];int find(int x) {return x==f[x]?x:f[x]=find(f[x]);}void kruskal(){sort(nt+1,nt+m+1);for(int i=1;i<=m;i++){if(cnt==n-1) return;int u=find(nt[i].u),v=find(nt[i].v),w=nt[i].w;if(u==v) continue;ans +=w;f[u]=v;cnt++;15 行未改动