|
|
|
|
|
|
|
|
|
|
|
|
|
#include <bits/stdc++.h> |
|
using namespace std; |
|
typedef long long ll; |
|
|
|
int V; |
|
vector<tuple<ll,int,int>> edge_list; |
|
|
|
class ufds { |
|
private: |
|
vector<int> parent; |
|
vector<int> size; |
|
int qty_disjoint_sets; |
|
|
|
public: |
|
ufds() {} |
|
ufds(int N) { |
|
parent.resize(N); |
|
size.assign(N, 1); |
|
qty_disjoint_sets = N; |
|
|
|
|
|
for (int n = 0; n < N; n++) parent[n] = n; |
|
} |
|
|
|
|
|
int find_set(int n) { |
|
if (parent[n] == n) return n; |
|
return parent[n] = find_set(parent[n]); |
|
} |
|
|
|
|
|
bool is_same_set(int n, int m) { |
|
return find_set(n) == find_set(m); |
|
} |
|
|
|
|
|
void union_sets(int n, int m) { |
|
int n_id = find_set(n); |
|
int m_id = find_set(m); |
|
|
|
|
|
if (!is_same_set(n_id, m_id)) { |
|
if (size[n_id] > size[m_id]) |
|
swap(n_id, m_id); |
|
|
|
parent[n_id] = m_id; |
|
size[m_id] += size[n_id]; |
|
|
|
qty_disjoint_sets--; |
|
} |
|
} |
|
|
|
|
|
int set_size(int n) { |
|
return size[find_set(n)]; |
|
} |
|
|
|
|
|
int count() { |
|
return qty_disjoint_sets; |
|
} |
|
}; |
|
|
|
|
|
ll kruskal(int s, int t) { |
|
ufds sets(V); |
|
sort(edge_list.begin(), edge_list.end()); |
|
|
|
|
|
for (auto edge: edge_list) { |
|
int u, v; ll w; tie(w,u,v) = edge; |
|
|
|
|
|
if (!sets.is_same_set(u, v)) { |
|
sets.union_sets(u, v); |
|
|
|
if (sets.is_same_set(s, t)) |
|
return w; |
|
} |
|
} |
|
return -1; |
|
} |
|
|
|
int main() { |
|
int E, Q, test = 1; |
|
while (cin >> V >> E >> Q && (V || E || Q)) { |
|
if (test > 1) cout << endl; |
|
edge_list.clear(); |
|
while (E--) { |
|
int u, v; ll w; cin >> u >> v >> w; u--; v--; |
|
edge_list.push_back({w, u, v}); |
|
} |
|
|
|
cout << "Case #" << (test++) << endl; |
|
while (Q--) { |
|
int s, t; cin >> s >> t; s--; t--; |
|
ll max_w = kruskal(s, t); |
|
if (max_w == -1) cout << "no path" << endl; |
|
else cout << max_w << endl; |
|
} |
|
} |
|
|
|
return 0; |
|
} |
|
|