-
Notifications
You must be signed in to change notification settings - Fork 0
/
1060.cpp
92 lines (90 loc) · 3.08 KB
/
1060.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
int map[30][30],appear[30];
int n,m,j,sum;
bool de,co;
string ans;
void toposort()
{
queue<int>q;
ans="";
int size[30],flag=false,dn=0,cnt=0;
memset(size,0,sizeof(size));
for(int i=1;i<=n;i++)
{
if(!appear[i])
continue;
for(int j=1;j<=n;j++)
if(map[j][i])
size[i]++;
if(!size[i])
{
q.push(i);
}
}
if(q.empty())
co=true;
if(q.size()>1)
flag=true;
while(!q.empty())
{
int s=q.front();
q.pop();
ans+=char(s+'A'-1);
dn++;
for(int i=1;i<=n;i++)
if(map[s][i])
{
size[i]--;
if(size[i]==0)
q.push(i);
}
if(q.size()>1)
flag=true;
}
for(int i=1;i<=n;i++)
if(appear[i])cnt++;
if(cnt==dn&&!flag&&cnt==n)
de=true;
if(cnt>dn)
co=true;
}
int main()
{
char a[3];
while(scanf("%d%d",&n,&m),!(m==0&&n==0))
{
sum=0;
int count=0;
de=co=false;
memset(map,0,sizeof(map));
memset(appear,0,sizeof(appear));
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%s",a);
if(de||co)continue;
x=*a-'A'+1;
y=*(a+2)-'A'+1;
map[x][y]=1;
appear[x]=appear[y]=1;
toposort();
if(de||co)
count=i;
}
if(de)
{
cout<<"Sorted sequence determined after "<<count<<" relations: "<<ans<<"."<<endl;
}
else
if(co)
cout<<"Inconsistency found after "<<count<<" relations."<<endl;
else
cout<<"Sorted sequence cannot be determined."<<endl;
}
return 0;
}