0%

[題解]元件測試排程問題

a136. 元件測試排程問題

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
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define double long double
#define N 30
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define INF 2e18
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define rrep(i,l,r) for(int i=l;i<r;i++)
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int n,m,ans[N];;
bool edge[N][N],is_root[N];

bool is_front(int u, int v){
if(edge[u][v])return true;
for(int i=0;i<n;i++){
if(edge[u][i] && is_front(i,v))
return true;
}
return false;
}

bool togological(){
int deg[N],sum = 0,ind = 0;
memset(deg,0,sizeof(deg));
rep(i,0,n-1){
rep(j,0,n-1){
if(edge[i][j])deg[j] += 1;
}
}
queue<int> que;
rep(i,0,n-1)if(deg[i]==0){
if(sum == 1)return false;
sum += 1;
que.push(i);
}
while(!que.empty()){
int cur = que.front();sum = 0;
ans[ind++] = cur;
que.pop();
for(int i=0;i<n;i++){
if(edge[cur][i]){
deg[i]--;
if(deg[i] == 0){
if(sum == 1)return false;
sum += 1;
que.push(i);
}
}
}
}
if(ind < n)return false;
return true;
}


signed main(){
Orz;
cin>>n>>m;
bool flag = 1;
memset(edge,0,sizeof(edge));
fill(is_root,is_root+N,1);
for(int i=1;i<=m;i++){
char a,b;cin>>a>>b;
int from = a-'A',to = b-'A';
is_root[to] = 0;
edge[from][to] = 1;
if(is_front(to,from)){
flag = 0;
cout<<"Order conflict after getting pair "<<i<<endl;
break;
}
else if(togological()){
flag = 0;
cout<<"Determine the testing sequence after getting pair "<<i<<" : ";
for(int i=0;i<n;i++)cout<<(char)(ans[i]+'A');
cout<<endl;
break;
}
}
if(flag)cout<<"No answer"<<endl;
}