-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriticalLinks_UVA796.cpp
154 lines (137 loc) · 3.45 KB
/
criticalLinks_UVA796.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
ifstream fin("criticalLinks_UVA796.in");
ofstream fout("criticalLinks_UVA796.out");
bool check(int p, int q, int n, vector<vector<bool>> & a)
{
stack<int> s; s.push(p);
vector<bool> visit(n + 1, false);
while (!(s.empty()))
{
int t = s.top();
bool _find = false;
for (int i = 0; i <= n - 1; ++i)
{
if (t == p && i == q)
{
// do nothing
continue;
}
else if (a[t][i] == true && visit[i] == false)
{
if (i == q)
{
return true;
}
visit[i] = true;
_find = true;
s.push(i);
}
}
if (_find == false)
{
s.pop();
}
}
return false;
}
int main()
{
bool tF = true;
while (true)
{
if (tF == false)
{
fout << '\n';
}
tF = true;
int n = -1;
fin >> n;
if (n == 0)
{
fout << "0 critical links\n\n";
continue;
}
else if (n == -1)
{
break;
}
vector<vector<bool>> a(n + 1, vector<bool>(n + 1, false));
string useless;
getline(fin, useless);
for (int c = 1; c <= n; ++c)
{
string in;
getline(fin, in);
int size0 = in.size(), first = -1;
string temp = "";
for (int i = 0; i <= size0 - 1; ++i)
{
if (in[i] == ' ')
{
if (first == -1)
{
first = stoi(temp);
++i;
while (in[i] != ' ')
{
++i;
}
}
else
{
int j = stoi(temp);
a[first][j] = true;
a[j][first] = true;
}
temp = "";
}
else
{
temp += in[i];
}
}
if (temp != "")
{
int j = stoi(temp);
a[first][j] = true;
a[j][first] = true;
}
}
int total = 0;
vector<string> out;
for (int i = 0; i <= n - 1; ++i)
{
for (int j = i + 1; j <= n - 1; ++j)
{
if (a[i][j] == true)
{
if (check(i, j, n, a) == false)
{
++total;
string temp = to_string(i) + " - " + to_string(j);
out.push_back(temp);
}
}
}
}
fout << total << " critical links\n";
for (int i = 0; i <= total - 1; ++i)
{
fout << out[i] << '\n';
}
fout << '\n';
}
return 0;
}