forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path230.cpp
More file actions
78 lines (73 loc) · 1.24 KB
/
230.cpp
File metadata and controls
78 lines (73 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//ac
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int n, m;
void run() {
vector<vector<int> > mat(n, vector<int>(n, 0));
int flag = 0;
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
--a, --b;
if (mat[a][b] == 1) {
flag = 1;
}
mat[a][b] = -1, mat[b][a] = 1;
}
if (flag) {
printf("No solution\n");
return;
}
//Marshall Algorithm
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[j][i] == 1) {
for (int k = 0; k < n; ++k) {
if (mat[i][k] == 1) {
if (mat[j][k] == -1) {
printf("No solution\n");
return;
}
mat[j][k] = 1, mat[k][j] = -1;
}
}
}
}
}
vector<int> out(n);
vector<int> num(n, 0);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1) {
++num[i];
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (num[j] == 0) {
out[j] = i;
num[j] = -1;
for (int k = 0; k < n; ++k) {
if (mat[k][j] == 1) {
mat[k][j] = mat[j][k] = 0;
--num[k];
}
}
break;
}
}
}
for (int i = 0; i < n; ++i) {
if (i) printf(" ");
printf("%d", out[i] + 1);
}
printf("\n");
}
int main() {
scanf("%d %d", &n, &m);
run();
return 0;
}