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 |
并查集做的 188ms (附代码)#include<stdio.h> #define max 50000 int father[max+1]; int s[max+1][2]; //#include <string.h> //#include <stdlib.h> //#include <time.h> int n=80,k=300; int hua[1111][3]={0,0,0 }; fun_max(int a,int b) {return a>b?a:b;} fun_min(int a,int b) {return a<b?a:b; } find(int n) {int temp,ans,i; int s0=0,s1=0; if(n<=0)printf("error\n"); for(i=n;i!=father[i];) {s[i][0]=fun_max(s[i][0],s0); s[i][1]=fun_max(s[i][1],s1); s0=s[i][0]; s1=s[i][1]; i=father[i];} ans=i; for(i=n;i!=father[i];){ temp=father[i]; father[i]=ans;i=temp; } return ans; } judge(int i,int j) { int n1,n2,n3,n4; n1=find(i); n2=find(j); if(n1==n2)return 1;//同类 if((s[n1][1]!=0)&&(find(s[n1][1])==n2) )return 2; //i catch j if((s[n2][1]!=0)&&(find(s[n2][1])==n1) )return 3; //j catch i return 0; //random } init() {int a; for(a=1;a<=n;a++){father[a]=a;s[a][0]=s[a][1]=0;} } fun_merge(int i,int j) { int n1,n2; if(i==0||j==0)return i+j; n1=find(i); n2=find(j); father[n1]=n2; return n2; } fun_equal(int i,int j) { int n1=find(i),n2=find(j); int n3,n4,n5; n3=fun_merge(n1,n2); n4=fun_merge(s[n1][0],s[n2][0]); n5=fun_merge(s[n1][1],s[n2][1]); if(n4!=0){ s[n4][0]=n5;s[n4][1]=n3; } if(n5!=0){ s[n5][0]=n3;s[n5][1]=n4; } s[n3][0]=n4; s[n3][1]=n5; } fun_catch(int i,int j) { int n1,n2; int n3,n4,n5; n1=find(i); n2=find(j); n3=fun_merge(s[n1][0],s[n2][1]); n4=fun_merge(n1,s[n2][0]); n5=fun_merge(s[n1][1],n2); s[n4][0]=n3;s[n4][1]=n5; if(n3!=0){ s[n3][0]=n5; s[n3][1]=n4; } if(n5!=0){ s[n5][0]=n4;s[n5][1]=n3; } } fun() {int a,b,c; int n1,n2,n3; int ans=0; int temp; int f; init(); //fun_catch(1,5);fun_catch(9,3);fun_equal(1,8);fun_equal(2,5);fun_equal(2,9); //for(a=1;a<=n;a++){printf("%d ",father[a]); }printf("\n");for(a=1;a<=n;a++){printf("%d %d\n",s[a][0],s[a][1]); } for(a=1;a<=k;a++) { temp=ans; //n1=hua[a-1][0];n2=hua[a-1][1];n3=hua[a-1][2]; scanf("%d%d%d",&n1,&n2,&n3); if(n2>n||n3>n){ ans++;} else { if(n1==1) { if(n2==n3){ } else { f=judge(n2,n3); if(f==0){ fun_equal(n2,n3);} else if(f==1){} else if(f==2||f==3){ ans++; } } } else if(n1==2) { if(n2==n3){ ans++;} else{ f=judge(n2,n3); if(f==0){ fun_catch(n2,n3); } else if(f==1||f==3){ans++;} else if(f==2){} } } } //printf("%s",temp==ans?"t":"f"); //for(b=1;b<=n;b++){printf("%d ",father[b]); }printf("\n");for(b=1;b<=n;b++){printf("%d %d\n",s[b][0],s[b][1]); }printf("\n"); } printf("%d\n",ans); } //#include"11.h" int main() {scanf("%d",&n); scanf("%d",&k); fun(); //printf("right answer: "); fun2(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator