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 <iostream> using namespace std; const int MAX = 100000; bool used[MAX+1]; struct Node { int parent; int rank; int opposite; } node[MAX+1]; int collapseFind(int x) { if(!node[x].parent) return x; return node[x].parent = collapseFind(node[x].parent); } int weightUnion(int x, int y) { if(node[x].rank == node[y].rank) node[x].rank++; return node[x].rank < node[y].rank?node[x].parent = y:node[y].parent = x; // node[x].parent = y; // // cout << "node "<< x << " " << "parent " << y << endl; } void combine(int a, int b) { if (node[a].opposite != b) { int n = node[b].opposite ? weightUnion(a, node[b].opposite) : a; int m = node[a].opposite ? weightUnion(b, node[a].opposite) : b; node[n].opposite = m; node[m].opposite = n; } } void solve() { char t[10], en; int n, m; int a, b; cin >> n >> m; //scanf("%d", &en); memset(node, 0, sizeof(Node)*(n+1)); memset(used, 0, sizeof(bool)*(n+1)); for(int i = 1; i <= m; i++) { scanf("%s %d %d", &t, &a, &b); if(t[0] == 'D') { used[a] = used[b] = true; } int c = collapseFind(a); int d = collapseFind(b); if(t[0] == 'A') { if(!used[c] || !used[d]) cout << "Not sure yet." << endl; else { if(c == d) cout << "In the same gang." << endl; else cout << "In different gangs." << endl; } } if(t[0] == 'D') { combine(c, d); continue; } } } int main() { int times; scanf("%d\n", ×); while(times--) { solve(); } //while(1); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator