forked from MountAim/Graph-Coding-Minutes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
72-Choosing Capital for Treeland.cpp
79 lines (77 loc) · 1.46 KB
/
72-Choosing Capital for Treeland.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>v;
vector<int>sub,ans;
unordered_map<int,unordered_set<int>>m;
void dfs_1(int x,int p)
{
sub[x]=0;
for(auto itr:v[x])
{
if(itr!=p)
{
dfs_1(itr,x);
int cnt=0;
if(m[itr].find(x)!=m[itr].end())
{
cnt=1;
}
sub[x]+=sub[itr]+cnt;
}
}
}
void dfs(int x,int p)
{
if(p!=0)
{
int par=ans[p]-sub[x];
if(m[x].find(p)!=m[x].end())
{
par--;
}
ans[x]=sub[x]+par;
if(m[p].find(x)!=m[p].end())
{
ans[x]++;
}
}
for(auto itr:v[x])
{
if(itr!=p)
{
dfs(itr,x);
}
}
}
vector<int> choosingCapital(int n, vector<vector<int>> edges)
{
v=vector<vector<int>>(n+1,vector<int>());
ans=vector<int>(n+1);
sub=vector<int>(n+1);
m.clear();
int x,y,val;
for(int i=0;i<n-1;i++)
{
x=edges[i][0],y=edges[i][1];
v[x].push_back(y);
v[y].push_back(x);
m[x].insert(y);
}
dfs_1(1,0);
ans[1]=sub[1];
dfs(1,0);
vector<int>vec;
val=1e9;
for(int i=1;i<=n;i++)
{
val=min(val,ans[i]);
}
for(int i=1;i<=n;i++)
{
if(val==ans[i])
{
vec.push_back(i);
}
}
return vec;
}