Untitled
unknown
plain_text
2 years ago
1.2 kB
5
Indexable
#include <iostream> #include <cmath> #include <vector> #include <map> #include <algorithm> using namespace std; typedef long long ll; map<vector<ll>, int> m; vector<vector<ll>> a(100000+1), b(100000+1); ll getHash(vector<ll> &values) { if (m.find(values) != m.end()) return m[values]; m.insert({values, (int)values.size()}); return m[values]; } ll dfs(ll v, bool f, ll p) { vector<ll> values; if (f) { for (auto x : a[v]) { if (x != p) { values.push_back(dfs(x, f, v)); } } } else { for (auto x : b[v]) { if (x != p) { values.push_back(dfs(x, f, v)); } } } sort(values.begin(), values.end()); return getHash(values); } int main() { ll n, r1, r2, u, v; cin >> n >> r1 >> r2; for (int i=0; i < n-1; i++) { cin >> u >> v; a[u].push_back(v); a[v].push_back(u); } for (int i=0; i < n-1; i++) { cin >> u >> v; b[u].push_back(v); b[v].push_back(u); } if (dfs(r1, 1, 0) == dfs(r2, 0, 0)) { cout << "YES"; } else { cout << "NO"; } return 0; }
Editor is loading...