forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmafija.cpp
56 lines (44 loc) · 925 Bytes
/
mafija.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
#include <bits/stdc++.h>
using namespace std;
vector<bool> vis;
vector<int> indeg;
vector<int> adj;
int mafia = 0;
void dfs(int curr, bool ismafia) {
if(vis[curr]) return;
vis[curr] = true;
if(ismafia) mafia++;
indeg[adj[curr]]--;
if(indeg[adj[curr]] == 0 || ismafia) {
dfs(adj[curr], !ismafia);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
vis.resize(n,false);
indeg.resize(n,0);
adj.resize(n);
for(int i = 0; i < n; i++) {
int t;
cin >> t;
t--;
indeg[t]++;
adj[i] = t;
}
for(int i = 0; i < n; i++) {
if(indeg[i] == 0) {
dfs(i, true);
}
}
int unvis = 0;
for(int i = 0; i < n; i++) {
if(!vis[i]) {
unvis++;
}
}
cout << mafia + (unvis/2) << endl;
}