-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0406-2.cpp
More file actions
58 lines (49 loc) · 1.21 KB
/
0406-2.cpp
File metadata and controls
58 lines (49 loc) · 1.21 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
#include <string>
#include <vector>
#include <cstring>
using namespace std;
bool isFixed[101];
bool isVisited[101];
vector<vector<int>> edges = vector<vector<int>>(101, vector<int>());
vector<int> vec;
vector<int> check;
void topo(int idx)
{
if(isVisited[idx]) return;
for(int i = 0; i < edges[idx].size(); i++)
{
topo(edges[idx][i]);
}
isVisited[idx] = true;
vec.push_back(idx);
}
int solution(int n, vector<vector<int>> results) {
int answer = 0;
memset(isFixed, true, sizeof(isFixed));
for(int i = 0; i < results.size(); i++)
{
edges[results[i][0]].push_back(results[i][1]);
}
for(int i = 1; i <= n; i++)
{
memset(isVisited, false, sizeof(isVisited));
vec.clear();
for(int j = 0; j < n; j++)
{
int start = ((i + j) % n) + 1;
if(!isVisited[start]) topo(start);
}
if(check.empty()) {
check = vector<int>(vec.begin(), vec.end());
}
for(int j = 0; j < n; j++)
{
if(check[j] != vec[j]) isFixed[j] = false;
}
}
for(int i = 0; i < n; i++)
{
if(isFixed[i]) answer++;
}
return answer;
}