답안 #563819

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563819 2022-05-18T07:16:49 Z dantoh000 Mergers (JOI19_mergers) C++14
0 / 100
143 ms 41416 KB
#include <bits/stdc++.h>
using namespace std;
int n,k;
vector<int> G[1000005];
int c[1000005];
int low[1000005];
int num[1000005];
int p[1000005];
int sz[1000005];
int ct = 0;

int pa[500005];
int eg[500005];
int rk[500005];
void init(int n){
    for (int i = 1; i <= n; i++){
        pa[i] = i; rk[i] = 0;
        eg[i] = 0;
    }
}
int fin(int x){
    return pa[x] == x ? x : p[x] = fin(p[x]);
}
void un(int x, int y){
    x = fin(x), y = fin(y);
    if (x == y) return;
    if (rk[x] < rk[y]) swap(rk[x], rk[y]);
    pa[y] = x;
    eg[x] += eg[y];
    if (rk[x] == rk[y]) rk[x]++;
}
void dfs(int u){
    num[u] = low[u] = ct++;
    sz[u] = 1;
    for (auto v : G[u]){
        if (num[v] == -1){
            p[v] = u;
            //printf("%d -> %d\n",u,v);
            dfs(v);
            if (u <= n && v <= n){
                if (low[v] <= num[u]){
                    un(u,v);
                }
                else{
                    eg[fin(u)]++;
                    eg[fin(v)]++;
                }
                //printf("%d %d\n",u,v);
            }
            low[u] = min(low[v], low[u]);
        }
        else if (v != p[u]){
            low[u] = min(num[v], low[u]);
        }
    }
}

int main(){
    scanf("%d%d",&n,&k);

    for (int i = 1; i < n; i++){
        int a,b;
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for (int i = 1; i <= n; i++){
        scanf("%d",&c[i]);
        G[i].push_back(n+c[i]);
        G[n+c[i]].push_back(i);
    }
    memset(low,-1,sizeof(low));
    memset(num,-1,sizeof(num));
    init(n);
    dfs(1);
    int ans = 0;
    for (int i = 1; i <= n; i++){
        if (i == pa[i] && eg[i] == 1) ans++;
    }
    printf("%d",ans);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |     scanf("%d%d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~
mergers.cpp:63:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         scanf("%d%d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~
mergers.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         scanf("%d",&c[i]);
      |         ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 31572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 31572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 31572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 37968 KB Output is correct
2 Incorrect 143 ms 41416 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 31572 KB Output isn't correct
2 Halted 0 ms 0 KB -