Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
本来以为是水题,结果A得很痛苦。。。//本人菜鸟一枚,还在凌乱的小伙伴可以看看 #include<iostream> #include<cstring> #include<stdio.h> using namespace std; int par[100010], dpar[100010], r[100010];//r是根的高度,dpar[i]==j表示i与j对立 bool vs[100010]; int find(int x) { if (x == par[x])return x; else return par[x] = find(par[x]); } void unite(int x, int y) { int a = find(x), b = find(y); if (a != b) { if (r[a] < r[b])par[a] = b; else par[b] = a; if (r[a] == r[b])r[a]++; } return; } void init(int n) { memset(dpar, -1, sizeof(dpar)); memset(vs, 0, sizeof(vs)); memset(r, 0, sizeof(r)); for (int i = 0; i <= n; i++)par[i] = i; return; } int main() { char s; int a, b, tes, n, m ; scanf("%d", &tes); while(tes--) { scanf("%d%d", &n, &m); init(n); while (m--) { cin >> s ; scanf("%d%d", &a, &b); if (s == 'D') { if (!vs[a])dpar[a] = b; if (!vs[b])dpar[b] = a; vs[a] = vs[b] = 1; unite(dpar[a], b); unite(dpar[b], a); continue; } if (s == 'A') { if (find(a) == find(b))printf("In the same gang.\n"); else if (find(dpar[a]) == find(b))printf("In different gangs.\n");//别用find(a)!=find(b)判断(此处凌乱几小时··) else printf("Not sure yet.\n"); } } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator