-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTask P.cpp
More file actions
92 lines (76 loc) · 2.01 KB
/
Task P.cpp
File metadata and controls
92 lines (76 loc) · 2.01 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
void dfs(int vertex, int n, bool direction, vector<bool> &visited, vector<vector<bool>> &g_check){
visited[vertex] = true;
for (int i = 0; i < n; i++){
if (direction){
if (g_check[i][vertex] && !visited[i]) {
dfs(i, n, direction, visited, g_check);
}
} else {
if (g_check[vertex][i] && !visited[i]){
dfs(i, n, direction, visited, g_check);
}
}
}
}
bool check_connectivity(int n, vector<bool> &visited){
for (int i = 0; i < n; i++){
if (visited[i]){
continue;
} else {
return false;
}
}
return true;
}
void solve(){
int n;
cin >> n;
vector<vector<int>> graph(n, vector<int> (n));
vector<vector<bool>> g_check(n, vector<bool>(n));
vector<bool> visited;
int i = 0, j = 0, oil;
while (cin >> oil){
graph[i][j] = oil;
j++;
if (j == n){
i++;
j = 0;
}
}
int l = 0, r = 1000000000;
while (l != r){
int mid = (l + r) / 2;
visited = vector<bool>(n, false);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
g_check[i][j] = graph[i][j] <= mid;
}
}
dfs(0, n, 0, visited, g_check);
bool connectivity = false;
if (check_connectivity(n, visited)){
visited = vector<bool>(n, false);
dfs(0, n, 1, visited, g_check);
if (!check_connectivity(n, visited)){
connectivity = true;
}
} else connectivity = true;
if (connectivity){
l = mid + 1;
} else {
r = mid;
}
}
cout << l << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
return 0;
}