-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbosses.cpp
More file actions
60 lines (56 loc) · 1.24 KB
/
Copy pathbosses.cpp
File metadata and controls
60 lines (56 loc) · 1.24 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define f first
#define s second
int N;
bool vis[5000];
vector<int> graph[5000];
vector<int> tree[5000];
pii dfs(int id){
int res = 1;
int total = 0;
for(int i : tree[id]){
pii x = dfs(i);
res += x.f;
total += x.s;
}
return pii(res, res + total);
}
signed main(){
cin >> N;
for(int i = 0; i < N; i++){
int k; cin >> k;
for(int j = 0; j < k; j++){
int x; cin >> x;
graph[x-1].push_back(i);
}
}
int ans = 1e9;
for(int i = 0; i < N; i++){
fill(vis, vis+N, false);
for(int j = 0; j < N; j++){
tree[j].clear();
}
queue<pii> q;
q.push(pii(-1, i));
int e = 0;
while(!q.empty()){
int id = q.front().s, pv = q.front().f;
q.pop();
if(vis[id]) continue;
vis[id] = true;
if(pv != -1){
tree[pv].push_back(id);
e++;
}
for(int j : graph[id]){
q.push(pii(id, j));
}
}
if(e == N-1){
ans = min(ans, dfs(i).s);
}
}
cout << ans << endl;
}