답안 #828649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
828649 2023-08-17T13:11:19 Z Ronin13 Mergers (JOI19_mergers) C++17
0 / 100
105 ms 34364 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 5e5 + 1;
 
 
int in[nmax], out[nmax];
int timer = 0;
int p[nmax][22];
vector <vector <int> > g(nmax);
void dfs(int v, int e){
    in[v] = timer++;
    p[v][0] = e;
    for(int j = 1; j <= 20; j++)
        p[v][j] = p[p[v][j - 1]][j - 1];
    for(int to : g[v]){
        if(to == e) continue;
        dfs(to, v);
    }
    out[v] = timer++;
}

bool isancestor(int u, int v){
    return in[u] <= in[v] && out[u] >= out[v];
}

int lca(int u, int v){
    if(isancestor(u, v)) return u;
    if(isancestor(v, u)) return v;
    for(int j = 20; j >= 0; j--){
        if(!isancestor(p[u][j], v)) u = p[u][j];
    }
    return p[u][0];
}

vector <int> sz(nmax);
vector <int> a(nmax);
int cnt = 0;
void DFS(int v, int e){
    sz[v] = 1;
    for(int to : g[v]){
        if(to == e) continue;
        DFS(to, v);
        sz[v] += sz[to];
    }
    sz[v] -= a[v];
    if(sz[v] == 0) cnt++;
}

int ansdfs(int v, int e){
    int sum = 0;
    for(int to : g[v]){
        if(to == e) continue;
        sum += ansdfs(to, v);
    }
    if(sz[v]) return sum;
    else{
        return max(sum, 1);
    }
}

int main(){
  //  ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    int k; cin >> k;
    vector <vector <int> > vec(k + 1);
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
    }
    dfs(1, 1);
    for(int i = 1; i <= n; ++i){
        int x; cin >> x;
        vec[x].pb(i);
    }
    
    for(int i = 1; i <= k; i++){
        if(vec[i].empty()) continue;
        int lc = vec[i][0];
        for(int to : vec[i]) lc = lca(lc, to);
        a[lc] += vec[i].size();
    }
    DFS(1, 1);
    if(cnt == 1) cout << 0 << "\n";
    else
    cout << (ansdfs(1, 1) + 1) / 2 << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 8 ms 16000 KB Output is correct
3 Correct 8 ms 15956 KB Output is correct
4 Correct 7 ms 15956 KB Output is correct
5 Correct 8 ms 15916 KB Output is correct
6 Correct 8 ms 15956 KB Output is correct
7 Correct 7 ms 15956 KB Output is correct
8 Correct 8 ms 15956 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 7 ms 15956 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 7 ms 15956 KB Output is correct
13 Correct 7 ms 15956 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 8 ms 15956 KB Output is correct
16 Incorrect 7 ms 15956 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 8 ms 16000 KB Output is correct
3 Correct 8 ms 15956 KB Output is correct
4 Correct 7 ms 15956 KB Output is correct
5 Correct 8 ms 15916 KB Output is correct
6 Correct 8 ms 15956 KB Output is correct
7 Correct 7 ms 15956 KB Output is correct
8 Correct 8 ms 15956 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 7 ms 15956 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 7 ms 15956 KB Output is correct
13 Correct 7 ms 15956 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 8 ms 15956 KB Output is correct
16 Incorrect 7 ms 15956 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 8 ms 16000 KB Output is correct
3 Correct 8 ms 15956 KB Output is correct
4 Correct 7 ms 15956 KB Output is correct
5 Correct 8 ms 15916 KB Output is correct
6 Correct 8 ms 15956 KB Output is correct
7 Correct 7 ms 15956 KB Output is correct
8 Correct 8 ms 15956 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 7 ms 15956 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 7 ms 15956 KB Output is correct
13 Correct 7 ms 15956 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 8 ms 15956 KB Output is correct
16 Incorrect 7 ms 15956 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 29464 KB Output is correct
2 Incorrect 105 ms 34364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 8 ms 16000 KB Output is correct
3 Correct 8 ms 15956 KB Output is correct
4 Correct 7 ms 15956 KB Output is correct
5 Correct 8 ms 15916 KB Output is correct
6 Correct 8 ms 15956 KB Output is correct
7 Correct 7 ms 15956 KB Output is correct
8 Correct 8 ms 15956 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 7 ms 15956 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 7 ms 15956 KB Output is correct
13 Correct 7 ms 15956 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 8 ms 15956 KB Output is correct
16 Incorrect 7 ms 15956 KB Output isn't correct
17 Halted 0 ms 0 KB -