题目链接:
思路:先合并,然后判断根结点是否唯一,如果是,则在判断是否是欧拉路;否则,直接结束。
View Code
1 #include2 #include 3 #include 4 using namespace std; 5 const int MAXN=100000+10; 6 int In[MAXN],Out[MAXN]; 7 int parent[MAXN]; 8 bool mark[MAXN]; 9 int n;10 11 12 void Initiate(){13 for(int i=0;i =0;s=parent[s]);21 while(s!=x){22 int tmp=parent[x];23 parent[x]=s;24 x=tmp;25 }26 return s;27 }28 29 void Union(int R1,int R2){30 int r1=Find(R1);31 int r2=Find(R2);32 if(r1!=r2){33 parent[r1]=r2;34 }35 }36 37 int main(){38 int _case;39 scanf("%d",&_case);40 while(_case--){41 scanf("%d",&n);42 Initiate();43 memset(mark,false,sizeof(mark));44 memset(In,0,sizeof(In));45 memset(Out,0,sizeof(Out));46 for(int i=0;i 1){63 printf("The door cannot be opened.\n");64 }else {65 int p[27];66 int k=0;67 for(int i=0;i<26;i++){68 if(mark[i]&&In[i]!=Out[i]){69 p[k++]=i;70 }71 }72 if(k==0){73 printf("Ordering is possible.\n");74 }else {75 if(k==2&&abs(In[p[0]]-Out[p[0]])==1&&abs(In[p[1]]-Out[p[1]])==1){76 printf("Ordering is possible.\n");77 }else 78 printf("The door cannot be opened.\n");79 }80 }81 }82 return 0;83 }