# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
112593 | Bruteforceman | Mergers (JOI19_mergers) | C++11 | 1398 ms | 126668 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
int city[500010];
int cnt[500010];
vector <int> g[500010];
int P[500010];
int root(int x) {
if(x == P[x]) return P[x];
return P[x] = root(P[x]);
}
void join(int x, int y) {
x = root(x);
y = root(y);
if(x != y) {
P[x] = y;
}
}
void dfs(int x, int par, map <int, int> &s) {
if(cnt[city[x]] > 1) {
s[city[x]] = 1;
}
for(auto i : g[x]) {
if(i - par) {
map <int, int> t;
dfs(i, x, t);
if(t.size() > s.size()) s.swap(t);
for(auto j : t) {
s[j.first] += j.second;
if(cnt[j.first] == s[j.first]) {
s.erase(j.first);
}
}
}
}
if(!s.empty()) {
join(x, par);
}
}
int l[500010], r[500010];
int deg[500010];
int main(int argc, char const *argv[])
{
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i < n; i++) {
int p, q;
scanf("%d %d", &p, &q);
g[p].push_back(q);
g[q].push_back(p);
l[i] = p;
r[i] = q;
}
for(int i = 1; i <= n; i++) {
scanf("%d", &city[i]);
cnt[city[i]] += 1;
P[i] = i;
}
map <int, int> s;
dfs(1, 0, s);
set <int> node;
for(int i = 1; i <= n; i++) {
node.insert(root(i));
}
for(int i = 1; i < n; i++) {
if(root(l[i]) != root(r[i])) {
++deg[root(l[i])];
++deg[root(r[i])];
}
}
int ans = 0;
for(auto i : node) {
if(deg[i] <= 1) {
++ans;
}
}
if(ans > 1) {
ans = (ans + 1) / 2;
} else {
ans = 0;
}
printf("%d\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |