Submission #563828

# Submission time Handle Problem Language Result Execution time Memory
563828 2022-05-18T07:20:56 Z dantoh000 Mergers (JOI19_mergers) C++14
0 / 100
99 ms 41360 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(x,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+1)/2);
}

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]);
      |         ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31572 KB Output is correct
2 Correct 17 ms 31540 KB Output is correct
3 Incorrect 20 ms 31604 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31572 KB Output is correct
2 Correct 17 ms 31540 KB Output is correct
3 Incorrect 20 ms 31604 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31572 KB Output is correct
2 Correct 17 ms 31540 KB Output is correct
3 Incorrect 20 ms 31604 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 79 ms 37956 KB Output is correct
2 Correct 99 ms 41360 KB Output is correct
3 Incorrect 19 ms 31844 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31572 KB Output is correct
2 Correct 17 ms 31540 KB Output is correct
3 Incorrect 20 ms 31604 KB Output isn't correct
4 Halted 0 ms 0 KB -