題目地址:點擊打開鏈接
並查集和歐拉回路
C++代碼:
#include#include #include using namespace std; bool flag; int d[700]; int find_father(int x) { while(d[x]!=-1) return find_father(d[x]); return x; } int main() { int m; while(cin>>m) { while(m--) { int n; cin>>n; int a[26][2];//0:in;1:out memset(a,0,sizeof(a)); int i; for(i=0;i<700;++i) d[i]=-2; for(i=0;i >s; int x=s[0]-'a'; int y=s[s.size()-1]-'a'; ++a[x][0]; ++a[y][1]; if(d[x]==-2) d[x]=-1; if(d[y]==-2) d[y]=-1; int father_x=find_father(x),father_y=find_father(y); if(father_x!=father_y) d[father_x]=father_y; } int equal=0,less=0,greater=0; bool flag=true; for(i=0;i<26;++i) { if(a[i][0]==a[i][1]) ++equal; else { if(a[i][0]-a[i][1]==1) ++greater; else { if(a[i][1]-a[i][0]==1) ++less; else { flag=false; break; } } } } int num=0; for(i=0;i<700;++i) { if(d[i]==-1) ++num; } if(flag&&((less==0&&greater==0)||(less==1&&greater==1))&&num==1) cout<<"Ordering is possible."<