-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1700.cpp
46 lines (44 loc) · 983 Bytes
/
1700.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
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define emb emplace_back
using namespace std;
using vi = vector<int>;
struct Ahu {
int n, cnt = 0;
vector<vi> g1, g2;
map<vi, int> mp;
int dfs(vector<vi> &g, int u, int p = -1) {
vi id;
for (int v: g[u])
if (v != p) id.emb(dfs(g, v, u));
sort(all(id));
if (!mp.count(id)) mp[id] = ++cnt;
return mp[id];
}
bool check() {
return (dfs(g1, 0) == dfs(g2, 0));
}
};
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T; cin >> T;
Ahu ahu;
while (T--) {
Ahu ahu;
cin >> ahu.n;
ahu.g1.resize(ahu.n);
ahu.g2.resize(ahu.n);
for (int i = 1, u, v; i < ahu.n; ++i) {
cin >> u >> v; --u; --v;
ahu.g1[u].emb(v);
ahu.g1[v].emb(u);
}
for (int i = 1, u, v; i < ahu.n; ++i) {
cin >> u >> v; --u; --v;
ahu.g2[u].emb(v);
ahu.g2[v].emb(u);
}
cout << (ahu.check() ? "YES\n" : "NO\n");
}
return 0;
}