Submission #391422

# Submission time Handle Problem Language Result Execution time Memory
391422 2021-04-18T17:42:13 Z qwerasdfzxcl Mergers (JOI19_mergers) C++14
0 / 100
115 ms 26508 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<int> adj[500500];
int col[500500], cnt[500500], val=0;
bool visited[500500], visited2[500500], chk[500500];

unordered_map<int, int> dfs1(int s){
    unordered_map<int, int> ret;
    ret[col[s]] = 1;
    visited[s]=1;
    for (auto &v:adj[s]) if (!visited[v]){
        auto tmp = dfs1(v);
        for (auto &p:tmp){
            ret[p.first] += p.second;
        }
    }
    chk[s] = 1;
    for (auto &p:ret){
        if (p.second!=cnt[p.first]){
            chk[s] = 0; break;
        }
    }
    return ret;
}

int dfs2(int s){
    int ret=0;
    if (chk[s]) ret++;
    visited2[s] = 1;
    for (auto &v:adj[s]) if (!visited2[v]){
        ret += dfs2(v);
    }
    if (chk[s] && ret==1 && s!=1) val++;
    return ret;
}

int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i=0;i<n-1;i++){
        int a, b;
        scanf("%d %d", &a, &b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (int i=1;i<=n;i++){
        scanf("%d", col+i);
        cnt[col[i]]++;
    }
    dfs1(1);
    dfs2(1);
    /*for (int i=1;i<=n;i++) printf("%d ", col[i]);
    printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", cnt[i]);
    printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", chk[i]);
    printf("\n");*/
    printf("%d\n", (val+1)/2);
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   41 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   44 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   49 |         scanf("%d", col+i);
      |         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 8 ms 12084 KB Output is correct
5 Correct 8 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 12012 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 7 ms 12136 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 8 ms 11980 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 8 ms 12060 KB Output is correct
15 Correct 8 ms 12064 KB Output is correct
16 Incorrect 8 ms 11980 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 8 ms 12084 KB Output is correct
5 Correct 8 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 12012 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 7 ms 12136 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 8 ms 11980 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 8 ms 12060 KB Output is correct
15 Correct 8 ms 12064 KB Output is correct
16 Incorrect 8 ms 11980 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 8 ms 12084 KB Output is correct
5 Correct 8 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 12012 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 7 ms 12136 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 8 ms 11980 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 8 ms 12060 KB Output is correct
15 Correct 8 ms 12064 KB Output is correct
16 Incorrect 8 ms 11980 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 71 ms 16268 KB Output is correct
2 Incorrect 115 ms 26508 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 8 ms 12084 KB Output is correct
5 Correct 8 ms 11980 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 8 ms 11980 KB Output is correct
8 Correct 8 ms 12012 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 7 ms 12136 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 8 ms 11980 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 8 ms 12060 KB Output is correct
15 Correct 8 ms 12064 KB Output is correct
16 Incorrect 8 ms 11980 KB Output isn't correct
17 Halted 0 ms 0 KB -