답안 #569973

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
569973 2022-05-28T11:50:10 Z Doxeno Mergers (JOI19_mergers) C++17
0 / 100
154 ms 74204 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500010;
vector<int> adj[MAXN];
vector<int> col[MAXN];
int c[MAXN];
int d[MAXN];
int st[MAXN][20];
bool vis[MAXN];
int mc[MAXN];
int og[MAXN];
int ans;
void dd(int n){
    vis[n] = 1;
    for(auto x: adj[n]){
        if(vis[x])continue;
        st[x][0] = n;
        d[x] = d[n]+1;
        dd(x);
    }
    for(int i = 1; i < 20; i++){
        st[n][i] = st[st[n][i-1]][i-1];
    }
}

int lca(int a, int b){
    if(d[a] < d[b])swap(a,b);
    for(int i = 0; i < 20; i++){
        if((d[a]-d[b])&(1<<i))a = st[a][i];
    }
    for(int i = 19; i >= 0; i--){
        if(st[a][i] != st[b][i]){
            a = st[a][i];
            b = st[b][i];
        }
    }
    return st[a][0];
}

int dfs(int n){
    vis[n] = 1;
    int k = 0;
    og[n] = mc[c[n]];
    for(auto x: adj[n]){
        if(vis[x])continue;
        if(dfs(x))k++;
        og[n] = min(og[n],og[x]);
    }
    if(og[n] == d[n])ans++;
    //ans+=k;
    //return(((n!=0) && (og[n] == d[n])) || (k&1));
}

int main(){
    int N,K,a,b;
    cin >> N >> K;
    for(int i = 0; i < N-1; i++){
        cin >> a >> b;
        adj[--a].push_back(--b);
        adj[b].push_back(a);
    }
    for(int i = 0; i < N; i++){
        cin >> c[i]; c[i]--;
        col[c[i]].push_back(i);
    }
    dd(0);
    for(int i = 0; i < N; i++)vis[i] = 0;
    for(int i = 0; i < K; i++){
        if(col[i].empty())continue;
        mc[i] = col[i][0];
        for(int  j = 1; j < col[i].size(); j++){
            mc[i] = lca(mc[i],col[i][j]);
        }
        mc[i] = d[mc[i]];
    }
    dfs(0);
    cout << ans/2 << endl;
}

Compilation message

mergers.cpp: In function 'int dfs(int)':
mergers.cpp:52:1: warning: no return statement in function returning non-void [-Wreturn-type]
   52 | }
      | ^
mergers.cpp: In function 'int main()':
mergers.cpp:71:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |         for(int  j = 1; j < col[i].size(); j++){
      |                         ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 37 ms 48192 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 37 ms 48192 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 37 ms 48192 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 154 ms 74204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 37 ms 48192 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -