题解分享
题解分享简介
团建(编程题) - 题解
```cpp
// https://www.lanqiao.cn/problems/19704/learning/?page=1&first_category_id=1&name=%E5%9B%A2%E5%BB%BA
#include <bits/stdc++.h>
#define N 200020
using namespace std;
typedef long long ll;
ll n, m, ans = 0;
vector<ll> t1v(N), t2v(N); // value[]
map<ll, vector<ll>> tree1, tree2; // id, list[]
void dfs(ll x, ll y, ll cnt) {
if (t1v[x] != t2v[y]) return;
ans = max(ans, cnt + 1);
for (int i = 0; i < tree1[x].size(); i++)
for (int j = 0; j < tree2[y].size(); j++)
dfs(tree1[x][i], tree2[y][j], cnt + 1);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> t1v[i];
for (int i = 1; i <= m; i++) cin >> t2v[i];
for (int i = 1; i < n; i++) {
ll u, v;
cin >> u >> v;
tree1[u].push_back(v);
}
for (int i = 1; i < m; i++) {
ll p, q;
cin >> p >> q;
tree2[p].push_back(q);
}
dfs(1, 1, 0);
cout << ans << endl;
return 0;
}
```
查看全文
1
0
1
3



