D. Choosing Capital for Treeland
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.
The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a to any other city. For that some roads may have to be inversed.
Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.
Input
The first input line contains integer n (2 ≤ n ≤ 2·105) — the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers si, ti (1 ≤ si, ti ≤ n; si ≠ ti) — the numbers of cities, connected by that road. The i-th road is oriented from city si to city ti. You can consider cities in Treeland indexed from 1 to n.
Output
In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.
<코드>
#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int maxn = 2e5 + 10, inf = 1 << 29;
int val[maxn] = { 0 }, ans;
struct node {
int id, dir, num;
node(int i, int d, int n) :id(i), dir(d), num(n) {}
};
vector<node> s[maxn];
bool cmp1(node a1, node a2) {
return a1.num > a2.num;
}
void dfs1(int x, int fa) {
int len = s[x].size();
for (int i = 0; i < len; i++) {
int v = s[x][i].id;
if (v == fa)
continue;
dfs1(v, x);
val[x] += (val[v] + s[x][i].dir);
}
}
void dfs2(int x, int fa) {
int len = s[x].size();
int c, k;
if (fa) {
for (int i = 0; i < len; i++) {
if (s[x][i].id == fa) {
k = i;
c = s[x][i].dir;
break;
}
}
val[x] += (val[fa] - val[x] - !c);
val[x] += c;
}
for (int i = 0; i < len; i++) {
int v = s[x][i].id;
if (v == fa)
continue;
dfs2(v, x);
}
ans = min(ans, val[x]);
}
int main() {
int n, a, b;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i < n; i++) {
scanf("%d %d", &a, &b);
s[b].push_back(node(a, 1, 0));
s[a].push_back(node(b, 0, 0));
}
ans = inf;
dfs1(1, 0);
dfs2(1, 0);
printf("%d\n", ans);
int cc = 0;
for (int i = 1; i <= n; i++) {
if (val[i] == ans) {
if (cc)
printf(" ");
printf("%d", i);
cc++;
}
}
printf("\n");
for (int i = 1; i <= n; i++) {
val[i] = 0;
s[i].clear();
}
}
return 0;
}
https://codeforces.com/problemset/problem/219/D
'🧩PS > 🥈Nomal' 카테고리의 다른 글
[C/C++] 백준 10989번 - 수 정렬하기 3(Counting Sort) (0) | 2020.07.01 |
---|---|
[C/C++] Codeforce 522A - Reposts(DFS) (0) | 2020.06.09 |
[C/C++] Codeforce 276C - Little Girl and Maximum Sum (0) | 2020.06.07 |
[C/C++] 백준 15649번 - N과 M (1) (DFS, 백트래킹) (0) | 2020.06.06 |
[C/C++] 백준 2606번 바이러스(BFS/플로이드 와샬 알고리즘) (0) | 2020.06.06 |