洛谷 P1347 排序

题目描述

一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列A,B,C,D 表示A<B,B<C,C<D。在这道题中,我们将给你一系列形如A<B的关系,并要求你判断是否能够根据这些关系确定这个数列的顺序。

输入输出格式

输入格式:

 

第一行有两个整数n,m,n表示需要排序的元素数量,2<=n<=26,第1到n个元素将用大写的A,B,C,D....表示。m表示将给出的形如A<B的关系的数量。

接下来有m行,每行有3个字符,分别为一个大写字母,一个<符号,一个大写字母,表示两个元素之间的关系。

 

输出格式:

 

若根据前x个关系即可确定这n个元素的顺序yyy..y(如ABC),输出

Sorted sequence determined after xxx relations: yyy...y.

若根据前x个关系即发现存在矛盾(如A<B,B<C,C<A),输出

Inconsistency found after 2 relations.

若根据这m个关系无法确定这n个元素的顺序,输出

Sorted sequence cannot be determined.

(提示:确定n个元素的顺序后即可结束程序,可以不用考虑确定顺序之后出现矛盾的情况)

 

输入输出样例

输入样例#1:
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:
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 }
View Code

 

posted @ 2016-11-23 15:34  SBSOI  阅读(585)  评论(0编辑  收藏  举报