-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuva_10048.cpp
106 lines (90 loc) · 2.15 KB
/
uva_10048.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
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = a, _b = b; i < _b; ++i)
#define FOR(i, a, b) for(int i = a, _b = b; i <= b; ++i)
#define FORD(i, a, b) for(int i = a, _b = b; i >= b; --i)
#define endl "\n"
#define newline cout << "\n"
#define puts(_xxx_) cout << _xxx_ << "\n"
#define db1(x) cout << #x << " = " << x << "\n"
#define db2(a, i) cout << #a << "[" << i << "] = " << a[i] << "\n"
using namespace std;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef long long llong;
const int MX = 105;
int V, E, Q;
vector<iii> edges;
int link[MX], ele[MX];
int ans[MX][MX];
vector<ii> adj[MX];
bool visited[MX];
void dfs (int start, int cur) {
visited[cur] = true;
for(auto e: adj[cur]) {
if(!visited[e.first]) {
ans[start][e.first] = max(ans[start][cur], e.second);
dfs(start, e.first);
}
}
}
int find (int u) {
while(u != link[u]) u = link[u];
return u;
}
bool same (int u, int v) {
return find(u) == find(v);
}
void unite (int u, int v) {
u = find(u);
v = find(v);
if(ele[u] < ele[v]) swap(u, v);
ele[u] += ele[v];
link[v] = u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifndef Home
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
for(int case_no = 1; ; ++case_no) {
cin >> V >> E >> Q;
if(V+E+Q == 0) break;
if(case_no != 1) puts("");
cout << "Case #" << case_no << endl;
edges.clear();
memset(ans, 0, sizeof ans);
FOR(v, 1, V) adj[v].clear();
int u, v, w;
REP(i, 0, E) {
cin >> u >> v >> w;
edges.push_back(iii(w, u, v));
}
sort(edges.begin(), edges.end());
// Kruskal
FOR(v, 1, V) {
link[v] = v;
ele[v] = 1;
}
for(auto e: edges) {
tie(w, u, v) = e;
if(!same(u, v)) {
unite(u, v);
adj[u].push_back(ii(v, w));
adj[v].push_back(ii(u, w));
}
}
REP(i, 0, Q) {
cin >> u >> v;
if(ans[u][v] == 0) ans[u][v] = ans[v][u];
if(ans[u][v] == 0) {
memset(visited, 0, sizeof visited);
dfs(u, u);
}
if(ans[u][v] == 0) cout << "no path" << endl;
else cout << ans[u][v] << endl;
}
}
return 0;
}