-
Notifications
You must be signed in to change notification settings - Fork 0
/
2617_구슬 찾기.cpp
64 lines (58 loc) · 1011 Bytes
/
2617_구슬 찾기.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
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
const int INF = 987654321;
const int MAX_N = 100 + 2;
bool Bvisited[MAX_N] = { 0, }, Svisited[MAX_N] = { 0, };
int N, M, ans = 0;
struct Marble
{
vector <int> bigger, smaller;
}marble[MAX_N];
int bigger_DFS(int n)
{
int ret = 0;
for (auto i : marble[n].bigger)
if (!Bvisited[i])
{
Bvisited[i] = true;
ret += bigger_DFS(i) + 1;
}
return ret;
}
int smaller_DFS(int n)
{
int ret = 0;
for (auto i : marble[n].smaller)
if(!Svisited[i])
{
Svisited[i] = true;
ret += smaller_DFS(i) + 1;
}
return ret;
}
int main()
{
cin >> N >> M;
while (M--)
{
int big, small;
cin >> big >> small;
marble[big].smaller.push_back(small);
marble[small].bigger.push_back(big);
}
for (int i = 1; i <= N; i++)
{
memset(Bvisited, 0, sizeof(Bvisited));
memset(Svisited, 0, sizeof(Svisited));
if (bigger_DFS(i) > N / 2)
{
ans++;
continue;
}
if (smaller_DFS(i) > N / 2)
ans++;
}
cout << ans;
}