답안 #563778

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563778 2022-05-18T06:17:40 Z dantoh000 Mergers (JOI19_mergers) C++14
0 / 100
149 ms 50336 KB
#include <bits/stdc++.h>
using namespace std;
int n,k;
vector<int> G[500005];
int c[500005];
int ct[500005];
int p[500005];
int sz[500005];
int sz2[500005];
set<int> s[500005];
void merg(int u, int v){
    if (s[u].size() < s[v].size()) swap(s[u], s[v]);
    for (auto x : s[v]){
        if (s[u].find(x) == s[u].end()){
            sz2[u] += ct[x];
            s[u].insert(x);
        }
    }
}
int ans = 0;
void dfs(int u){
    sz[u] = 1;
    for (auto v : G[u]){
        if (v == p[u]) continue;
        p[v] = u;
        dfs(v);
        sz[u] += sz[v];
        merg(u,v);
    }
    if (sz[u] == sz2[u]) ans++;
    //printf("%d %d\n",sz[u], sz2[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]);

        ct[c[i]]++;
    }
    for (int i = 1; i <= n; i++){
        s[i].insert(c[i]);
        sz2[i] = ct[c[i]];
    }
    for (int i = 1; i <= n; i++){
        if (G[i].size() == 1){
            dfs(i);
            break;
        }
    }
    if (ans == 0) printf("0");
    else printf("%d",max(ans-1,1));
}

Compilation message

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