-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeisomorphism.cpp
More file actions
239 lines (223 loc) · 5.68 KB
/
Copy pathtreeisomorphism.cpp
File metadata and controls
239 lines (223 loc) · 5.68 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pii pair<int, int>
#define endl '\n'
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define vvpii vector<vpii>
typedef long long ll;
typedef long double ld;
using namespace std;
template<class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> void setmax(T& a, T b) { a = max(a, b); };
template<typename T> void setmin(T& a, T b) { a = min(a, b); };
template<typename T> bool in(T v, T lo, T hi) { return lo <= v && v <= hi; };
class TreeIsomorphism {
/*
Use this to check if two trees are isomorphic.
This does not validate the inputs. The
inputs must be valid trees. If there are loops,
multiple connected components, or cycles,
then the checker will not perform well.
Vertices must be numbered from [0, N)
Complexity: O(N)
*/
int N;
vi depthA, depthB, parA, parB;
vi findCenters(const vvi &T) {
// center is the vertex in the middle of the diameter
// there may be two centers of a Tree
vi vis(N, 0);
int u = 0;
vis[u] = 1;
queue<int> bfs;
bfs.push(u);
while (bfs.size()) {
u = bfs.front(); bfs.pop();
for (int v : T[u]) {
if (!vis[v]) {
vis[v] = 1;
bfs.push(v);
}
}
}
int E1 = u;
// reuse vis for distance
fill(vis.begin(), vis.end(), 0);
vi pre(N, -1);
pre[u] = u;
vis[u] = 1;
bfs.push(u);
while (bfs.size()) {
u = bfs.front(); bfs.pop();
for (int v : T[u]) {
if (pre[v] == -1) {
vis[v] = vis[u] + 1;
pre[v] = u;
bfs.push(v);
}
}
}
int E2 = u;
// cout << E1 << " " << E2 << endl;
if (E1 == E2) {
return {E1};
}
if (vis[E2] == 2) {
return {E1, E2};
}
vi centers;
int diam = vis[E2];
u = E2;
while (u != E1) {
if (diam & 1) {
if (vis[u] == (diam + 1) / 2) {
centers.pb(u);
}
} else {
if (vis[u] == diam / 2 || vis[u] == diam / 2 + 1) {
centers.pb(u);
}
}
u = pre[u];
}
return centers;
}
int fill_depths(const vvi A, const int root, vi &depth, vi &par) {
// returns depth of entire tree
// depths are computed from [0, D)
depth.assign(N, -1);
par.assign(N, -1);
int u = root;
depth[u] = 0;
queue<int> bfs;
bfs.push(u);
while (bfs.size()) {
u = bfs.front(); bfs.pop();
for (int v : A[u]) {
if (depth[v] == -1) {
par[v] = u;
depth[v] = depth[u] + 1;
bfs.push(v);
}
}
}
return depth[u] + 1;
}
bool rootedTreeIso(const vvi A, const int rootA, const vvi B, const int rootB) {
// cout << "roots: " << rootA << " " << rootB << endl;
int Adepth = fill_depths(A, rootA, depthA, parA);
int Bdepth = fill_depths(B, rootB, depthB, parB);
// cout << "depths: " << Adepth << " " << Bdepth << endl;
if (Adepth != Bdepth) {
return false;
}
int D = Adepth;
vvi atDepthA(D), atDepthB(D);
for (int i = 0; i < N; i++) {
atDepthA[depthA[i]].pb(i);
atDepthB[depthB[i]].pb(i);
}
vi labelA(N), labelB(N);
auto getLabelsAtLevel = [&](int level, const vvi &T, const vi &par, const vvi &atDepth, const vi &label) -> vector<pair<vi, int>> {
vector<pair<vi, int>> result;
for (int vertex : atDepth[level]) {
vi childlabels;
for (int ch : T[vertex]) {
if (ch == par[vertex]) continue;
childlabels.pb(label[ch]);
}
if (childlabels.size() == 0) {
childlabels.pb(0); // leaf node
} else {
sort(childlabels.begin(), childlabels.end());
}
result.pb({childlabels, vertex});
}
sort(result.begin(), result.end());
return result;
};
for (int d = D - 1; d >= 0; d--) {
auto levelA = getLabelsAtLevel(d, A, parA, atDepthA, labelA);
auto levelB = getLabelsAtLevel(d, B, parB, atDepthB, labelB);
int SZ = levelA.size();
if (levelB.size() != SZ) {
return false;
}
for (int i = 0; i < SZ; i++) {
if (levelA[i].f != levelB[i].f) {
return false;
}
}
int label = 1;
for (int i = 0; i < SZ; i++) {
int vertA = levelA[i].s;
int vertB = levelB[i].s;
if (i > 0 && levelA[i - 1].f != levelA[i].f) {
++label;
}
labelA[vertA] = labelB[vertB] = label;
}
}
return labelA[rootA] == labelB[rootB];
}
public:
// only public method
bool checkIfIsomorphic(const vvi &A, const vvi &B) {
N = A.size();
if (B.size() != N) {
return false;
}
vi centersA = findCenters(A);
vi centersB = findCenters(B);
// for (int x : centersA) cout << x << " "; cout << endl;
// for (int x : centersB) cout << x << " "; cout << endl;
if (centersA.size() != centersB.size()) {
return false;
}
for (int centerB : centersB) {
if (rootedTreeIso(A, centersA.back(), B, centerB)) {
return true;
}
}
return false;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vvi A(n), B(n);
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
--x;
--y;
A[x].pb(y);
A[y].pb(x);
}
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
--x;
--y;
B[x].pb(y);
B[y].pb(x);
}
TreeIsomorphism t;
if (t.checkIfIsomorphic(A, B)) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}