Submission #377949

#TimeUsernameProblemLanguageResultExecution timeMemory
3779492qbingxuanMergers (JOI19_mergers)C++14
100 / 100
1428 ms126600 KiB
#include <bits/stdc++.h>
#ifdef local
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
    int cnt = sizeof...(T);
    ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : "\033[0m)\n")));
}
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define sort_uni(v) sort(all(v)), v.erase(unique(all(v)), v.end())

using namespace std;
using ll = int64_t;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1e18;
const int maxn = 500025, inf = 1e9;

struct Dsu {
    vector<int> pa, rk;
    Dsu(int n) : pa(n), rk(n) { iota(all(pa), 0); }
    int anc(int x) { return x==pa[x] ? x : pa[x] = anc(pa[x]); }
    bool join(int x, int y) {
        if ((x=anc(x)) == (y=anc(y))) return false;
        if (rk[x] < rk[y]) swap(x, y);
        return pa[y] = x, rk[x] != rk[y] || ++rk[x];
    }
} dsu(maxn);

vector<int> g[maxn], adj[maxn];
int color[maxn];
int cnt[maxn];
map<int,int> mp[maxn];
void join(map<int,int> &a, map<int,int> &b) {
    if (a.size() < b.size()) a.swap(b);
    for (auto [x, y]: b) {
        a[x] += y;
        if (a[x] == cnt[x]) a.erase(x);
    }
    b.clear();
}
void dfs(int i, int p) {
    for (int j: g[i]) {
        if (j == p) continue;
        dfs(j, i);
        join(mp[i], mp[j]);
    }
    mp[i][color[i]] += 1;
    if (mp[i][color[i]] == cnt[color[i]])
        mp[i].erase(color[i]);
    debug(i);
    for (auto [a, b]: mp[i])
        debug(a, b);
    if (!mp[i].empty())
        dsu.join(i, p);
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    for (int i = 1; i <= n; i++) cin >> color[i], ++cnt[color[i]];
    dfs(1, 0);

    vector<int> deg(n + 1);
    for (int i = 1; i <= n; i++) {
        for (int j: g[i]) {
            int a = dsu.anc(i);
            int b = dsu.anc(j);
            if (a != b)
                adj[a].push_back(b);
        }
    }
    for (int i = 1; i <= n; i++) sort_uni(adj[i]);
    int leaf = 0;
    for (int i = 1; i <= n; i++) {
        int deg = adj[i].size();
        debug(deg);
        if (deg == 1) ++leaf;
    }
    debug(leaf);
    cout << (leaf + 1) / 2 << '\n';
}

Compilation message (stderr)

mergers.cpp: In function 'void join(std::map<int, int>&, std::map<int, int>&)':
mergers.cpp:40:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   40 |     for (auto [x, y]: b) {
      |               ^
mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:56:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |     for (auto [a, b]: mp[i])
      |               ^
mergers.cpp:56:15: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
   56 |     for (auto [a, b]: mp[i])
      |               ^~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...