标签:strong sub mat ase however cst soft number des
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 43855 | Accepted: 13509 |
Description
Input
Output
Sample Input
1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4
Sample Output
Not sure yet. In different gangs. In the same gang.
思路:弱化的食物链
代码:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<map> 7 #define pi acos(-1.0) 8 #define mj 9 #define inf 0x3f3f3f 10 typedef long long ll; 11 using namespace std; 12 const int N=1e5+5; 13 const int mod=1e9+7; 14 int f[N],dis[N]; 15 16 int find(int x){ 17 if(x==f[x]) 18 return x; 19 int t=f[x]; 20 f[x]=find(f[x]); 21 dis[x]=(dis[x]+dis[t])%2; 22 return f[x]; 23 } 24 25 void init(int n) 26 { 27 for(int i=0;i<=n;i++){ 28 f[i]=i; 29 dis[i]=0; 30 } 31 } 32 33 void unit(int x,int y) 34 { 35 int fx=find(x),fy=find(y); 36 if(fx!=fy) 37 { 38 f[fx]=fy; 39 dis[fx]=(1+dis[x]+dis[y])%2;//fx到根fy的距离 40 // if(x!=fx) dis[x]=(dis[fx]+dis[x])%2;操作已经在find里面完成,重复操作会出现错误! 41 } 42 } 43 int main() 44 { 45 int t; 46 scanf("%d",&t); 47 while(t--) 48 { 49 int n,m; 50 scanf("%d%d",&n,&m); 51 init(n); 52 int a,b; 53 getchar(); 54 for(int i=0;i<m;i++){ 55 char e; 56 scanf("%c %d %d",&e,&a,&b); 57 getchar(); 58 59 if(e==‘D‘){ 60 unit(a,b); 61 } 62 else 63 { 64 if(find(a)!=find(b)){ 65 puts("Not sure yet."); 66 } 67 else { 68 if(dis[a]^dis[b]==0){ 69 puts("In the same gang."); 70 } 71 else if(dis[a]^dis[b]!=0) puts("In different gangs."); 72 } 73 } 74 // for(int i=1;i<=n;i++){ 75 // printf("dis[%d]:%d\n",i,dis[i]); 76 // } 77 } 78 79 } 80 return 0; 81 82 }
标签:strong sub mat ase however cst soft number des
原文地址:http://www.cnblogs.com/mj-liylho/p/6442182.html