//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
llo n,k;
vector<pair<llo,llo>> adj[100001];
llo vis[100001];
llo cot[100001];
llo ss[100001];
void dfs(llo no,llo parr=-1){
ss[no]=1;
for(auto j:adj[no]){
if(vis[j.a]==0){
if(j.a!=parr){
dfs(j.a,no);
ss[no]+=ss[j.a];
}
}
}
}
llo pp;
llo find(llo no,llo parr=-1){
for(auto j:adj[no]){
if(vis[j.a]==0){
if(j.a!=parr){
if(ss[j.a]>ss[pp]/2){
// cout<<no<<",,"<<j.a<<endl;
return find(j.a,no);
}
}
}
}
return no;
}
llo lev[100001];
llo par[100001];
vector<llo> tree[100001];
void u(llo ind,llo i,llo j){
while(i<tree[ind].size()){
tree[ind][i]+=j;
i+=(i&-i);
}
}
llo ss2(llo ind,llo i){
llo su=0;
i=min(i,(llo)(tree[ind].size())-1);
while(i>0){
su+=tree[ind][i];
i-=(i&-i);
}
return su;
}
vector<llo> zz;
void dfs2(llo no,llo parr=-1,llo levv=0,llo xx=-1){
if(levv==1){
xx=no;
zz.pb(no);
}
par[no]=xx;
lev[no]=levv;
ss[no]=1;
for(auto j:adj[no]){
if(vis[j.a]==0){
if(j.a!=parr){
dfs2(j.a,no,levv+1,xx);
ss[no]+=ss[j.a];
}
}
}
}
llo ans=0;
void dec(llo no){
pp=no;
dfs(no);
/*for(int i=0;i<n;i++){
cout<<ss[i]<<",";
}
cout<<endl;*/
llo cen=find(no);
dfs2(cen);
tree[0].clear();
for(llo i=0;i<=ss[cen];i++){
tree[0].pb(0);
}
for(auto j:zz){
tree[j+1].clear();
for(llo m=0;m<=ss[j];m++){
tree[j+1].pb(0);
}
}
//cout<<cen<<":"<<endl;
//return ;
priority_queue<pair<llo,llo>> xx;
xx.push({0,cen});
while(xx.size()){
pair<llo,llo> no=xx.top();
xx.pop();
no.a=-no.a;
//cout<<no.a<<","<<no.b<<endl;
for(auto j:adj[no.b]){
if(vis[j.a]==0 and lev[j.a]>lev[no.b]){
xx.push({-max(no.a,j.b),j.a});
}
}
llo cur=no.a-k-lev[no.b];
if(cur>=0){
ans+=ss2(0,cur+1);
if(no.b!=cen){
ans-=(ss2(par[no.b]+1,cur+1));
}
}
u(0,lev[no.b]+1,1);
if(no.b!=cen){
u(par[no.b]+1,lev[no.b]+1,1);
}
}
// cout<<cen<<":"<<ans<<endl;
zz.clear();
vis[cen]=1;
for(auto j:adj[cen]){
if(vis[j.a]==0){
dec(j.a);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>k;
for(llo i=0;i<n-1;i++){
llo aa,bb,cc;
cin>>aa>>bb>>cc;
aa--;
bb--;
adj[aa].pb({bb,cc});
adj[bb].pb({aa,cc});
}
dec(0);
ans*=2;
cout<<ans<<endl;
return 0;
}
Compilation message
Main.cpp: In function 'void u(llo, llo, llo)':
Main.cpp:45:9: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | while(i<tree[ind].size()){
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5228 KB |
Output is correct |
5 |
Correct |
5 ms |
5228 KB |
Output is correct |
6 |
Correct |
5 ms |
5228 KB |
Output is correct |
7 |
Correct |
5 ms |
5228 KB |
Output is correct |
8 |
Correct |
5 ms |
5228 KB |
Output is correct |
9 |
Correct |
5 ms |
5228 KB |
Output is correct |
10 |
Correct |
5 ms |
5228 KB |
Output is correct |
11 |
Correct |
5 ms |
5228 KB |
Output is correct |
12 |
Correct |
5 ms |
5228 KB |
Output is correct |
13 |
Correct |
5 ms |
5228 KB |
Output is correct |
14 |
Correct |
6 ms |
5228 KB |
Output is correct |
15 |
Correct |
5 ms |
5228 KB |
Output is correct |
16 |
Correct |
5 ms |
5228 KB |
Output is correct |
17 |
Correct |
5 ms |
5228 KB |
Output is correct |
18 |
Correct |
6 ms |
5228 KB |
Output is correct |
19 |
Correct |
5 ms |
5228 KB |
Output is correct |
20 |
Correct |
5 ms |
5228 KB |
Output is correct |
21 |
Correct |
5 ms |
5228 KB |
Output is correct |
22 |
Correct |
5 ms |
5228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5228 KB |
Output is correct |
5 |
Correct |
21 ms |
8172 KB |
Output is correct |
6 |
Correct |
103 ms |
20836 KB |
Output is correct |
7 |
Correct |
192 ms |
37088 KB |
Output is correct |
8 |
Correct |
215 ms |
37344 KB |
Output is correct |
9 |
Correct |
194 ms |
36960 KB |
Output is correct |
10 |
Correct |
216 ms |
37344 KB |
Output is correct |
11 |
Correct |
190 ms |
36988 KB |
Output is correct |
12 |
Correct |
214 ms |
37344 KB |
Output is correct |
13 |
Correct |
202 ms |
37088 KB |
Output is correct |
14 |
Correct |
213 ms |
37216 KB |
Output is correct |
15 |
Correct |
215 ms |
37088 KB |
Output is correct |
16 |
Correct |
216 ms |
37216 KB |
Output is correct |
17 |
Correct |
218 ms |
37216 KB |
Output is correct |
18 |
Correct |
211 ms |
37216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5228 KB |
Output is correct |
5 |
Correct |
5 ms |
5228 KB |
Output is correct |
6 |
Correct |
5 ms |
5228 KB |
Output is correct |
7 |
Correct |
5 ms |
5228 KB |
Output is correct |
8 |
Correct |
5 ms |
5228 KB |
Output is correct |
9 |
Correct |
5 ms |
5228 KB |
Output is correct |
10 |
Correct |
5 ms |
5228 KB |
Output is correct |
11 |
Correct |
5 ms |
5228 KB |
Output is correct |
12 |
Correct |
5 ms |
5228 KB |
Output is correct |
13 |
Correct |
5 ms |
5228 KB |
Output is correct |
14 |
Correct |
6 ms |
5228 KB |
Output is correct |
15 |
Correct |
5 ms |
5228 KB |
Output is correct |
16 |
Correct |
5 ms |
5228 KB |
Output is correct |
17 |
Correct |
5 ms |
5228 KB |
Output is correct |
18 |
Correct |
6 ms |
5228 KB |
Output is correct |
19 |
Correct |
5 ms |
5228 KB |
Output is correct |
20 |
Correct |
5 ms |
5228 KB |
Output is correct |
21 |
Correct |
5 ms |
5228 KB |
Output is correct |
22 |
Correct |
5 ms |
5228 KB |
Output is correct |
23 |
Correct |
4 ms |
5100 KB |
Output is correct |
24 |
Correct |
4 ms |
5100 KB |
Output is correct |
25 |
Correct |
4 ms |
5100 KB |
Output is correct |
26 |
Correct |
5 ms |
5228 KB |
Output is correct |
27 |
Correct |
21 ms |
8172 KB |
Output is correct |
28 |
Correct |
103 ms |
20836 KB |
Output is correct |
29 |
Correct |
192 ms |
37088 KB |
Output is correct |
30 |
Correct |
215 ms |
37344 KB |
Output is correct |
31 |
Correct |
194 ms |
36960 KB |
Output is correct |
32 |
Correct |
216 ms |
37344 KB |
Output is correct |
33 |
Correct |
190 ms |
36988 KB |
Output is correct |
34 |
Correct |
214 ms |
37344 KB |
Output is correct |
35 |
Correct |
202 ms |
37088 KB |
Output is correct |
36 |
Correct |
213 ms |
37216 KB |
Output is correct |
37 |
Correct |
215 ms |
37088 KB |
Output is correct |
38 |
Correct |
216 ms |
37216 KB |
Output is correct |
39 |
Correct |
218 ms |
37216 KB |
Output is correct |
40 |
Correct |
211 ms |
37216 KB |
Output is correct |
41 |
Correct |
4 ms |
5100 KB |
Output is correct |
42 |
Correct |
190 ms |
36832 KB |
Output is correct |
43 |
Correct |
211 ms |
37344 KB |
Output is correct |
44 |
Correct |
188 ms |
36960 KB |
Output is correct |
45 |
Correct |
215 ms |
37216 KB |
Output is correct |
46 |
Correct |
188 ms |
36832 KB |
Output is correct |
47 |
Correct |
227 ms |
37480 KB |
Output is correct |
48 |
Correct |
193 ms |
36960 KB |
Output is correct |
49 |
Correct |
248 ms |
37216 KB |
Output is correct |
50 |
Correct |
217 ms |
37216 KB |
Output is correct |
51 |
Correct |
216 ms |
37216 KB |
Output is correct |
52 |
Correct |
260 ms |
22112 KB |
Output is correct |
53 |
Correct |
232 ms |
22588 KB |
Output is correct |
54 |
Correct |
187 ms |
21856 KB |
Output is correct |
55 |
Correct |
229 ms |
22496 KB |
Output is correct |
56 |
Correct |
234 ms |
22368 KB |
Output is correct |
57 |
Correct |
419 ms |
26468 KB |
Output is correct |
58 |
Correct |
432 ms |
26088 KB |
Output is correct |
59 |
Correct |
389 ms |
26720 KB |
Output is correct |
60 |
Correct |
457 ms |
26968 KB |
Output is correct |
61 |
Correct |
469 ms |
26480 KB |
Output is correct |
62 |
Correct |
440 ms |
24568 KB |
Output is correct |
63 |
Correct |
431 ms |
25068 KB |
Output is correct |
64 |
Correct |
584 ms |
25444 KB |
Output is correct |
65 |
Correct |
21 ms |
5996 KB |
Output is correct |
66 |
Correct |
3 ms |
5100 KB |
Output is correct |