一道比较简单的关于树的题目,首先我们要明确的是,后面的u和v代表了这两个节点相通,那么我们需要判断是否要把他们剪下来,最后得出答案。
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
void work()
{
int n ; cin >> n ;
vector<vector<int>>tre(n + 1);
string s ; cin >> s;
s = " " + s ;
int h = n - 1 ;
int ans = 0 ;
while(h--)
{
int u , v ; cin >> u >> v;
tre[u].push_back(v);
tre[v].push_back(u);
if(s[u] == s[v])
{
ans++;
}
}
cout << ans << endl ;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--)
{
work();
}
return 0;
}

京公网安备 11010502036488号