1 solutions
-
0
C++ :
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> using namespace std; int parent[100010],num[100010]; int root(int p) { if(parent[p]==-1)return p; int t=parent[p]; parent[p]=root(parent[p]); num[p]=(num[p]+num[t])%2; return parent[p]; } void merge(int a,int b) { int x=root(a),y=root(b); parent[x]=y; num[x]=(num[b]-num[a]+1)%2; } int main() { int j,i,m,n; while(scanf("%d%d",&n,&m)!=EOF) { memset(parent,-1,sizeof(parent)); memset(num,0,sizeof(num)); for(i=1;i<=m;i++) { char ch; int a,b; cin>>ch>>a>>b; if(ch=='D') { if(root(a)!=root(b)) merge(a,b); } else { if(root(a)!=root(b)) printf("Sorry,I'm not sure.\n"); else if(num[a]==num[b])printf("In one alliance.\n"); else printf("Not in one alliance.\n"); } } } return 0; }
- 1
Information
- ID
- 17317
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By