-
Notifications
You must be signed in to change notification settings - Fork 70
/
Water_Connection_Problem.cpp
75 lines (66 loc) · 1.62 KB
/
Water_Connection_Problem.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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
int mini;
public:
int dfs(int start[], int dia[], int x)
{
if(dia[x] < mini && dia[x] != 0) mini = dia[x];
if(start[x] == 0) return x;
return dfs(start, dia, start[x]);
}
vector<vector<int>> solve(int n,int p,vector<int> a,vector<int> b,vector<int> d)
{
// code here
vector<vector<int>> answer;
int start[n+1];
int end[n+1];
int dia[n+1];
for(int i=0; i<=n; i++)
{
start[i]=0;
end[i]=0;
dia[i]=0;
}
for(int i=0; i<p; i++)
{
start[a[i]] = b[i];
end[b[i]] = a[i];
dia[a[i]] = d[i];
}
//preprocessing is done
// for(int i=1; i<=n; i++)
// cout << start[i] << " " << end[i] << " " << dia[i]
for(int i=1; i<=n; i++)
{
if(start[i] != 0 && end[i] == 0)
{
mini = INT_MAX;
int end = dfs(start, dia, i);
answer.push_back({i, end, mini});
}
}
return answer;
}
};
int main()
{
int t,n,p;
cin>>t;
while(t--)
{
cin>>n>>p;
vector<int> a(p),b(p),d(p);
for(int i=0;i<p;i++){
cin>>a[i]>>b[i]>>d[i];
}
Solution obj;
vector<vector<int>> answer = obj.solve(n,p,a,b,d);
cout<<answer.size()<<endl;
for(auto i:answer)
{
cout<<i[0]<<" "<<i[1]<<" "<<i[2]<<endl;
}
}
return 0;