一個不一樣的值的升序排序數列指的是一個從左到右元素依次增大的序列,例如,一個有序的數列A,B,C,D 表示A<B,B<C,C<D。在這道題中,咱們將給你一系列形如A<B的關係,並要求你判斷是否可以根據這些關係肯定這個數列的順序。ios
輸入格式:ide
第一行有兩個整數n,m,n表示須要排序的元素數量,2<=n<=26,第1到n個元素將用大寫的A,B,C,D....表示。m表示將給出的形如A<B的關係的數量。spa
接下來有m行,每行有3個字符,分別爲一個大寫字母,一個<符號,一個大寫字母,表示兩個元素之間的關係。code
輸出格式:blog
若根據前x個關係便可肯定這n個元素的順序yyy..y(如ABC),輸出排序
Sorted sequence determined after xxx relations: yyy...y.ip
若根據前x個關係即發現存在矛盾(如A<B,B<C,C<A),輸出ci
Inconsistency found after 2 relations.get
若根據這m個關係沒法肯定這n個元素的順序,輸出string
Sorted sequence cannot be determined.
(提示:肯定n個元素的順序後便可結束程序,能夠不用考慮肯定順序以後出現矛盾的狀況)
1: 4 6 A<B A<C B<C C<D B<D A<B 2: 3 2 A<B B<A 3: 26 1 A<Z
1: Sorted sequence determined after 4 relations: ABCD. 2: Inconsistency found after 2 relations. 3: Sorted sequence cannot be determined.
————————————————我是分割線——————————————————-
1 /* 2 Problem: 3 OJ: 4 User:S.B.S. 5 Time: 6 Memory: 7 Length: 8 */ 9 #include<iostream> 10 #include<cstdio> 11 #include<cstring> 12 #include<cmath> 13 #include<algorithm> 14 #include<queue> 15 #include<cstdlib> 16 #include<iomanip> 17 #include<cassert> 18 #include<climits> 19 #include<functional> 20 #include<bitset> 21 #include<vector> 22 #include<list> 23 #include<map> 24 #define maxn 100001 25 #define F(i,j,k) for(int i=j;i<=k;i++) 26 #define rep(i,j,k) for(int i=j;i<k;i++) 27 #define M(a,b) memset(a,b,sizeof(a)) 28 #define FF(i,j,k) for(int i=j;i>=k;i--) 29 #define inf 0x3f3f3f3f 30 #define maxm 1001 31 #define mod 998244353 32 //#define LOCAL 33 using namespace std; 34 int read(){ 35 int x=0,f=1;char ch=getchar(); 36 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 37 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 38 return x*f; 39 } 40 void print(int n){ 41 if(n<0){putchar('-');n=0-n;} 42 if(n>=10) print(n/10); 43 putchar((n%10)+'0'); 44 return; 45 } 46 int n,m; 47 int p[maxn],in[maxn],out[maxn]; 48 int tot,ans; 49 vector<int> edge[maxn]; 50 char mp[4]; 51 bool vis[maxn],cur[maxn]; 52 inline bool solve(int u) 53 { 54 // cout<<"in solve!"<<endl; 55 vis[u]=true; 56 for(register int i=edge[u].size()-1;i>=0;i--){ 57 int v=edge[u][i]; 58 if(vis[v]) return false; 59 if(!solve(edge[u][i])) return false; 60 } 61 vis[u]=false; 62 return true; 63 } 64 inline void dfs(int u,int sum,int id) 65 { 66 // cout<<"int dfs!!"<<endl; 67 p[sum]=u; 68 if(sum==n){ 69 cout<<"Sorted sequence determined after "<<id<<" relations: "; 70 F(i,1,n) cout<<(char)(p[i]+'A'-1); 71 cout<<"."<<endl;exit(0); 72 } 73 for(register int i=edge[u].size()-1;i>=0;i--) dfs(edge[u][i],sum+1,id); 74 } 75 inline void ok(int u) 76 { 77 // cout<<"in ok !!!"<<endl; 78 F(i,1,n){ 79 if(cur[i]){ 80 M(vis,false); 81 if(!solve(i)){ 82 cout<<"Inconsistency found after "<<u<<" relations."<<endl; 83 exit(0); 84 } 85 } 86 } 87 if(tot!=n) return; 88 F(i,1,n) if(!in[i]) {dfs(i,1,u);break;} 89 } 90 int main() 91 { 92 // std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 93 #ifdef LOCAL 94 freopen("data.in","r",stdin); 95 freopen("data.out","w",stdout); 96 #endif 97 n=read();m=read(); 98 F(i,1,m){ 99 // cout<<"in main!!!!"<<endl; 100 scanf("%s",mp); 101 int a=mp[0]-'A'+1,b=mp[2]-'A'+1; 102 out[a]++;in[b]++; 103 if(!cur[a]) tot++; 104 if(!cur[b]) tot++; 105 cur[a]=cur[b]=true; 106 edge[a].push_back(b); 107 ok(i); 108 } 109 cout<<"Sorted sequence cannot be determined."<<endl; 110 return 0; 111 }