#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define pll pair<lli,lli>
#define MAX 500000
lli n,a,b,k,cont,general,res;
vector<lli> hijos[MAX+2];
lli id_g[MAX+2],e_g[MAX+2],todos[MAX+2],sub[MAX+2],activos[MAX+2],marcados[MAX+2];
pll euler[MAX+2];
void suma(lli g) {
activos[g]++;
if(activos[g] == 1) general++;
if(activos[g] == todos[g]) general--;
}
void dfs1(lli pos, lli padre,lli keep) { //tiene que ser con un small to large
lli bigchlid = -1;
lli tam = 0;
for(auto h : hijos[pos]) {
if(h == padre) continue;
if(tam < sub[h]) {
tam = sub[h];
bigchlid = h;
}
}
for(auto h : hijos[pos]) {
if(h == padre || h == bigchlid) continue;
dfs1(h,pos,0);
}
if (bigchlid != -1) {
dfs1(bigchlid,pos,1);
rep(i,euler[pos].first, euler[bigchlid].first - 1) suma(e_g[i]);
rep(i,euler[bigchlid].second+1, euler[pos].second) suma(e_g[i]);
}
else suma(id_g[pos]);
if(general > 0) marcados[pos] = 1;
if (!keep) { //confirmar que se resta del vector correspondiente (grupo esta incorrecto)
rep(i,euler[pos].first, euler[pos].second) activos[e_g[i]] = 0;
general = 0;
}
}
void eul(lli pos,lli padre) {
e_g[cont] = id_g[pos];
euler[pos].first = cont++;
sub[pos] = 1;
for(auto h : hijos[pos]) {
if (h == padre) continue;
eul(h,pos);
sub[pos] += sub[h];
}
euler[pos].second = cont-1;
}
bool dfs2(lli pos, lli padre) {
bool respuesta = false;
for(auto h : hijos[pos]) {
if(h == padre) continue;
respuesta |= dfs2(h,pos);
}
if(!respuesta && marcados[pos]) {
res++;
respuesta=true;
}
return respuesta;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
rep(i,1,n-1) {
cin >> a >> b;
hijos[a].push_back(b);
hijos[b].push_back(a);
}
rep(i,1,n) {
cin >> id_g[i];
todos[id_g[i]]++;
}
cont = 1;
eul(1,0);
dfs1(1,0,1);
marcados[1] = 0;
dfs2(1,0);
res++;
res/=2;
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
12068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
12068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
12068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
22076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
12068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |