-
Notifications
You must be signed in to change notification settings - Fork 0
/
12880_scc.cpp
115 lines (113 loc) · 2.22 KB
/
12880_scc.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 210907 #12880 ±×·¡ÇÁ Â÷ÀÌ ÃÖ¼Ò Platinum IV
// SCC + coordinate_compression + two_pointers
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#define all(v) (v).begin(), (v).end()
#define press(v) (v).erase(unique(all(v)), (v).end())
using namespace std;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
const int MAX = 51;
const int INF = 150000;
int N, id, d[MAX], m = INF, M = 0;
vector<int> v[MAX], E2;
vector<vector<int>> SCC;
vector<pii> E;
stack<int> s;
bool finished[MAX], visit[MAX][MAX];
int dfs(int x) {
d[x] = id++;
s.push(x);
int parent = d[x];
for (auto i : v[x]) {
if (visit[x][i])
continue;
if (d[i] == -1)
parent = min(parent, dfs(i));
else if (!finished[i])
parent = min(parent, d[i]);
}
if (parent == d[x]) {
vector<int> scc;
int prev = -1;
while (1) {
int t = s.top();
s.pop();
scc.push_back(t);
finished[t] = true;
prev = t;
if (t == x)
break;
}
SCC.push_back(scc);
}
return parent;
}
bool getScc() {
id = 0;
s.empty();
SCC.clear();
fill(d, d + N, -1);
fill(finished, finished + N, false);
for (int i = 0; i < N; i++) {
if (d[i] == -1)
dfs(i);
}
return SCC.size() == 1 ? true : false;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int num;
cin >> num;
E.push_back({ num, {i, j} });
E2.push_back(num);
m = min(m, num);
M = max(M, num);
}
}
if (N == 1) {
cout << 0 << "\n";
return 0;
}
sort(all(E));
sort(all(E2));
press(E2);
int s = m, e = m, ans = INF, size = E.size();
int idx = 0, idx2 = 0, idx3 = 0, idx4 = 0;
bool newE = true;
while (s != M) {
while (newE && idx<size && E[idx].first == e) {
int n1 = E[idx].second.first;
int n2 = E[idx].second.second;
v[n1].push_back(n2);
idx++;
}
while (!newE && idx2<size && E[idx2].first < s) {
int n1 = E[idx2].second.first;
int n2 = E[idx2].second.second;
visit[n1][n2] = true;
idx2++;
}
bool flag = getScc();
if (e != M && !flag) {
newE = true;
e = E2[++idx4];
}
else {
if (flag)
ans = min(ans, e - s);
newE = false;
s = E2[++idx3];
}
}
cout << ans << "\n";
}