forked from Autoratch/Thailand-Olympiad-in-Informatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoi06_gem.cpp
70 lines (64 loc) · 1.51 KB
/
toi06_gem.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
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 1;
int n,m;
vector<int> adj[N],rev[N];
stack<int> st;
set<int> now;
bool visited[N];
void dfs(int u)
{
if(visited[u]) return;
visited[u] = true;
for(int v : adj[u]) dfs(v);
st.push(u);
}
void dfs2(int u)
{
if(visited[u]) return;
visited[u] = true;
now.insert(u);
for(int v : rev[u]) dfs2(v);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 5;
while(t--)
{
cin >> m >> n;
for(int i = 1;i <= n*2;i++) adj[i].clear(),rev[i].clear(),visited[i] = false;
for(int i = 1;i <= m;i++)
{
int a,b;
cin >> a >> b;
adj[a+n].push_back(b);
adj[b+n].push_back(a);
rev[b].push_back(a+n);
rev[a].push_back(b+n);
}
for(int i = 1;i <= n/2;i++)
{
int a,b;
cin >> a >> b;
adj[a].push_back(b+n);
adj[b].push_back(a+n);
rev[b+n].push_back(a);
rev[a+n].push_back(b);
}
for(int i = 1;i <= n*2;i++) if(!visited[i]) dfs(i);
for(int i = 1;i <= n*2;i++) visited[i] = false;
bool ok = true;
while(!st.empty())
{
int u = st.top();
st.pop();
if(visited[u]) continue;
now.clear();
dfs2(u);
for(int x : now) if(now.find(x+n)!=now.end()) ok = false;
}
if(ok) cout << 'Y';
else cout << 'N';
}
}