UVALive - 3027 Corporative Network (并查集)
2017-07-29 14:24
393 查看
这题比较简单,注意路径压缩即可。
AC代码
如有不当之处欢迎指出!
AC代码
//#define LOCAL #include <stdio.h> #include <algorithm> using namespace std; const int maxn = 20000+5; int par[maxn], dis[maxn]; void init(int n) { for(int i = 0; i <= n; i++) { par[i] = i; dis[i] = 0; } } int findRoot(int x) { if(x == par[x]) { return x; } else { int root = findRoot(par[x]); dis[x] += dis[par[x]]; return par[x] = root; } } int main() { #ifdef LOCAL freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif // LOCAL int T, n; scanf("%d", &T); while(T--) { scanf("%d", &n); init(n); char cmd[10]; int x, y; while(scanf("%s", cmd) == 1 && cmd[0] != 'O') { char tag = cmd[0]; if(tag == 'E') { scanf("%d", &x); findRoot(x); printf("%d\n", dis[x]); } else { scanf("%d%d", &x, &y); par[x] = y; dis[x] = abs(x-y) % 1000; } } } return 0; }
如有不当之处欢迎指出!
相关文章推荐
- UVALive3027 并查集
- UVALive 3027---Corporative Network+并查集的应用
- uvalive 3027(并查集)
- uvalive 3027 Corporative Network 并查集+路径压缩
- uvalive 3027 Corporative Network(种类并查集)
- 【并查集】UVALive3027 Corporative Network
- UVALive 3027 Corporative Network 并查集水题
- Corporative Network UVALive - 3027 并查集
- 指南 第三章 例题6 UVALive 3027 Corporative Network(并查集的应用)
- uvalive 3027 Corporative Network(并查集)
- UVALive 3027 并查集
- 带权值并查集(Uvalive 3027)
- UVALive - 3027 - Corporative Network (并查集!!)
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
- uvalive 3027
- UVALive 3887 (暴力 并查集)
- (组队赛E/F Number of Connected Components )UVALive - 7638 (并查集)
- UVALive - 3027:Corporative Network
- UVALive 3027 Corporative Network (带权并查集)
- UVALive 4487 - Exclusive-OR -并查集 虚根