-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJack's Crush - DFS, Coloring
76 lines (70 loc) · 1.48 KB
/
Jack's Crush - DFS, Coloring
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
/*
Jack wants to impress his crush during Aurora 2K18. He decided to do some tricks with the cards so that he can get the attention of his crush.
Jack made an permutation 'I' of N cards (cards are numbered 1, 2, 3 ... N), and showed it to his crush, but she doesn't like it, and asks him to get permutation 'F'.
Also, she gave him 'M' cool pairs of integers (ai,bi). Jack can perform following operation with his permutation 'I' :
Swap Ix and Iy if and only if (x,y) is a cool pair.
If Jack can successfully complete this task and get permutation 'F' she will go on a date with him.
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int> g[100001];
int col[100001];
int vis[100001];
int a[100001];
int b[100001];
int c;
void dfs(int u)
{
vis[u] = 1;
col[u] = c;
for(auto v:g[u])
{
if(!vis[v])
dfs(v);
}
}
signed main()
{
int t;
cin >> t;
while(t--)
{
c=0;
memset(col,0,sizeof col);
memset(vis,0,sizeof vis);
for(int i=1;i<=100000;i++)
g[i].clear();
int ind[100001];
int n,m;
cin >> n >> m;
for(int i=1;i<=n;i++)cin >> a[i],ind[a[i]]=i;
for(int i=1;i<=n;i++)cin >> b[i];
while(m--)
{
int u,v;cin >> u >> v;g[u].push_back(v);g[v].push_back(u);
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
c++;
dfs(i);
}
}
int flag = 1;
for(int i=1;i<=n;i++)
{
if(a[i] == b[i])
continue;
int x = i;
int y = ind[b[i]];
if(col[x] != col[y])
flag=0;
}
if(flag)
cout << "YES\n";
else
cout << "NO\n";
}
}