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了3小时了,求测试数据我是用并查集,同时维护每个节点到根节点的奇偶性,2表示既可以奇数步到也可以偶数步到 #include <iostream> #include <fstream> using namespace std; const int N = 1000001; int case_num, n, k, a, b, c, ra; int root[N]; int type_to_root[N]; int Find(int x) { if(root[x] != root[root[x]]) { int temp = root[x]; root[x] = Find(root[x]); if(type_to_root[root[x]] == 2) { type_to_root[x] = 2; type_to_root[temp] = 2; } else type_to_root[x] = (type_to_root[x]+type_to_root[temp])%2; } return root[x]; } void Union(int x, int y) { int rx = Find(x); int ry = Find(y); if(rx != ry) { root[rx] = ry; if(type_to_root[rx] == 2 || type_to_root[ry] == 2) { type_to_root[rx] = 2; type_to_root[ry] = 2; } else type_to_root[rx] = (1+type_to_root[x]+type_to_root[y])%2; } else { if(type_to_root[x] == type_to_root[y]) { type_to_root[rx] = 2; } } } int main() { fstream fin("1227.txt"); cin>>case_num; while(case_num--) { cin>>n>>k; for(int i = 0; i < N; i++) { root[i] = i; type_to_root[i] = 0; } for(int i = 0; i < n; i++) { cin>>a>>c; for(int j = 0; j < c; j++) { cin>>b; Union(a, b); } } if(k == 1) { cout<<"YES"<<endl; } else { bool test = true; cin>>a; ra = Find(a); for(int i = 1; i < k; i++) { cin>>b; if(ra != Find(b) || (type_to_root[ra] != 2 && (type_to_root[a]+type_to_root[b]) == 1) ) { test = false; break; } } if(test) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator