Submission #623096

# Submission time Handle Problem Language Result Execution time Memory
623096 2022-08-05T07:34:41 Z radal Mergers (JOI19_mergers) C++17
10 / 100
3000 ms 41216 KB
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 5e5+10,mod = 998244353,inf = 1e9+10,sq = 700;
inline int mkay(int a,int b){
    if (a+b >= mod) return a+b-mod;
    if (a+b < 0) return a+b+mod;
    return a+b;
}
 
inline int poww(int a,int k){
    if (k < 0) return 0;
    int z = 1;
    while (k){
        if (k&1) z = 1ll*z*a%mod;
        a = 1ll*a*a%mod;
        k /= 2;
    } 
    return z; 
}
vector<int> adj[N],col[N];
int par[N][20],h[N],T,tin[N],calc[N];

void pre(int v,int p){
    par[v][0] = p;
    tin[v] = T++;
    for (int u : adj[v]){
        if (u == p) continue;
        h[u] = h[v]+1;
        pre(u,v);
    }
}
bool cmp(int u,int v){
    return (tin[u] < tin[v]);
}

int lca(int u,int v){
    if (h[u] < h[v]) swap(u,v);
    repr(i,19,0){
        if ((1 << i) <= h[u]-h[v]) u = par[u][i];
    }
    if (u == v) return u;
    repr(i,19,0){
        if (par[u][i] != par[v][i]){
            v = par[v][i];
            u = par[u][i];
        }
    }
    return par[v][0];
}
void dfs(int v,int p){
    for (int u : adj[v]){
        if (u != p){
            dfs(u,v);
            calc[v] += calc[u];
        }
    }
}
int main(){
    ios :: sync_with_stdio(0); cin.tie(0);
    int n,k;
    cin >> n >> k;
    rep(i,1,n){
        int u,v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    rep(i,1,n+1){
        int c;
        cin >> c;
        col[c].pb(i);
    }
    rep(i,1,k+1)  sort(all(col[i]),cmp);
    pre(1,0);
    rep(j,1,20){
        rep(i,2,n+1)
            par[i][j] = par[par[i][j-1]][j-1];
    }
    rep(i,1,k+1){
        int sz = col[i].size();
        if (sz < 2) continue;
        rep(j,1,sz){
            calc[col[i][j]]++;
            calc[col[i][j-1]]++;
            calc[lca(col[i][j],col[i][j-1])] -= 2;
        }
    }
    dfs(1,0);
    int cnt = 0,ans = 0;
    rep(i,2,n+1) cnt += (calc[i] == 0);
    while (cnt){
        ans++;
        int mi = inf,x = -1,y = -1;
        rep(i,1,k){
            if (col[i].empty()) continue;
            rep(j,i+1,k+1){
                if (col[j].empty()) continue;
                rep(f,1,n+1) calc[f] = 0;
                rep(f,1,k+1){
                    if (f == j || f == i) continue;
                    int sz = col[f].size();
                    if (sz < 2) continue;
                    rep(g,1,sz){
                        calc[col[f][g]]++;
                        calc[col[f][g-1]]++;
                        calc[lca(col[f][g],col[f][g-1])] -= 2;
                    }
                }
                vector<int> tmp;
                tmp.resize(col[i].size()+col[j].size());
                merge(all(col[i]),all(col[j]),tmp.begin(),cmp);
                int sz = tmp.size();
                rep(f,1,sz){
                    calc[tmp[f]]++;
                    calc[tmp[f-1]]++;
                    calc[lca(tmp[f],tmp[f-1])] -= 2;
                }
                dfs(1,0);
                int fuck = 0;
                rep(i,2,n+1) fuck += (calc[i] == 0);
                if (fuck < mi){
                    mi = fuck;
                    x = i;
                    y = j;
                }
            }
        }
        int i = x,j = y;
        rep(f,1,n+1) calc[f] = 0;
        rep(f,1,k+1){
            if (f == j || f == i) continue;
            int sz = col[f].size();
            if (sz < 2) continue;
            rep(g,1,sz){
                calc[col[f][g]]++;
                calc[col[f][g-1]]++;
                calc[lca(col[f][g],col[f][g-1])] -= 2;
            }
        }
        vector<int> tmp;
        tmp.resize(col[i].size()+col[j].size());
        merge(all(col[i]),all(col[j]),tmp.begin(),cmp);
        int sz = tmp.size();
        rep(f,1,sz){
            calc[tmp[f]]++;
            calc[tmp[f-1]]++;
            calc[lca(tmp[f],tmp[f-1])] -= 2;
        }
        col[i] = tmp;
        col[j].clear();
        dfs(1,0);
        cnt = 0;
        rep(i,2,n+1) cnt += (calc[i] == 0);
    }
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23748 KB Output is correct
2 Correct 13 ms 23812 KB Output is correct
3 Correct 12 ms 23820 KB Output is correct
4 Correct 12 ms 23804 KB Output is correct
5 Correct 15 ms 23724 KB Output is correct
6 Correct 14 ms 23828 KB Output is correct
7 Correct 13 ms 23836 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 13 ms 23820 KB Output is correct
11 Correct 12 ms 23820 KB Output is correct
12 Correct 13 ms 23848 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23812 KB Output is correct
15 Correct 14 ms 23776 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 13 ms 23812 KB Output is correct
18 Correct 13 ms 23812 KB Output is correct
19 Correct 13 ms 23732 KB Output is correct
20 Correct 14 ms 23892 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 12 ms 23760 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 14 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23748 KB Output is correct
2 Correct 13 ms 23812 KB Output is correct
3 Correct 12 ms 23820 KB Output is correct
4 Correct 12 ms 23804 KB Output is correct
5 Correct 15 ms 23724 KB Output is correct
6 Correct 14 ms 23828 KB Output is correct
7 Correct 13 ms 23836 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 13 ms 23820 KB Output is correct
11 Correct 12 ms 23820 KB Output is correct
12 Correct 13 ms 23848 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23812 KB Output is correct
15 Correct 14 ms 23776 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 13 ms 23812 KB Output is correct
18 Correct 13 ms 23812 KB Output is correct
19 Correct 13 ms 23732 KB Output is correct
20 Correct 14 ms 23892 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 12 ms 23760 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 14 ms 23764 KB Output is correct
25 Correct 28 ms 23792 KB Output is correct
26 Execution timed out 3090 ms 24208 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23748 KB Output is correct
2 Correct 13 ms 23812 KB Output is correct
3 Correct 12 ms 23820 KB Output is correct
4 Correct 12 ms 23804 KB Output is correct
5 Correct 15 ms 23724 KB Output is correct
6 Correct 14 ms 23828 KB Output is correct
7 Correct 13 ms 23836 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 13 ms 23820 KB Output is correct
11 Correct 12 ms 23820 KB Output is correct
12 Correct 13 ms 23848 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23812 KB Output is correct
15 Correct 14 ms 23776 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 13 ms 23812 KB Output is correct
18 Correct 13 ms 23812 KB Output is correct
19 Correct 13 ms 23732 KB Output is correct
20 Correct 14 ms 23892 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 12 ms 23760 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 14 ms 23764 KB Output is correct
25 Correct 23 ms 23844 KB Output is correct
26 Correct 70 ms 38432 KB Output is correct
27 Correct 116 ms 38024 KB Output is correct
28 Execution timed out 3049 ms 24260 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 69 ms 38332 KB Output is correct
2 Execution timed out 3083 ms 41216 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23748 KB Output is correct
2 Correct 13 ms 23812 KB Output is correct
3 Correct 12 ms 23820 KB Output is correct
4 Correct 12 ms 23804 KB Output is correct
5 Correct 15 ms 23724 KB Output is correct
6 Correct 14 ms 23828 KB Output is correct
7 Correct 13 ms 23836 KB Output is correct
8 Correct 15 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 13 ms 23820 KB Output is correct
11 Correct 12 ms 23820 KB Output is correct
12 Correct 13 ms 23848 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 13 ms 23812 KB Output is correct
15 Correct 14 ms 23776 KB Output is correct
16 Correct 13 ms 23764 KB Output is correct
17 Correct 13 ms 23812 KB Output is correct
18 Correct 13 ms 23812 KB Output is correct
19 Correct 13 ms 23732 KB Output is correct
20 Correct 14 ms 23892 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 12 ms 23760 KB Output is correct
23 Correct 13 ms 23764 KB Output is correct
24 Correct 14 ms 23764 KB Output is correct
25 Correct 28 ms 23792 KB Output is correct
26 Execution timed out 3090 ms 24208 KB Time limit exceeded
27 Halted 0 ms 0 KB -