#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);cin.tie(0);
int N, M, ans=1, t=0;
cin>>N>>M;
vector<int> C(N), S(N), L(M, 0x3f3f3f3f), R(M);
vector<vector<int>> G(N);
for(int i=1; i<N; i++) {
int x, y;
cin>>x>>y; --x, --y;
G[x].push_back(y);
G[y].push_back(x);
}
function<void(int, int)> f1=[&](int i, int p) {
S[i]=++t;
for(int j:G[i]) if(j!=p) f1(j, i);
};
f1(0, 0);
for(int i=0; i<N; i++) {
cin>>C[i], --C[i];
L[C[i]]=min(L[C[i]], S[i]);
R[C[i]]=max(R[C[i]], S[i]);
}
t=0;
function<pair<int, int>(int, int)> f2=[&](int i, int p) {
int a=L[C[i]], b=R[C[i]];
++t;
for(int j:G[i]) if(j!=p) {
auto[c,d]=f2(j, i);
a=min(a, c);
b=max(b, d);
}
if(i && S[i]<=a && b<=t) ans++, a=-1;
return make_pair(a, b);
};
for(int i=0; i<N; i++) if(G[i].size()<2) { f2(i, i); break; }
cout<<ans/2<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
7012 KB |
Output is correct |
2 |
Incorrect |
52 ms |
7780 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |