| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 332887 | Kenzo_1114 | Mergers (JOI19_mergers) | C++17 | 87 ms | 42724 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500010;
int n, k, qtt[MAXN], in[MAXN];
int par[MAXN], depth[MAXN], need[MAXN], have[MAXN];
set<int> s[MAXN];
set<int> :: iterator it;
bool marc[MAXN];
vector<int> graph[MAXN];
map<pair<int, int>, bool> specialEdge;
void Special(int u, int v)
{
specialEdge[make_pair(u, v)] = true;
specialEdge[make_pair(v, u)] = true;
}
int find(int v)
{
return (v == par[v]) ? v : par[v] = find(par[v]);
}
void join(int a, int b)
{
a = find(a), b = find(b);
if(a == b) return;
if(depth[a] > depth[b]) swap(a, b);
par[a] = b;
have[b] += have[a];
for(it = s[a].begin(); it != s[a].end(); it++)
if(s[b].find(*it) == s[b].end()) need[b] += qtt[*it], s[b].insert(*it);
if(depth[a] == depth[b]) depth[b]++;
}
int main ()
{
scanf("%d %d", &n, &k);
for(int i = 0, u, v; i < n - 1; i++)
{
scanf("%d %d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
in[u]++, in[v]++;
}
queue<int> q;
for(int i = 1, g; i <= n; i++)
{
scanf("%d", &g); qtt[g]++;
if(in[i] == 1) q.push(i), marc[i] = true;
}
for(int i = 1; i <= n; i++)
{
par[i] = i;
need[i] = qtt[i];
have[i] = 1;
}
while(!q.empty())
{
int v = q.front(); q.pop();
for(int i = 0; i < (int) graph[v].size(); i++)
{
int u = graph[v][i];
if(marc[u]) continue;
if(have[v] == need[v]) Special(u, v);
q.push(u);
marc[u] = true;
join(v, u);
}
}
for(int i = 1; i <= n; i++)
{
marc[i] = false;
if(in[i] == 1) q.push(i), marc[i] = true;
}
int ans = 0;
while(!q.empty())
{
int v = q.front(); q.pop();
for(int i = 0; i < (int) graph[v].size(); i++)
{
int u = graph[v][i];
if(marc[u]) continue;
if(specialEdge[make_pair(u, v)]){ ans++; continue; }
q.push(u);
marc[u] = true;
join(v, u);
}
}
printf("%d\n", (ans + 1) >> 1);
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4
*/컴파일 시 표준 에러 (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... | ||||
