-
Notifications
You must be signed in to change notification settings - Fork 1
/
PossiblePathsInATree.cpp
65 lines (64 loc) · 1.51 KB
/
PossiblePathsInATree.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
//User function template for C++
class Solution {
int ans;
private:
int root(int i, vector<int> &parent)
{
while(parent[i]!=i)
{
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
int Union(int a, int b, vector<int> &parent, vector<int> &sz)
{
int ra = root(a, parent);
int rb = root(b, parent);
if(ra == rb)
return sz[ra]*sz[ra];
if(sz[ra] < sz[rb])
{
swap(ra,rb);
swap(a,b);
}
ans += sz[ra]*sz[rb];
parent[rb] = ra;
sz[ra] += sz[rb];
return ans;
}
public:
vector<int> maximumWeight(int n, vector<vector<int>> edges, int q, vector<int> &queries)
{
ans = 0;
vector<int> parent(n+1, 0), sz(n+1, 0);
for(int i = 0;i <= n; i++)
{
parent[i] = i;
sz[i] = 1;
}
vector<pair<int, pair<int, int>>> wt;
for(int i = 0; i < n-1; i++)
wt.push_back({edges[i][2] , {edges[i][0], edges[i][1]}}); sort(wt.begin() , wt.end());
map<int, int> mp;
for(int i = 0;i < n-1; i++){
int a = wt[i].first;
int b = wt[i].second.first;
int c = wt[i].second.second;
mp[a] = Union(b, c, parent, sz);
}
vector<int> res;
for(int i = 0; i < q; i++)
{
auto val = mp.upper_bound(queries[i]);
if(val == mp.begin())
res.push_back(0);
else
{
val--;
res.push_back(val->second);
}
}
return res;
}
};