#include <iostream>
#include <set>
#include <vector>
#include <queue>
using namespace std;
const int maxN = 500'000;
vector<int> edge[1+maxN];
vector<int> parent(1+maxN, 0);
vector<int> subtree(1+maxN, 1);
vector<int> max_child(1+maxN, -1);
int rt = 1;
void dfs1(int u)
{
for(int v: edge[u])
{
if(parent[u] == v) continue;
parent[v] = u;
dfs1(v);
subtree[u] += subtree[v];
if(max_child[u] == -1 || subtree[v] > subtree[ max_child[u] ])
max_child[u] = v;
}
}
struct state
{
int i;
int c;
};
bool operator < (state A, state B)
{
return A.i < B.i;
}
int S[1+maxN];
vector<int> state_count(1+maxN, 0);
set<state>* T[1+maxN];
void add_state(int u, state x)
{
// cerr << "add_state " << u << ' ' << x.i << ' ' << x.c << '\n';
set<state>::iterator it = T[u]->find(x);
if(it == T[u]->end())
{
// cerr << "case A\n";
if(x.c != state_count[x.i])
T[u]->insert(x);
}
else
{
// cerr << "case B\n";
int ct = it->c + x.c;
T[u]->erase(it);
if(ct != state_count[x.i])
{
T[u]->insert(state{x.i, ct});
}
}
}
vector<int> is_bad(1+maxN, 0);
struct edg
{
int u;
int v;
edg(int U, int V)
{
u = min(U, V);
v = max(U, V);
}
};
bool operator < (edg A, edg B)
{
if(A.u == B.u) return A.v < B.v;
return A.u < B.u;
}
set<edg> bad_edges;
void dfs2(int u)
{
// cerr << "dfs2 " << u << ' ' << max_child[u] << '\n';
if(max_child[u] == -1)
{
// cerr << "case 1\n";
T[u] = new set<state>;
add_state(u, state{S[u], 1});
}
else
{
for(int v: edge[u])
{
if(v == parent[u]) continue;
dfs2(v);
}
// cerr << "case 2\n";
T[u] = T[ max_child[u] ];
// cerr << "x\n";
add_state(u, state{S[u], 1});
// cerr << "check X\n";
for(int v: edge[u])
{
// cerr << "v = " << v << '\n';
if(v == parent[u] || v == max_child[u]) continue;
for(state s: *(T[v]))
add_state(u, s);
T[v]->clear();
}
}
if(T[u]->size() == 0 && u != rt)
{
is_bad[u] = 1;
bad_edges.insert(edg(u, parent[u]));
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
subtree[0] = 1e9;
int N, K;
cin >> N >> K;
for(int i = 1; i <= N-1; i++)
{
int A, B;
cin >> A >> B;
edge[A].push_back(B);
edge[B].push_back(A);
}
for(int i = 1; i <= N; i++)
{
cin >> S[i];
state_count[ S[i] ]++;
}
if(N == 2)
{
cout << "1\n";
return 0;
}
// cerr << "check 0\n";
parent[1] = 1;
dfs1(1);
// cerr << "check 1\n";
dfs2(1);
// res = (1 + bad_count[rt])/2;
int res = 0;
queue<int> tbv;
vector<int> visit(1+N, 0);
for(int i = 1; i <= N; i++)
if(edge[i].size() == 1)
{
tbv.push(i);
}
set<edg> ans_set;
while(!tbv.empty())
{
int u = tbv.front();
tbv.pop();
if(visit[u]) continue;
visit[u] = 1;
// cerr << u << '\n';
for(int v: edge[u])
{
if(visit[v]) continue;
if(bad_edges.find(edg(u, v)) == bad_edges.end())
{
tbv.push(v);
}
else
{
ans_set.insert(edg(u, v));
}
}
}
int ct = ans_set.size();
// for(edg a: ans_set) cerr << a.u << ' ' << a.v << '\n';
res = (1+ct)/2;
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
21836 KB |
Output is correct |
2 |
Correct |
14 ms |
21788 KB |
Output is correct |
3 |
Correct |
15 ms |
21880 KB |
Output is correct |
4 |
Correct |
16 ms |
21888 KB |
Output is correct |
5 |
Correct |
14 ms |
21836 KB |
Output is correct |
6 |
Correct |
14 ms |
21836 KB |
Output is correct |
7 |
Correct |
14 ms |
21852 KB |
Output is correct |
8 |
Correct |
14 ms |
21836 KB |
Output is correct |
9 |
Incorrect |
14 ms |
21796 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
21836 KB |
Output is correct |
2 |
Correct |
14 ms |
21788 KB |
Output is correct |
3 |
Correct |
15 ms |
21880 KB |
Output is correct |
4 |
Correct |
16 ms |
21888 KB |
Output is correct |
5 |
Correct |
14 ms |
21836 KB |
Output is correct |
6 |
Correct |
14 ms |
21836 KB |
Output is correct |
7 |
Correct |
14 ms |
21852 KB |
Output is correct |
8 |
Correct |
14 ms |
21836 KB |
Output is correct |
9 |
Incorrect |
14 ms |
21796 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
21836 KB |
Output is correct |
2 |
Correct |
14 ms |
21788 KB |
Output is correct |
3 |
Correct |
15 ms |
21880 KB |
Output is correct |
4 |
Correct |
16 ms |
21888 KB |
Output is correct |
5 |
Correct |
14 ms |
21836 KB |
Output is correct |
6 |
Correct |
14 ms |
21836 KB |
Output is correct |
7 |
Correct |
14 ms |
21852 KB |
Output is correct |
8 |
Correct |
14 ms |
21836 KB |
Output is correct |
9 |
Incorrect |
14 ms |
21796 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
39740 KB |
Output is correct |
2 |
Correct |
192 ms |
44344 KB |
Output is correct |
3 |
Incorrect |
16 ms |
22152 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
21836 KB |
Output is correct |
2 |
Correct |
14 ms |
21788 KB |
Output is correct |
3 |
Correct |
15 ms |
21880 KB |
Output is correct |
4 |
Correct |
16 ms |
21888 KB |
Output is correct |
5 |
Correct |
14 ms |
21836 KB |
Output is correct |
6 |
Correct |
14 ms |
21836 KB |
Output is correct |
7 |
Correct |
14 ms |
21852 KB |
Output is correct |
8 |
Correct |
14 ms |
21836 KB |
Output is correct |
9 |
Incorrect |
14 ms |
21796 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |