-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallingCircles_UVA247OJ.cpp
143 lines (122 loc) · 3.12 KB
/
callingCircles_UVA247OJ.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <stack>
using namespace std;
void tarjan(int u, int n, int & index, vector<int> & dfn, vector<int> & low, vector<bool> & inStack, vector<vector<int>> & g, stack<int> & stk, vector<string> & names)
{
++index;
dfn[u] = index; low[u] = index;
stk.push(u); inStack[u] = true;
int ans = 0;
int size0 = g[u].size();
for (int i = 0; i <= size0 - 1; ++i)
{
int v = g[u][i];
if (dfn[v] == -1)
{
tarjan(v, n, index, dfn, low, inStack, g, stk, names);
low[u] = min(low[u], low[v]);
}
else if (inStack[v] == true)
{
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u])
{
bool first = true;
int t;
vector<int> temp;
do
{
t = stk.top();
if (first == true)
{
first = false;
}
else
{
cout << ", ";
}
cout << names[t];
inStack[t] = false;
stk.pop();
}
while (t != u);
cout << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int _case = 0;
int lastN = -1, lastM = -1;
while (true)
{
++_case;
int n = 0, m = 0;
cin >> n >> m;
if (n + m == 0) break;
if (_case == 98)
{
cout << "";
}
if (_case > 1 && lastN != 0 && lastM != 0)
{
cout << '\n';
}
if (n == 0 || m == 0)
{
cout << "Calling circles for data set " << _case << "\n";
continue;
}
vector<vector<int>> g(n + 1);
map<string, int> _map;
vector<string> names(n + 1);
int nameCount = 1;
for (int c = 1; c <= m; ++c)
{
string in0, in1; cin >> in0 >> in1;
if (_map[in0] == 0)
{
_map[in0] = nameCount;
names[nameCount] = in0;
++nameCount;
}
if (_map[in1] == 0)
{
_map[in1] = nameCount;
names[nameCount] = in1;
++nameCount;
}
g[_map[in0]].push_back(_map[in1]);
}
cout << "Calling circles for data set " << _case << ":\n";
int zero = 0, _count = 0;
vector<int> dfn(n + 1, -1), low(n + 1, -1);
vector<bool> inStack(n + 1, false);
stack<int> stk;
vector<int> res(n + 1, 0);
for (int i = 1; i <= n; ++i)
{
if (dfn[i] == -1)
{
tarjan(i, n, zero, dfn, low, inStack, g, stk, names);
}
}
lastN = n;
lastM = m;
}
cout.flush();
return 0;
}