# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
154943 | Mercenary | Mergers (JOI19_mergers) | C++14 | 1097 ms | 191608 KiB |
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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 5e5 + 5;
int n , k , col[maxn];
int total[maxn];
int ans = 0;
vector<int> adj[maxn];
int cnt[maxn];
gp_hash_table<int,int> *s[maxn];
int res[maxn];
int cut[maxn];
int sub[maxn];
void DFS(int u , int par){
int big = -1;
sub[u] = 1;
int have = 0;
for(int c: adj[u]){
if(c != par){
DFS(c , u);
sub[u] += sub[c];
cut[u] += cut[c];
if(big == -1 || sub[big] < sub[c])big = c;
}
}
ans += (u == 1 && cut[u] == 1);
if(u == 1)return;
if(big == -1){
s[u] = new gp_hash_table<int,int>();
}else{
s[u] = s[big];
res[u] = res[big];
}
auto add = [&](int col , int num){
(*s[u])[col] += num;
res[u] += (*s[u])[col] == total[col];
};
add(col[u] , 1);
for(int c : adj[u]){
if(c != par && c != big){
for(auto d : (*s[c])){
add(d.first , d.second);
}
}
}
if(cut[u] == 0 && (s[u]->size() == res[u]))ans++;
if((s[u]->size() == res[u]))cut[u] = 1;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
cin >> n >> k;
for(int i = 1 ; i < n ; ++i){
int u , v;cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i = 1 ; i <= n ; ++i){
cin >> col[i];
total[col[i]]++;
}
DFS(1 , 0);
// cout << ans;
cout << (ans + 1) / 2;
}
Compilation message (stderr)
# | 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... |