This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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], deg[maxn], cnt, sum[maxn], cur, st[maxn], last1 = 0, en[maxn], ITSum[maxn * 4], ITMax[4 * maxn];
vector<int> vt[maxn], col, ahihi;
vector<int> prvans, ans;
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 Add(int x)
{
while(++cur < prvans.size() && st[prvans[cur]] <= en[x])
{
deg[x]++;
deg[prvans[cur]]++;
Add(prvans[cur]);
}
cur--;
}
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;
prvans.push_back(i);
}
}
sort(prvans.begin(), prvans.end(), [](const int &a, const int &b)
{
return st[a] < st[b];
});
Add(1);
cout << (count(deg + 1, deg + n + 1, 1) + 1) / 2;
}
Compilation message (stderr)
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 'void Add(long long int)':
mergers.cpp:53:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(++cur < prvans.size() && st[prvans[cur]] <= en[x])
~~~~~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int32_t main()':
mergers.cpp:66: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:66:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |