Submission #555360

# Submission time Handle Problem Language Result Execution time Memory
555360 2022-04-30T15:35:04 Z urosk Mergers (JOI19_mergers) C++14
10 / 100
3000 ms 262144 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;
ll a[maxn];
ll dp[maxn][2];
ll b[maxn];
ll in[maxn];
ll out[maxn];
vector<ll> g[maxn];
vector<ll> v[maxn];
vector<ll> f[maxn];
ll ti = 1;
ll col = -1;
ll con[maxn];
ll dsu[maxn];
ll pari[maxn];
ll root(ll x){
    while(x!=dsu[x]){
        dsu[x] = dsu[dsu[x]];
        x = dsu[x];
    }
    return x;
}
void upd(ll x,ll y){
    x = root(x); y = root(y);
    dsu[x] = y;
}
ll root2(ll x){
    while(x!=con[x]){
        con[x] = con[con[x]];
        x = con[x];
    }
    return x;
}
void upd2(ll x,ll y){
    x = root2(x); y = root2(y);
    con[x] = y;
}
void updg(ll x,ll y){
    upd2(x,y);
    f[x].pb(y);
    f[y].pb(x);
}
vector<vector<ll> > cnt;
set<ll> st[maxn];
set<ll> up[maxn];
set<ll> mrg(set<ll> a,set<ll> b){
    if(sz(a)<sz(b)){
        for(ll x : a) b.insert(x);
        return b;
    }
    for(ll x : b) a.insert(x);
    return a;
}
void dfs(ll u,ll par){
    st[u].insert(b[u]);
    pari[u] = par;
    for(ll s : g[u]){
        if(s==par) continue;
        dfs(s,u);
        st[u] = mrg(st[u],st[s]);
    }
}
void dfs2(ll u,ll par){
    if(u!=par){
        up[u] = up[par];
        up[u].insert(b[par]);
        for(ll s : g[par]){
            if(s==pari[par]) continue;
            if(s==u) continue;
            up[u] = mrg(up[u],st[s]);
        }
        /*
        here;
        cerr<<"u: "<<u<<" "<<par<<endl;
        for(ll x : st[u]) cerr<<x<< " ";
        cerr<<endl;
        for(ll x : up[u]) cerr<<x<< " ";
        cerr<<endl;
        */
        if(sz(up[u])+sz(st[u])!=k) upd(par,u);
    }
    for(ll s : g[u]){
        if(s==par) continue;
        dfs2(s,u);
    }
}
ll dis[maxn];
ll getnaj(ll u,ll par){
    ll y = u;
    for(ll s : f[u]){
        if(s==par) continue;
        dis[s] = dis[u] + 1;
        ll e = getnaj(s,u);
        if(dis[e]>dis[y]) y = e;
    }
    return y;
}
ll sizi = 0;
void brisi(ll u,ll par,ll x,vector<ll> w){
    w.pb(u);
    if(u==x){
        for(ll i = 0;i<sz(w)-1;i++){
            upd(w[i],w[i+1]);
        }
        sizi-=(sz(w)-1);
        return;
    }
    for(ll s : f[u]){
        if(s==par) continue;
        brisi(s,u,x,w);
    }
}
bool vis[maxn];
int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n >> k;
    iota(dsu+1,dsu+n+1,1);
    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 >> b[i];
    for(ll i = 1;i<=n;i++){
        v[b[i]].pb(i);
    }
    dfs(1,1);
    dfs2(1,1);
    for(ll i = 1;i<=n;i++){
        if(vis[root(i)]) continue;
        vis[root(i)] = 1;
        sizi++;
    }
    iota(con+1,con+n+1,1);
    for(ll i = 1;i<=n;i++){
        ll x = root(i);
        for(ll j : g[i]){
            ll y = root(j);
            if(root2(x)==root2(y)) continue;
            updg(x,y);
        }
    }
    //for(ll i= 1;i<=n;i++) cerr<<root(i)<< " ";
    //cerr<<endl;
    ll ans = 0;
    while(sizi>1){
        /*
        here;
        for(ll i = 1;i<=n;i++){
            if(sz(f[i])==0) continue;
            cerr<<"i: "<<i<<endl;
            for(ll x : f[i]) cerr<<x<< " ";
            cerr<<endl;
        }
        */
        ll poc = 1;
        for(ll i = 1;i<=n;i++) if(sz(f[i])>0) poc = i;
        //cerr<<"poc: "<<poc<<endl;
        fill(dis,dis+n+1,0);
        dis[0] = llinf;
        ll x = getnaj(poc,poc);
        //cerr<<x<<endl;

        fill(dis,dis+n+1,0);
        dis[0] = llinf;
        ll y = getnaj(x,x);
        brisi(x,x,y,{});
        //cerr<<y<<endl;
        for(ll i = 1;i<=n;i++) f[i].clear();
        iota(con+1,con+n+1,1);
        for(ll i = 1;i<=n;i++){
            ll x = root(i);
            for(ll j : g[i]){
                ll y = root(j);
                if(root2(x)==root2(y)) continue;
                updg(x,y);
            }
        }
        ans++;
    }
    cout<<ans<<endl;
	return 0;
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4
*/
# Verdict Execution time Memory Grader output
1 Correct 38 ms 82516 KB Output is correct
2 Correct 37 ms 82472 KB Output is correct
3 Correct 43 ms 82508 KB Output is correct
4 Correct 43 ms 82516 KB Output is correct
5 Correct 39 ms 82524 KB Output is correct
6 Correct 40 ms 82508 KB Output is correct
7 Correct 38 ms 82504 KB Output is correct
8 Correct 38 ms 82496 KB Output is correct
9 Correct 39 ms 82476 KB Output is correct
10 Correct 43 ms 82460 KB Output is correct
11 Correct 38 ms 82516 KB Output is correct
12 Correct 37 ms 82508 KB Output is correct
13 Correct 47 ms 82532 KB Output is correct
14 Correct 39 ms 82600 KB Output is correct
15 Correct 40 ms 82516 KB Output is correct
16 Correct 39 ms 82428 KB Output is correct
17 Correct 38 ms 82448 KB Output is correct
18 Correct 38 ms 82496 KB Output is correct
19 Correct 42 ms 82532 KB Output is correct
20 Correct 38 ms 82496 KB Output is correct
21 Correct 40 ms 82564 KB Output is correct
22 Correct 38 ms 82644 KB Output is correct
23 Correct 39 ms 82572 KB Output is correct
24 Correct 41 ms 82512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 82516 KB Output is correct
2 Correct 37 ms 82472 KB Output is correct
3 Correct 43 ms 82508 KB Output is correct
4 Correct 43 ms 82516 KB Output is correct
5 Correct 39 ms 82524 KB Output is correct
6 Correct 40 ms 82508 KB Output is correct
7 Correct 38 ms 82504 KB Output is correct
8 Correct 38 ms 82496 KB Output is correct
9 Correct 39 ms 82476 KB Output is correct
10 Correct 43 ms 82460 KB Output is correct
11 Correct 38 ms 82516 KB Output is correct
12 Correct 37 ms 82508 KB Output is correct
13 Correct 47 ms 82532 KB Output is correct
14 Correct 39 ms 82600 KB Output is correct
15 Correct 40 ms 82516 KB Output is correct
16 Correct 39 ms 82428 KB Output is correct
17 Correct 38 ms 82448 KB Output is correct
18 Correct 38 ms 82496 KB Output is correct
19 Correct 42 ms 82532 KB Output is correct
20 Correct 38 ms 82496 KB Output is correct
21 Correct 40 ms 82564 KB Output is correct
22 Correct 38 ms 82644 KB Output is correct
23 Correct 39 ms 82572 KB Output is correct
24 Correct 41 ms 82512 KB Output is correct
25 Correct 42 ms 82576 KB Output is correct
26 Runtime error 313 ms 262144 KB Execution killed with signal 9
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 82516 KB Output is correct
2 Correct 37 ms 82472 KB Output is correct
3 Correct 43 ms 82508 KB Output is correct
4 Correct 43 ms 82516 KB Output is correct
5 Correct 39 ms 82524 KB Output is correct
6 Correct 40 ms 82508 KB Output is correct
7 Correct 38 ms 82504 KB Output is correct
8 Correct 38 ms 82496 KB Output is correct
9 Correct 39 ms 82476 KB Output is correct
10 Correct 43 ms 82460 KB Output is correct
11 Correct 38 ms 82516 KB Output is correct
12 Correct 37 ms 82508 KB Output is correct
13 Correct 47 ms 82532 KB Output is correct
14 Correct 39 ms 82600 KB Output is correct
15 Correct 40 ms 82516 KB Output is correct
16 Correct 39 ms 82428 KB Output is correct
17 Correct 38 ms 82448 KB Output is correct
18 Correct 38 ms 82496 KB Output is correct
19 Correct 42 ms 82532 KB Output is correct
20 Correct 38 ms 82496 KB Output is correct
21 Correct 40 ms 82564 KB Output is correct
22 Correct 38 ms 82644 KB Output is correct
23 Correct 39 ms 82572 KB Output is correct
24 Correct 41 ms 82512 KB Output is correct
25 Correct 45 ms 82592 KB Output is correct
26 Execution timed out 3086 ms 94680 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3083 ms 94440 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 82516 KB Output is correct
2 Correct 37 ms 82472 KB Output is correct
3 Correct 43 ms 82508 KB Output is correct
4 Correct 43 ms 82516 KB Output is correct
5 Correct 39 ms 82524 KB Output is correct
6 Correct 40 ms 82508 KB Output is correct
7 Correct 38 ms 82504 KB Output is correct
8 Correct 38 ms 82496 KB Output is correct
9 Correct 39 ms 82476 KB Output is correct
10 Correct 43 ms 82460 KB Output is correct
11 Correct 38 ms 82516 KB Output is correct
12 Correct 37 ms 82508 KB Output is correct
13 Correct 47 ms 82532 KB Output is correct
14 Correct 39 ms 82600 KB Output is correct
15 Correct 40 ms 82516 KB Output is correct
16 Correct 39 ms 82428 KB Output is correct
17 Correct 38 ms 82448 KB Output is correct
18 Correct 38 ms 82496 KB Output is correct
19 Correct 42 ms 82532 KB Output is correct
20 Correct 38 ms 82496 KB Output is correct
21 Correct 40 ms 82564 KB Output is correct
22 Correct 38 ms 82644 KB Output is correct
23 Correct 39 ms 82572 KB Output is correct
24 Correct 41 ms 82512 KB Output is correct
25 Correct 42 ms 82576 KB Output is correct
26 Runtime error 313 ms 262144 KB Execution killed with signal 9
27 Halted 0 ms 0 KB -