-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSU Modified(DSU+Ordered Set).cpp
More file actions
204 lines (147 loc) · 3.92 KB
/
DSU Modified(DSU+Ordered Set).cpp
File metadata and controls
204 lines (147 loc) · 3.92 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//Intermediary
//Young kid on the block
//AIAsif try's Continuing the journey
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl "\n"
#define int long long int
#define ordered_set tree< int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>
//https://codeforces.com/gym/102625/problem/G
//Modified dsu
//dsu + ordered set
// [ u -- v edge] lets say I want to get the size of [left part of u in the component tree and right part of v in the same component tree]
const int N = 2e5+100;
vector<int>graph[N];
int in[N];
int out[N];
int tim;
void dfs(int s , int p)
{
in[s] = tim++;
for(auto child : graph[s])
{
if(child==p)
{
continue;
}
dfs(child,s);
}
out[s] = tim;
}
int parent[N];
ordered_set sz[N];
void make(int v)
{
parent[v] = v;
sz[v].insert(in[v]);
}
int Find(int v)
{
if(v==parent[v])
{
return v;
}
return parent[v] = Find(parent[v]);
}
void Union(int a,int b)
{
a = Find(a);
b = Find(b);
if(a!=b)
{
if(sz[a].size()<sz[b].size())
{
swap(a,b);
}
for(auto x : sz[b])
{
sz[a].insert(x);
}
parent[b] = a;
}
}
pair<int,int> left_right_size(int u,int v)
{
bool flag = false;
if(in[u]>in[v])
{
flag = true;
swap(u,v);
} // u age dukbe
u = Find(u);
int a = sz[u].order_of_key(out[v]) - sz[u].order_of_key(in[v]); // This represent same component e v er subtree size . ba right part er size
int b = sz[u].size()-a ; // this represent the component size of u or left part
if(flag)
{
return {b,a}; // b hocche u er diker size and a hocche v er diker size
}
else
{
return {a,b}; // a hocce u er diker size and b hocce v er diker size
}
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
vector<array<int,4>>edges;
set<int>s;
for(int i=1;i<n;i++)
{
int x,y,w;
cin>>x>>y>>w;
edges.push_back({i,x,y,w});
graph[x].push_back(y);
graph[y].push_back(x);
s.insert(w);
}
dfs(1,0);
sort(edges.begin() , edges.end() , [&](auto a, auto b){return a[3]<b[3]; });
for(int i=1;i<=n;i++)
{
make(i);
}
int l = 0;
int mx = 0;
vector<int>ans(n+1);
for(auto w : s)
{
int r = l;
while(r+1<edges.size() && edges[r+1][3] == w)
{
r++;
}
for(int i = l; i<= r ; i++)
{
Union(edges[i][1] , edges[i][2]);
}
for(int i=l;i<=r;i++)
{
auto take = left_right_size(edges[i][1] , edges[i][2]);
int temp = take.first * 2LL * take.second;
mx = max(temp,mx);
ans[edges[i][0]] = temp; // edges[i][0] represent id no
}
l = r+1;
}
vector<int>res;
for(int i=1;i<n;i++)
{
if(mx == ans[i])
{
res.push_back(i);
}
}
cout<<mx<<" "<<res.size()<<endl;
for(auto x : res)
{
cout<<x<<" ";
}
cout<<endl;
return 0;
}