이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb emplace_back
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }
const int MAXN = 500000;
int n, k;
int a, b;
vector <int> path[MAXN+1];
int clr[MAXN+1];
int cnt_clr[MAXN+1];
unordered_map <int,int> cnt[MAXN+1];
int full[MAXN+1];
int dsu[MAXN+1];
vector <int> G[MAXN+1];
int leaves;
int fnd(int a) {
return a == dsu[a] ? a : dsu[a] = fnd(dsu[a]);
}
void Merge(int a, int b) {
a = fnd(a), b = fnd(b);
dsu[a] = b;
}
void DFS(int now, int par) {
cnt[now][clr[now]]++;
if (cnt_clr[clr[now]] == 1)
full[now]++;
for (int i : path[now]) {
if (i == par) continue;
DFS(i, now);
if (cnt[i].size() > cnt[now].size())
swap(cnt[now], cnt[i]);
for (auto [v, c] : cnt[i]) {
cnt[now][v] += c;
if (cnt[now][v] == cnt_clr[v])
full[now]++;
}
}
if (full[now] != cnt[now].size())
Merge(now, par);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 1; i < n; i++) {
cin >> a >> b;
path[a].pb(b);
path[b].pb(a);
}
for (int i = 1; i <= n; i++) {
cin >> a;
clr[i] = a;
cnt_clr[a]++;
dsu[i] = i;
}
DFS(1, 0);
for (int i = 1; i <= n; i++) {
for (int j : path[i]) {
if (fnd(i) != fnd(j)) {
G[fnd(i)].pb(fnd(j));
}
}
}
for (int i = 1; i <= n; i++) {
if (!G[i].size()) continue;
bool flag = true;
for (int j : path[i]) {
flag &= (j == path[i][0]);
}
if (flag)
leaves++;
}
cout << (leaves+1)/2 << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
mergers.cpp: In function 'void DFS(int, int)':
mergers.cpp:39:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | for (auto [v, c] : cnt[i]) {
| ^
mergers.cpp:45:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::unordered_map<int, int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if (full[now] != cnt[now].size())
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |