Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- const int maxn = 200000 + 100;
- int n, m, u, v;
- int c[maxn], d[maxn];
- vector<int> G1[maxn], G2[maxn];
- map<int, int> mp[maxn];
- void dfs1(int root, int fa) {
- for (int pos : G1[root]) {
- if (pos == fa) {
- continue;
- }
- mp[root][c[pos]] = pos;
- dfs1(pos, root);
- }
- }
- int dfs2(int root, int fa, int other) {
- int mx = 0;
- map<int, int>::iterator it;
- for (int pos : G2[root]) {
- if (pos == fa) {
- continue;
- }
- it = mp[other].find(d[pos]);
- if (it != mp[other].end()) {
- mx = max(mx, dfs2(pos, root, it->second));
- }
- }
- return mx + 1;
- }
- int main() {
- #ifdef ExRoc
- freopen("test.txt", "r", stdin);
- #endif
- ios::sync_with_stdio(false);
- cin >> n >> m;
- for (int i = 1; i <= n; ++i) {
- cin >> c[i];
- }
- for (int i = 1; i <= m; ++i) {
- cin >> d[i];
- }
- for (int i = 1; i < n; ++i) {
- cin >> u >> v;
- G1[u].push_back(v);
- G1[v].push_back(u);
- }
- for (int i = 1; i < m; ++i) {
- cin >> u >> v;
- G2[u].push_back(v);
- G2[v].push_back(u);
- }
- dfs1(1, 1);
- if (c[1] != d[1]) {
- cout << 0 << endl;
- return 0;
- }
- cout << dfs2(1, 1, 1) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement