#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 5e5 + 7;
const int maxState = (1 << 8);
int n, m, u, v, Min[maxn], Max[maxn], cnt, sum[maxn], st[maxn], last1 = 0, en[maxn], ITSum[maxn * 4], ITMax[4 * maxn];
vector<int> vt[maxn], col, ahihi;
int ans = 0;
set<int> St;
bool ok[maxn];
/// first: sum
/// second: last
void Update(int id, int l, int r, int pos, pair<int, int> x)
{
if(l == r)
{
ITSum[id] = x.first;
ITMax[id] = x.second;
return;
}
int mid = l + r >> 1;
if(pos <= mid) Update(id * 2,l , mid, pos, x);
else Update(id * 2 + 1, mid + 1, r, pos, x);
ITSum[id] = ITSum[id * 2] + ITSum[id * 2 + 1];
ITMax[id] = max(ITMax[id * 2], ITMax[id * 2 + 1]);
}
pair<int, int> Get(int id, int l, int r, int u, int v)
{
if(l > v || r < u) return {0, 0};
if(u <= l && r <= v)
{
return {ITSum[id], ITMax[id]};
}
int mid = l + r >> 1;
pair<int, int> gau1 = Get(id * 2, l, mid, u, v);
pair<int, int> gau2 = Get(id * 2 + 1, mid + 1, r, u, v);
return {gau1.first + gau2.first, max(gau1.second, gau2.second)};
}
void DFS(int u, int p)
{
st[u] = ++cnt;
for(auto v: vt[u])
{
if(v != p) DFS(v, u);
}
en[u] = cnt;
}
void DFS2(int u, int p, int last)
{
if(ok[u]) last = u;
int aha = 0;
if(last1 >= 0) last1 = u;
for(auto v: vt[u])
{
if(v != p)
{
aha++;
}
}
if(aha != 1 && last1 > 0)
{
last1 = -last1;
}
for(auto v:vt[u])
{
if(v != p) DFS2(v, u, last);
}
if(!aha && last != -1)
{
St.insert(last);
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; i++) Min[i] = INT_MAX;
for(int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
vt[u].push_back(v);
vt[v].push_back(u);
}
DFS(1, 1);
for(int i = 1; i <= n; i++)
{
ahihi.push_back(i);
int x;
cin >> x;
Max[x] = max(Max[x], st[i]);
Min[x] = min(Min[x], st[i]);
sum[x]++;
}
for(int i = 1; i <= m; i++) Update(1, 1, n, Min[i], {sum[i], Max[i]});
for(int i = 1; i <= n; i++)
{
pair<int, int> gau = Get(1, 1, n, st[i], en[i]);
if(gau.second <= en[i] && gau.first == en[i] - st[i] + 1)
{
ok[i] = true;
}
}
DFS2(1, 1, -1);
for(int i = 1; i <= st[-last1]; i++) if(ok[i]) ans++;
cout << (ans > 1) + St.size() - 1;
}
Compilation message
mergers.cpp: In function 'void Update(long long int, long long int, long long int, long long int, std::pair<long long int, long long int>)':
mergers.cpp:21:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
mergers.cpp: In function 'std::pair<long long int, long long int> Get(long long int, long long int, long long int, long long int, long long int)':
mergers.cpp:35:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
mergers.cpp: In function 'int32_t main()':
mergers.cpp:81:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:81:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
12152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
12152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
12152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
19180 KB |
Output is correct |
2 |
Incorrect |
177 ms |
29596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
12152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |