標籤:uil post int mem amp iostream add key std
題目連結:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075
題意:
有n個結點,開始都是單獨的結點,如今有I操作和E操作,I u v表示吧u的父親結點設為,距離為|u - v| % 1000,E操作詢問u到根的距離
代碼:
#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>#include <stack>#include <map>using namespace std;int fa[1000010];int d[1000010];int fd(int x){ if (fa[x] != -1) { int rot = fd(fa[x]); d[x] += d[fa[x]]; return fa[x] = fd(fa[x]); } else return x;}int main(){ int a, b; int t, n; scanf("%d",&t); char cmd[10]; while (t--) { memset(fa, -1, sizeof(fa)); memset(d,0,sizeof(d)); scanf("%d", &n); while (scanf("%s", cmd) && cmd[0] != ‘O‘) { if (cmd[0] == ‘E‘) { scanf("%d", &a); fd(a); printf("%d\n", d[a]); } else { scanf("%d%d", &a, &b); fa[a] = b; d[a] = abs(a - b) % 1000; } } } return 0;}
UVA 1329Corporative Network【並查集】