forked from KhyatiSaini/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWater Connection.cpp
More file actions
62 lines (60 loc) · 1006 Bytes
/
Copy pathWater Connection.cpp
File metadata and controls
62 lines (60 loc) · 1006 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int n, p;
int rd[1100];
int wt[1100];
int cd[1100];
vector<int> a;
vector<int> b;
vector<int> c;
int ans;
int dfs(int w)
{
if (cd[w] == 0)
return w;
if (wt[w] < ans)
ans = wt[w];
return dfs(cd[w]);
}
void solve(int arr[][3])
{
int i = 0;
while (i < p) {
int q = arr[i][0], h = arr[i][1],
t = arr[i][2];
cd[q] = h;
wt[q] = t;
rd[h] = q;
i++;
}
a.clear();
b.clear();
c.clear();
for (int j = 1; j <= n; ++j) {
if (rd[j] == 0 && cd[j]) {
ans = 1000000000;
int w = dfs(j);
a.push_back(j);
b.push_back(w);
c.push_back(ans);
}
cout << a.size() << endl;
for (int j = 0; j < a.size(); ++j)
cout << a[j] << " " << b[j]
<< " " << c[j] << endl;
}
int main()
{
n = 9, p = 6;
memset(rd, 0, sizeof(rd));
memset(cd, 0, sizeof(cd));
memset(wt, 0, sizeof(wt));
int arr[][3] = { { 7, 4, 98 },
{ 5, 9, 72 },
{ 4, 6, 10 },
{ 2, 8, 22 },
{ 9, 7, 17 },
{ 3, 1, 66 } };
solve(arr);
return 0;
}