#include <bits/stdc++.h>
#define debug printf
#define ff first
#define ss second
#define mkt make_tuple
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define ll long long
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
#define pii pair<int,int>
#define mk make_pair
#define pb push_back
#define tiiii tuple<int,int,int, int>
const int MAXN = 1e5+10 ;
using namespace std ;
ll ans ;
struct Query
{
vector<int> procuro ;
int other_vert , edge_weight ;
bool operator < ( Query other ) const { return edge_weight < other.edge_weight ; }
} ;
int N, K ;
int dsu[MAXN] , qtd[MAXN] , weight[MAXN] ;
vector<int> adj_dsu[MAXN] ;
vector<pair<int,int> > adj[MAXN] ;
vector< tuple<int,int,int, int> > edges ;
vector<Query> queries[MAXN] ;
bool marc[MAXN] ;
// ----------------------- UNION FIND -------------------------
int find(int x) { return dsu[x] = (x == dsu[x]) ? x : find(dsu[x]) ; }
int w_union_find ;
void dfs_union_find(int x, int y , int par, int depth )
{
if(w_union_find-K-depth-1 >= 0)
queries[y].back().procuro.pb( min(w_union_find-K-depth-1, N) ) ;
for(auto e : adj_dsu[x] )
{
if(e == par ) continue ;
dfs_union_find(e, y, x, depth+1) ;
}
}
void join(int x, int y, int w, int i)
{
int a = find(x) ;
int b = find(y) ;
w_union_find = w ;
if( qtd[a] > qtd[b] )
{
swap(a,b) ;
swap(x,y) ;
}
queries[y].pb( Query() ) ;
queries[y].back().edge_weight = i-1 ;
queries[y].back().other_vert = x ;
dfs_union_find(x, y, -1, 0 );
adj_dsu[x].pb(y) ;
adj_dsu[y].pb(x) ;
dsu[a] = b ;
qtd[b] += qtd[a] ;
}
// ------------------------------------------------------------
int q ;
int sub[MAXN] , bit[MAXN] , dist[MAXN] ;
vector<pair<int,int> > pares , perguntas ;
void upd(int i , int val) { if(i == -1 ) return ;for(++i; i < MAXN ; i += i &-i ) bit[i] += val; }
int qry(int i)
{
int tot = 0 ;
for(++i; i > 0 ; i -= i &-i ) tot += bit[i] ;
return tot ;
}
void dfs1(int x, int par )
{
sub[x] = 1 ;
for(auto e : adj[x])
if(e.ff != par && !marc[e.ff])
{
dfs1(e.ff,x) ;
sub[x] += sub[e.ff] ;
}
}
int dfs2(int x, int par )
{
for(auto e : adj[x] )
{
if(e.ff == par || marc[e.ff]) continue ;
if( sub[e.ff] > q/2 ) return dfs2(e.ff, x ) ;
}
return x ;
}
void dfs3(int x, int par , int cur_mx , int cur_depth )
{
dist[x] = cur_depth ;
pares.pb( mk(cur_mx, cur_depth) ) ;
for(int i = 0 ; i < sz(queries[x]) ; i++ )
{
if( queries[x][i].other_vert == par || cur_mx > queries[x][i].edge_weight ) continue ;
perguntas.pb(mk(x,i)) ;
}
for(auto e : adj[x] )
{
if(marc[e.ff] || e.ff == par ) continue ;
dfs3(e.ff, x, max(cur_mx, weight[e.ss]) , cur_depth+1 ) ;
}
}
void solve(int ptr1, int ptr2, int s)
{
while(ptr1 < sz(pares) || ptr2 < sz(perguntas))
{
if( ptr2 == sz(perguntas) )
{
upd(pares[ptr1].ss, 1) ;
ptr1++ ;
continue ;
}
int x = perguntas[ptr2].ff ;
int y = perguntas[ptr2].ss ;
int k = queries[ x ][y].edge_weight ;
if(ptr1 == sz(pares) || pares[ptr1].ff > k)
{
for( auto e : queries[x][y].procuro )
ans += qry( e - dist[x]) * s ;
ptr2++ ;
continue ;
}
else
{
upd(pares[ptr1].ss , 1 ) ;
ptr1++ ;
continue ;
}
}
}
bool cmp(pii p1, pii p2) { return queries[p1.ff][p1.ss] < queries[p2.ff][p2.ss] ; }
void decompose(int x)
{
dfs1(x,-1) ;
q = sub[x] ;
int cn = dfs2(x,-1) ;
marc[cn] = true ;
pares.clear() ; perguntas.clear() ;
pares.pb( mk( -1, 0 ) ) ;
for(auto e : adj[cn])
{
if(marc[e.ff]) continue ;
int l_pares= sz(pares) ;
int l_perguntas = sz(perguntas) ;
dfs3(e.ff, cn, weight[e.ss] , 1 ) ;
sort( pares.begin()+l_pares , pares.end() ) ;
sort( perguntas.begin()+l_perguntas, perguntas.end() , cmp ) ;
int ptr1 = l_pares , ptr2 = l_perguntas ;
solve(ptr1, ptr2,-1) ;
for(int i = l_pares ; i < sz(pares) ; i++ )
upd( pares[i].ss , -1 ) ;
}
sort(all(pares)) ;
sort(all(perguntas),cmp) ;
solve(0,0,1) ;
for(int i = 0 ; i < sz(pares) ; i++ )
upd( pares[i].ss , -1 ) ;
perguntas.clear() ;
for(int i = 0 ; i < sz(queries[cn]) ; i++ )
{
perguntas.pb(mk(cn,i)) ;
queries[cn][i].edge_weight-- ;
}
dist[cn] = 0 ;
solve(0,0,1) ;
for(int i = 0 ; i < sz(pares) ; i++ )
upd( pares[i].ss , -1 ) ;
for(auto e : adj[cn])
if(!marc[e.ff]) decompose(e.ff) ;
}
int main()
{
scanf("%d %d", &N, &K ) ;
for(int i = 1 ; i <= N ; i++ )
{
dsu[i] = i ;
qtd[i] = 1 ;
}
for(int i = 0, x , y , w ; i < N-1 ; i++ )
{
scanf("%d %d %d", &x, &y, &w ) ;
adj[x].pb(mk(y,i)) ;
adj[y].pb(mk(x,i) );
edges.pb(mkt(w, x, y,i)) ;
}
sort(all(edges)) ;
for(int i = 0 , x , y , w ; i < N-1; i++ )
{
w = get<0>(edges[i]) ;
x = get<1>(edges[i]) ;
y = get<2>(edges[i]) ;
join(x,y,w, (i+1)*2 ) ;
weight[get<3>(edges[i])] = (i+1)*2 ;
}
decompose(1) ;
printf("%lld\n" , ans*2LL ) ;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:231:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
231 | scanf("%d %d", &N, &K ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~
Main.cpp:241:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
241 | scanf("%d %d %d", &x, &y, &w ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
8 ms |
7628 KB |
Output is correct |
5 |
Correct |
8 ms |
7640 KB |
Output is correct |
6 |
Correct |
8 ms |
7664 KB |
Output is correct |
7 |
Correct |
7 ms |
7520 KB |
Output is correct |
8 |
Correct |
7 ms |
7628 KB |
Output is correct |
9 |
Correct |
8 ms |
7580 KB |
Output is correct |
10 |
Correct |
6 ms |
7500 KB |
Output is correct |
11 |
Correct |
6 ms |
7500 KB |
Output is correct |
12 |
Correct |
6 ms |
7500 KB |
Output is correct |
13 |
Correct |
9 ms |
7500 KB |
Output is correct |
14 |
Correct |
7 ms |
7508 KB |
Output is correct |
15 |
Correct |
8 ms |
7500 KB |
Output is correct |
16 |
Correct |
8 ms |
7500 KB |
Output is correct |
17 |
Correct |
6 ms |
7500 KB |
Output is correct |
18 |
Correct |
7 ms |
7500 KB |
Output is correct |
19 |
Correct |
8 ms |
7500 KB |
Output is correct |
20 |
Correct |
6 ms |
7500 KB |
Output is correct |
21 |
Correct |
7 ms |
7500 KB |
Output is correct |
22 |
Correct |
7 ms |
7500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
9 ms |
7628 KB |
Output is correct |
5 |
Correct |
48 ms |
9828 KB |
Output is correct |
6 |
Correct |
307 ms |
21736 KB |
Output is correct |
7 |
Correct |
515 ms |
31764 KB |
Output is correct |
8 |
Correct |
860 ms |
35000 KB |
Output is correct |
9 |
Correct |
537 ms |
31364 KB |
Output is correct |
10 |
Correct |
594 ms |
35872 KB |
Output is correct |
11 |
Correct |
526 ms |
31688 KB |
Output is correct |
12 |
Correct |
817 ms |
34864 KB |
Output is correct |
13 |
Correct |
530 ms |
31620 KB |
Output is correct |
14 |
Correct |
581 ms |
35832 KB |
Output is correct |
15 |
Correct |
595 ms |
34324 KB |
Output is correct |
16 |
Correct |
684 ms |
36400 KB |
Output is correct |
17 |
Correct |
712 ms |
36736 KB |
Output is correct |
18 |
Correct |
724 ms |
36700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
8 ms |
7628 KB |
Output is correct |
5 |
Correct |
8 ms |
7640 KB |
Output is correct |
6 |
Correct |
8 ms |
7664 KB |
Output is correct |
7 |
Correct |
7 ms |
7520 KB |
Output is correct |
8 |
Correct |
7 ms |
7628 KB |
Output is correct |
9 |
Correct |
8 ms |
7580 KB |
Output is correct |
10 |
Correct |
6 ms |
7500 KB |
Output is correct |
11 |
Correct |
6 ms |
7500 KB |
Output is correct |
12 |
Correct |
6 ms |
7500 KB |
Output is correct |
13 |
Correct |
9 ms |
7500 KB |
Output is correct |
14 |
Correct |
7 ms |
7508 KB |
Output is correct |
15 |
Correct |
8 ms |
7500 KB |
Output is correct |
16 |
Correct |
8 ms |
7500 KB |
Output is correct |
17 |
Correct |
6 ms |
7500 KB |
Output is correct |
18 |
Correct |
7 ms |
7500 KB |
Output is correct |
19 |
Correct |
8 ms |
7500 KB |
Output is correct |
20 |
Correct |
6 ms |
7500 KB |
Output is correct |
21 |
Correct |
7 ms |
7500 KB |
Output is correct |
22 |
Correct |
7 ms |
7500 KB |
Output is correct |
23 |
Correct |
5 ms |
7372 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
4 ms |
7372 KB |
Output is correct |
26 |
Correct |
9 ms |
7628 KB |
Output is correct |
27 |
Correct |
48 ms |
9828 KB |
Output is correct |
28 |
Correct |
307 ms |
21736 KB |
Output is correct |
29 |
Correct |
515 ms |
31764 KB |
Output is correct |
30 |
Correct |
860 ms |
35000 KB |
Output is correct |
31 |
Correct |
537 ms |
31364 KB |
Output is correct |
32 |
Correct |
594 ms |
35872 KB |
Output is correct |
33 |
Correct |
526 ms |
31688 KB |
Output is correct |
34 |
Correct |
817 ms |
34864 KB |
Output is correct |
35 |
Correct |
530 ms |
31620 KB |
Output is correct |
36 |
Correct |
581 ms |
35832 KB |
Output is correct |
37 |
Correct |
595 ms |
34324 KB |
Output is correct |
38 |
Correct |
684 ms |
36400 KB |
Output is correct |
39 |
Correct |
712 ms |
36736 KB |
Output is correct |
40 |
Correct |
724 ms |
36700 KB |
Output is correct |
41 |
Correct |
4 ms |
7372 KB |
Output is correct |
42 |
Correct |
519 ms |
31764 KB |
Output is correct |
43 |
Correct |
846 ms |
35220 KB |
Output is correct |
44 |
Correct |
549 ms |
31380 KB |
Output is correct |
45 |
Correct |
604 ms |
35912 KB |
Output is correct |
46 |
Correct |
522 ms |
31812 KB |
Output is correct |
47 |
Correct |
894 ms |
35008 KB |
Output is correct |
48 |
Correct |
518 ms |
31484 KB |
Output is correct |
49 |
Correct |
599 ms |
35912 KB |
Output is correct |
50 |
Correct |
598 ms |
34356 KB |
Output is correct |
51 |
Correct |
711 ms |
36172 KB |
Output is correct |
52 |
Correct |
273 ms |
25136 KB |
Output is correct |
53 |
Correct |
369 ms |
28552 KB |
Output is correct |
54 |
Correct |
285 ms |
26160 KB |
Output is correct |
55 |
Correct |
369 ms |
28600 KB |
Output is correct |
56 |
Correct |
374 ms |
27572 KB |
Output is correct |
57 |
Correct |
541 ms |
24248 KB |
Output is correct |
58 |
Correct |
657 ms |
28212 KB |
Output is correct |
59 |
Correct |
1369 ms |
27952 KB |
Output is correct |
60 |
Correct |
658 ms |
27448 KB |
Output is correct |
61 |
Correct |
682 ms |
27696 KB |
Output is correct |
62 |
Correct |
454 ms |
24188 KB |
Output is correct |
63 |
Correct |
613 ms |
28136 KB |
Output is correct |
64 |
Correct |
642 ms |
27060 KB |
Output is correct |
65 |
Correct |
23 ms |
8340 KB |
Output is correct |
66 |
Correct |
6 ms |
7372 KB |
Output is correct |