标签:
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 3945 | Accepted: 1416 |
Description
Input
Output
Sample Input
1 4 E 3 I 3 1 E 3 I 1 2 E 3 I 2 4 E 3 O
Sample Output
0 2 3 5
Source
// // main.cpp // poj1962 // // Created by Candy on 10/11/16. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int N=1e5+10; typedef long long ll; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int T,n,u,v; char s[5]; int fa[N],d[N]; inline int find(int x){ if(x!=fa[x]){ int root=find(fa[x]); d[x]+=d[fa[x]]; return fa[x]=root; }else return x; } int main(int argc, const char * argv[]) { T=read(); while(T--){ n=read(); for(int i=1;i<=n;i++){fa[i]=i;d[i]=0;} while(true){ scanf("%s",s); if(s[0]==‘O‘) break; if(s[0]==‘I‘){ u=read();v=read(); fa[u]=v; d[u]=abs(u-v)%1000; }else{ u=read(); find(u); printf("%d\n",d[u]); } } } return 0; }
POJ1962Corporative Network[带权并查集]
标签:
原文地址:http://www.cnblogs.com/candy99/p/5951265.html