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 |
并查集老是WA,哪位帮忙看下代码。。#include <stdio.h> #include <string.h> int pre[30000]; int find(int x) { int p = x, t; while (pre[p] > 0) p = pre[p]; while (x != p) { t = pre[x], pre[x] = p, x = t; } return p; } void uni(int x, int y) { int a = find(x), b = find(y); if (pre[a] > pre[b]) { pre[b] += pre[a]; pre[a] = b; } else { pre[a] += pre[b]; pre[b] = a; } } int main(void) { int n, m, k, t1, t2; while (scanf("%d%d", &n, &m)) { if (n == 0 && m == 0) break; memset(pre, -1, sizeof(pre)); while (m--) { scanf("%d%d", &k, &t1); k--; while (k--) { scanf("%d", &t2); if (find(t1) != find(t2)) uni(t1, t2); t1 = t2; } } printf("%d\n", -pre[find(0)]); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator