-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathPermutationsswap.cpp
131 lines (109 loc) · 3.47 KB
/
Permutationsswap.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
Kevin has a permutation P of N integers 1, 2, ..., N, but he doesn't like it. Kevin wants to get a permutation Q.
Also he believes that there are M good pairs of integers (ai , bi). Kevin can perform following operation with his permutation:
Swap Px and Py only if (x, y) is a good pair.
Help him and tell if Kevin can obtain permutation Q using such operations.
Input format:
The first line of input will contain an integer T, denoting the number of test cases.
Each test case starts with two space-separated integers N and M. The next line contains N space-separated integers Pi. The next line contains N space-separated integers Qi. Each of the next M lines contains two space-separated integers ai and bi.
Output format:
For every test case output "YES" (without quotes) if Kevin can obtain permutation Q and "NO" otherwise.
Constraints:
1 ≤ T ≤ 10
2 ≤ N ≤ 100000
1 ≤ M ≤ 100000
1 ≤ Pi, Qi ≤ N. Pi and Qi are all distinct.
1 ≤ ai < bi ≤ N
SAMPLE INPUT
2
4 1
1 3 2 4
1 4 2 3
3 4
4 1
1 3 2 4
1 4 2 3
2 4
SAMPLE OUTPUT
NO
YES
*/
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int> *graph, int start, bool *isVisited, unordered_set<int> *part){
isVisited[start] = true;
part->insert(start);
for(int i = 0; i < graph[start].size(); i++){
int next = graph[start][i];
if(!isVisited[next]){
dfs(graph, next, isVisited, part);
}
}
}
unordered_set< unordered_set<int> *> *getComponent(vector<int> *graph, int n){
bool *isVisited = new bool[n];
for(int i = 0; i < n; i++) isVisited[i] = false;
unordered_set< unordered_set<int> * > *component = new unordered_set< unordered_set<int> * >();
for(int i = 0; i < n; i++){
if(!isVisited[i]){
unordered_set<int> *part = new unordered_set<int>();
dfs(graph, i, isVisited, part);
component->insert(part);
}
}
return component;
}
int main(){
int flag;
int t;
cin >> t;
while(t--){
flag = 1;
int n, m;
cin >> n >> m;
int *p = new int[n];
for(int i = 0; i < n; i++){
int x;
cin >> x;
p[i] = x-1;
}
int *q = new int[n+1];
for(int i = 0; i < n; i++){
int x;
cin >> x;
q[i] = x-1;
}
vector<int> *graph = new vector<int>[n];
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
graph[a-1].push_back(b-1);
graph[b-1].push_back(a-1);
}
unordered_set< unordered_set<int> * > *component = getComponent(graph, n);
// for(unordered_set< unordered_set<int> * >::iterator i = component->begin(); i != component->end(); i++){
// for(unordered_set<int>::iterator j = (*i)->begin(); j != (*i)->end(); j++){
// cout << (*j)+1 << " ";
// }
// cout << endl;
// }
for(unordered_set< unordered_set<int> * >::iterator i = component->begin(); i != component->end(); i++){
set<int> setA;
set<int> setB;
for(unordered_set<int>::iterator j = (*i)->begin(); j != (*i)->end(); j++){
setA.insert(p[*j]);
setB.insert(q[*j]);
}
if(setA != setB){
flag = 0;
cout << "NO" << endl;
break;
}
}
if(flag) cout << "YES" << endl;
}
}