-
Notifications
You must be signed in to change notification settings - Fork 9
/
10048-Audiophobia.cpp
74 lines (68 loc) · 1.78 KB
/
10048-Audiophobia.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
#include <bits/stdc++.h>
using namespace std;
vector<int> parent;
vector<bool> visited;
vector<vector<pair<int,int>>> mst;
int highest = 0;
int find(int x){
return parent[x] == x ? x : parent[x] = find(parent[x]);
}
void join(int a,int b){
int parA = find(a), parB = find(b);
parent[parA] = parB;
}
bool connected(int a,int b){
return find(a) == find(b);
}
bool dfs(int cur,int target){
if(cur == target) return true;
if(visited[cur]) return false;
visited[cur] = true;
for(auto& edge : mst[cur]){
if(dfs(edge.first,target)) {
highest = max(highest, edge.second);
return true;
}
}
return false;
}
int main()
{
int c,s,q,a,b,w,tc=1;
bool first = true;
while(scanf("%d %d %d",&c,&s,&q),(c+s+q)!=0){
if(!first) printf("\n");
first = false;
parent.assign(c,0);
mst.assign(c,vector<pair<int,int>>());
for(int i=0;i<c;i++) parent[i] = i;
vector<tuple<int,int,int>> edges;
for(int i=0;i<s;i++){
scanf("%d %d %d",&a,&b,&w);
a--; b--;
edges.push_back(make_tuple(w,a,b));
}
sort(edges.begin(),edges.end());
for(auto& edge : edges){
tie(w,a,b) = edge;
if(!connected(a,b)){
join(a,b);
mst[a].push_back({b,w});
mst[b].push_back({a,w});
}
}
printf("Case #%d\n",tc++);
for(int i=0;i<q;i++){
scanf("%d %d",&a,&b);
a--; b--;
if(connected(a,b)){
visited.assign(c,false);
highest = INT_MIN;
dfs(a,b);
printf("%d\n",highest);
} else {
printf("no path\n");
}
}
}
}