標籤:
http://poj.org/problem?id=1962 (題目連結)
時隔多年又一次寫帶權並查集。
題意:n個節點,若干次詢問,I x y表示從x連一條邊到y,權值為|x-y|%1000;E x表示詢問x到x所指向的終點的距離。
Solution
很裸的帶權並查集。
代碼:
// poj1962#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#include<string>#define LL long long#define MOD 1000#define inf 2147483640#define Pi acos(-1.0)#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);using namespace std;int getint() { int f=1,x=0;char ch=getchar(); while (ch<=‘0‘ || ch>‘9‘) {if (ch==‘-‘) f=-1;ch=getchar();} while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘;ch=getchar();} return x*f;}const int maxn=20010;int r[maxn],fa[maxn],n;int find(int x) { if (x==fa[x]) return x; int f=find(fa[x]); r[x]=r[x]+r[fa[x]]; fa[x]=f; return f;}int main() { int T;scanf("%d",&T); while (T--) { scanf("%d",&n); for (int i=1;i<=n;i++) fa[i]=i,r[i]=0; char ch[10]; while (scanf("%s",ch)!=EOF) { int x,y; if (ch[0]==‘O‘) break; else if (ch[0]==‘E‘) { scanf("%d",&x); find(x); printf("%d\n",r[x]); } else { scanf("%d%d",&x,&y); fa[x]=y; r[x]=abs(x-y)%MOD; } } } return 0;}
【poj1962】 Corporative Network