-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10129.cpp
More file actions
52 lines (50 loc) · 1.37 KB
/
10129.cpp
File metadata and controls
52 lines (50 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//Euler path
//Look out for the connectivity!
//Euler path<-> base graph is connected & degree property.
#include <iostream>
#include <cstring>
using namespace std;
int degree[26+10];
int connected[26+10];
bool used[26+10];
char buf[2000];
int main(){
int kase;
cin>>kase;
for(int i=0;i<kase;++i){
int n_words;
cin>>n_words;
memset(degree,0,sizeof(degree));
memset(used,0,sizeof(used));
for(int j=0;j<26;++j) connected[j]=j;
for(int j=0;j<n_words;++j){
cin>>buf;
int from=buf[0]-'a', to=buf[strlen(buf)-1]-'a';
++degree[from];
--degree[to];
used[from]=true; used[to]=true;
if(connected[from]!=connected[to]){
int small=connected[from],large=connected[to];
if(small>large) {small=to;large=from;}
for(int k=0;k<26;++k) {if(connected[k]==large) connected[k]=small;}
}
}
//for(int j=0;j<26;++j) cout<<static_cast<char>((used[j]?'A':'a')+connected[j]);
bool plusOne=false,minusOne=false,possible=true;
int tag=-1;
for(int j=0;j<26;++j){
if(used[j]){
if(tag==-1) tag=connected[j];
else if(tag!=connected[j]){possible=false;break;}
}
if(degree[j]==0) continue;
if(degree[j]==1 && !plusOne){plusOne=true; continue;}
if(degree[j]==-1 && !minusOne){minusOne=true; continue;}
possible=false;
break;
}
if(possible) cout<<"Ordering is possible."<<endl;
else cout<<"The door cannot be opened."<<endl;
}
return 0;
}