-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathreversingroads.cc
75 lines (75 loc) · 1.49 KB
/
reversingroads.cc
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
// https://open.kattis.com/problems/reversingroads
#include<bits/stdc++.h>
using namespace std;
using ii=tuple<int,int>;
using vii=vector<ii>;
using vi=vector<int>;
using vvi=vector<vi>;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,u,v;
for(int T=1;cin>>n>>m;T++){
vvi g(n),h(n);
vii e;
for(int i=0;i<m;i++){
cin>>u>>v;
g[u].push_back(v);
h[v].push_back(u);
e.emplace_back(u,v);
}
vi a,s(n),t(n),r(n);
function<void(int)> dfs1=[&](int i){
s[i]=1;
for(int j:g[i])
if(!s[j])dfs1(j);
a.push_back(i);
};
for(int i=0;i<n;i++)
if(!s[i])dfs1(i);
reverse(a.begin(),a.end());
int k=0;
function<void(int)> dfs2=[&](int i){
t[i]=k;
for(int j:h[i])
if(!t[j])dfs2(j);
};
for(int i=0;i<a.size();i++)
if(!t[a[i]]){
k++;
dfs2(a[i]);
}
function<void(int)>dfs3=[&](int i){
r[i]=1;
for(int j:g[i])
if(!r[j])dfs3(j);
};
for(int i=0;i<n;i++)
if(t[i]==1){
dfs3(i);
break;
}
bool o=1;
for(int i=0;i<n;i++){
if(!r[i])o=0;
}
cout<<"Case "<<T<<": ";
if(k==1){
cout<<"valid\n";
continue;
}
if(!o){
cout<<"invalid\n";
continue;
}
vii f;
for(ii x:e){
tie(u,v)=x;
if(t[u]==1&&t[v]==k)f.push_back(x);
}
if((k==2&&f.size()>=2)||(k>2&&f.size())){
tie(u,v)=f[0];
cout<<u<<" "<<v<<"\n";
}else cout<<"invalid\n";
}
}