답안 #757194

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
757194 2023-06-12T19:00:55 Z sheldon Mergers (JOI19_mergers) C++14
0 / 100
122 ms 50576 KB
#include <bits/stdc++.h>

using namespace std;

const int nax = 5e5 + 5;

vector<int> edges[nax];
int state[nax], cnt[nax], sz[nax];

struct block {
    int have = 0;
    map<int, int> mp;

};
block bla[nax];

void merge (block &a, block &b) {
    if (a.mp.size() > b.mp.size()) {
        swap(a, b);
    }
    for (auto &it : a.mp) {
        b.mp[it.first] += it.second;
        if (b.mp[it.first] == cnt[it.first]) {
            ++b.have;
        }
    }
    a.mp.clear();
}
int ans = 0;
void dfs (int u, int p) {
    bla[u].mp[state[u]]++;
    if (cnt[state[u]] == 1) ++bla[u].have;
    for (int v : edges[u]) {
        if (v != p) {
            dfs (v, u);
            sz[u] += sz[v];
            merge(bla[v], bla[u]);
        }
    }
    if (bla[u].mp.size() == bla[u].have) {
        
        sz[u]++;
    }
    if (sz[u] == 1) ++ans;
}
void solve() {
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    for (int i = 1; i <= n; ++i) {
        cin >> state[i];
        cnt[state[i]]++;
    }
    dfs (1, 0);
    if (sz[1] == 1) {
        cout << "0\n";
    } else {
        cout << (ans + 1) / 2 << '\n';
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
}

Compilation message

mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:40:26: warning: comparison of integer expressions of different signedness: 'std::map<int, int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   40 |     if (bla[u].mp.size() == bla[u].have) {
      |         ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39380 KB Output is correct
2 Incorrect 20 ms 39508 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39380 KB Output is correct
2 Incorrect 20 ms 39508 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39380 KB Output is correct
2 Incorrect 20 ms 39508 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 44820 KB Output is correct
2 Incorrect 122 ms 50576 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 39380 KB Output is correct
2 Incorrect 20 ms 39508 KB Output isn't correct
3 Halted 0 ms 0 KB -