Submission #623113

# Submission time Handle Problem Language Result Execution time Memory
623113 2022-08-05T08:17:51 Z radal Mergers (JOI19_mergers) C++17
0 / 100
74 ms 39860 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],dp[N];
int odd[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];
            if (!calc[u] && !dp[u]){
                dp[v]++;
                odd[v]++;
            }
            else{
                dp[v] += dp[u];
                odd[v] += odd[u];
            }
        }
    }
    dp[v] -= odd[v]/2;
    odd[v] %= 2;
}
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);
    cout << dp[1];
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 15 ms 23744 KB Output is correct
3 Correct 12 ms 23864 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23852 KB Output is correct
6 Correct 13 ms 23856 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23792 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 13 ms 23808 KB Output is correct
13 Correct 13 ms 23840 KB Output is correct
14 Correct 12 ms 23788 KB Output is correct
15 Correct 13 ms 23856 KB Output is correct
16 Incorrect 13 ms 23740 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 15 ms 23744 KB Output is correct
3 Correct 12 ms 23864 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23852 KB Output is correct
6 Correct 13 ms 23856 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23792 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 13 ms 23808 KB Output is correct
13 Correct 13 ms 23840 KB Output is correct
14 Correct 12 ms 23788 KB Output is correct
15 Correct 13 ms 23856 KB Output is correct
16 Incorrect 13 ms 23740 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 15 ms 23744 KB Output is correct
3 Correct 12 ms 23864 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23852 KB Output is correct
6 Correct 13 ms 23856 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23792 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 13 ms 23808 KB Output is correct
13 Correct 13 ms 23840 KB Output is correct
14 Correct 12 ms 23788 KB Output is correct
15 Correct 13 ms 23856 KB Output is correct
16 Incorrect 13 ms 23740 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 74 ms 37696 KB Output is correct
2 Incorrect 69 ms 39860 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 15 ms 23744 KB Output is correct
3 Correct 12 ms 23864 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23852 KB Output is correct
6 Correct 13 ms 23856 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23764 KB Output is correct
10 Correct 12 ms 23792 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 13 ms 23808 KB Output is correct
13 Correct 13 ms 23840 KB Output is correct
14 Correct 12 ms 23788 KB Output is correct
15 Correct 13 ms 23856 KB Output is correct
16 Incorrect 13 ms 23740 KB Output isn't correct
17 Halted 0 ms 0 KB -