forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2617.cpp
More file actions
69 lines (57 loc) · 1.29 KB
/
Copy path2617.cpp
File metadata and controls
69 lines (57 loc) · 1.29 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
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
using namespace std;
typedef struct marble {
bool visited;
vector<int> next_marbles;
vector<int> previous_marbles;
};
vector<marble> Marbles;
int N, M;
int next_dfs(int n) {
int count = 1;
int i;
Marbles[n].visited = true;
for (i = 0; i < Marbles[n].next_marbles.size(); i++) {
if (!Marbles[Marbles[n].next_marbles[i]-1].visited) { count += next_dfs(Marbles[n].next_marbles[i]- 1); }
}
return count;
}
int previous_dfs(int n) {
int count = 1;
int i;
Marbles[n].visited = true;
for (i = 0; i < Marbles[n].previous_marbles.size(); i++) {
if (!Marbles[Marbles[n].previous_marbles[i] - 1].visited) count += previous_dfs(Marbles[n].previous_marbles[i] - 1);
}
return count;
}
int main()
{
int i, j;
cin >> N >> M;
int ans = 0;
for (i = 0; i < N; i++) {
marble new_marble;
Marbles.push_back(new_marble);
}
for (i = 0; i < M; i++) {
int next, x;
cin >> next >> x;
Marbles[x - 1].next_marbles.push_back(next);
Marbles[next - 1].previous_marbles.push_back(x);
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
Marbles[j].visited = false;
}
if (next_dfs(i) > (N + 1) / 2) ans += 1;
else {
for (j = 0; j < N; j++) {
Marbles[j].visited = false;
}
if (previous_dfs(i) > (N + 1) / 2) ans += 1;
}
}
cout << ans;
}