답안 #391433

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391433 2021-04-18T18:06:58 Z qwerasdfzxcl Mergers (JOI19_mergers) C++14
0 / 100
127 ms 24800 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<int> adj[500500];
int col[500500], cnt[500500], val=0, val2=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, bool test){
    int ret=0;
    if (chk[s]) ret++;
    if (chk[s] && s!=1) test=1, val2++;
    visited2[s] = 1;
    for (auto &v:adj[s]) if (!visited2[v]){
        ret += dfs2(v, test);
    }
    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, 0);
    /*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");*/
    if (val2==1) val++;
    printf("%d\n", (val+1)/2);
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   42 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   45 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   50 |         scanf("%d", col+i);
      |         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12020 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12080 KB Output is correct
4 Correct 8 ms 11980 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 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 10 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 9 ms 11980 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 8 ms 11980 KB Output is correct
15 Correct 9 ms 12108 KB Output is correct
16 Incorrect 8 ms 12044 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12020 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12080 KB Output is correct
4 Correct 8 ms 11980 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 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 10 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 9 ms 11980 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 8 ms 11980 KB Output is correct
15 Correct 9 ms 12108 KB Output is correct
16 Incorrect 8 ms 12044 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12020 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12080 KB Output is correct
4 Correct 8 ms 11980 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 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 10 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 9 ms 11980 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 8 ms 11980 KB Output is correct
15 Correct 9 ms 12108 KB Output is correct
16 Incorrect 8 ms 12044 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 16184 KB Output is correct
2 Incorrect 127 ms 24800 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12020 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12080 KB Output is correct
4 Correct 8 ms 11980 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 11980 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 10 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 9 ms 11980 KB Output is correct
13 Correct 8 ms 11980 KB Output is correct
14 Correct 8 ms 11980 KB Output is correct
15 Correct 9 ms 12108 KB Output is correct
16 Incorrect 8 ms 12044 KB Output isn't correct
17 Halted 0 ms 0 KB -