Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:一A留念

Posted by wangwenjie123 at 2016-03-19 20:40:42 on Problem 1703
In Reply To:一A留念 Posted by:555333444 at 2015-10-28 11:47:42
> #include <cstdio>
> #include <cstring>
> #define MAX_M 100100
> #define MAX_N 100100
> int f[MAX_N],N,M,T,op1,op2;
> int diff[MAX_N];
> void init(int N){for(int i = 0;i < N;++i)f[i] = i;}
> int find(int x){return f[x] == x ? f[x] : f[x] = find(f[x]);}
> void merge(int x,int y){x = find(x);y = find(y);f[x] = y;}
> bool same(int x,int y){return find(x) == find(y);}
> int main(){
>     scanf("%d",&T);
>     while(T--){
>         memset(diff,-1,sizeof(diff));
>         scanf("%d%d",&N,&M);getchar();
>         init(N + 10);
>         for(int i = 0;i < M;++i){
>             char ch = getchar();
>             scanf("%d%d",&op1,&op2);getchar();
>             if(ch == 'A'){
>                 if(same(op1,op2))printf("In the same gang.\n");
>                 else if(same(diff[op1],op2))printf("In different gangs.\n");
>                 else printf("Not sure yet.\n");
>             }else if(ch == 'D'){
>                 if(diff[op1] == -1)diff[op1] = op2;
>                 if(diff[op2] == -1)diff[op2] = op1;
>                 merge(diff[op1],op2);merge(diff[op2],op1);
>             }
>         }
>     }
>     return 0;
> }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator