# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
564818 |
2022-05-19T18:01:09 Z |
urosk |
Mergers (JOI19_mergers) |
C++14 |
|
1447 ms |
26632 KB |
// __builtin_popcount(x)
// __builtin_popcountll(x)
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;
#define maxn 500005
ll n,k;
vector<ll> g[maxn];
vector<ll> v;
ll a[maxn];
ll in[maxn];
ll out[maxn];
ll dp[maxn][3];
ll b[maxn][3];
ll it = 1;
void dfs(ll u,ll par){
in[u] = it++;
for(ll s : g[u]){
if(s==par) continue;
dfs(s,u);
}
out[u] = it-1;
}
bool intree(ll x,ll i){
return x>=in[i]&&out[i]>=x;
}
bool bio[maxn];
ll ans = 0;
void dfs2(ll u,ll par){
dp[u][1] = b[a[u]][1];
dp[u][2] = b[a[u]][2];
for(ll s : g[u]){
if(s==par) continue;
dfs2(s,u);
dp[u][1] = min(dp[u][1],dp[s][1]);
dp[u][2] = max(dp[u][2],dp[s][2]);
bio[u]|=bio[s];
}
bool da = 0;
if(u!=par){
if(intree(dp[u][1],u)&&intree(dp[u][2],u)) da = 1;
}
cerr<<"u: "<<u<<" "<<in[u]<< " "<<out[u]<<" "<<dp[u][1]<< " "<<dp[u][2]<<endl;
if((bio[u]==0)&&(da==1)){
ans++;
}
bio[u]|=da;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> k;
for(ll i = 1;i<=n-1;i++){
ll x,y; cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
for(ll i = 1;i<=n;i++) cin >> a[i];
dfs(1,1);
for(ll i = 1;i<=k;i++){
b[i][1] = llinf;
b[i][2] = 0;
}
for(ll i = 1;i<=n;i++){
ll col = a[i];
b[col][1] = min(b[col][1],in[i]);
b[col][2] = max(b[col][2],in[i]);
}
dfs2(1,1);
ll cnt = 0;
for(ll x : g[1]) if(bio[x]) cnt++;
if(cnt==1) ans++;
ans = (ans+1)/2;
cout<<ans<<endl;
return 0;
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4
5 4
1 2
2 3
3 4
4 5
1
2
3
4
1
5 5
1 2
2 3
3 4
4 5
1
2
3
4
5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
12116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
12116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
12116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1420 ms |
23876 KB |
Output is correct |
2 |
Correct |
1387 ms |
26632 KB |
Output is correct |
3 |
Correct |
45 ms |
12468 KB |
Output is correct |
4 |
Correct |
50 ms |
12428 KB |
Output is correct |
5 |
Correct |
7 ms |
12056 KB |
Output is correct |
6 |
Correct |
8 ms |
12044 KB |
Output is correct |
7 |
Correct |
52 ms |
12456 KB |
Output is correct |
8 |
Correct |
1447 ms |
25592 KB |
Output is correct |
9 |
Correct |
47 ms |
12372 KB |
Output is correct |
10 |
Incorrect |
1419 ms |
24464 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
12116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |