#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
int lowbit(int x){return x&-x;}
int bit[1000006];
stack<int>stk;
void upd(int pl,int v){
while(pl<=1000000){
bit[pl]+=v;
pl+=lowbit(pl);
}
}
int qsum(int pl){
int ret=0;
while(pl){
ret+=bit[pl];
pl-=lowbit(pl);
}
return ret;
}
int n;int K;
vector<pair<int,int>>e[500005];
int psmx[500005];
int dph[500005];
int vis[500005];
int sbsz[500005];
int fa[500005];
void dfssz(int nw,int pa){
sbsz[nw]=1;
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i].first;
if(v==pa||vis[v]){continue;}
dfssz(v,nw);
sbsz[nw]+=sbsz[v];
}
}
void fincen(int nw,int pa,int &cen,int tot){
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i].first;
if(v==pa||vis[v]){continue;}
fincen(v,nw,cen,tot);
}
if(cen==0&&sbsz[nw]*2>=tot){
cen=nw;
}
}
int ans;
void dfsmx(int nw,int pa){
fa[nw]=pa;
dph[nw]=dph[pa]+1;
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i].first;int w=e[nw][i].second;
if(v==pa||vis[v]){continue;}
w=max(w,psmx[nw]);
psmx[v]=w;
dfsmx(v,nw);
}
}
void solve(int rt){
int cen=0;
dfssz(rt,0);
fincen(rt,0,cen,sbsz[rt]);
psmx[cen]=-1e9;
dfsmx(cen,0);
//cout<<cen<<endl;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
for(int i=0;i<e[cen].size();i++){
int v=e[cen][i].first;
if(vis[v]){continue;}
pq.push({psmx[v],v});
}
//upd(1,1);
while(pq.size()){
int nw=pq.top().second;
// cout<<nw<<endl;
pq.pop();
int cal=psmx[nw]-(dph[nw]-1)-K;
if(cal==0){ans++;}
if(cal>0){
ans++;
ans+=qsum(cal+1);
// ans-=min(cal,max(0ll,dph[nw]-2ll));
}
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i].first;
if(v==fa[nw]||vis[v]){continue;}
pq.push({psmx[v],v});
}
upd(dph[nw],1);
stk.push(dph[nw]);
}
while(stk.size()){
upd(stk.top(),-1);
stk.pop();
}
for(int i=0;i<e[cen].size();i++){
int vv=e[cen][i].first;
if(vis[vv]){continue;}
pq.push({psmx[vv],vv});
while(pq.size()){
int nw=pq.top().second;
// cout<<nw<<endl;
pq.pop();
int cal=psmx[nw]-(dph[nw]-1)-K;
//if(cal==0){ans++;}
if(cal>0){
//ans++;
ans-=qsum(cal+1);
// ans-=min(cal,max(0ll,dph[nw]-2ll));
}
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i].first;
if(v==fa[nw]||vis[v]){continue;}
pq.push({psmx[v],v});
}
upd(dph[nw],1);
stk.push(dph[nw]);
}
while(stk.size()){
upd(stk.top(),-1);
stk.pop();
}
}
vis[cen]=1;
//cout<<cen<<endl;
for(int i=0;i<e[cen].size();i++){
int v=e[cen][i].first;
if(vis[v]){continue;}
solve(v);
}
}
signed main(){
cin>>n>>K;
for(int i=1;i<=n-1;i++){
int a;int b;int c;
cin>>a>>b>>c;
e[a].push_back({b,c});
e[b].push_back({a,c});
}
ans=0;
solve(1);
cout<<ans*2<<endl;
}
Compilation message
Main.cpp: In function 'void dfssz(long long int, long long int)':
Main.cpp:33:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
Main.cpp: In function 'void fincen(long long int, long long int, long long int&, long long int)':
Main.cpp:41:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
Main.cpp: In function 'void dfsmx(long long int, long long int)':
Main.cpp:54:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
Main.cpp: In function 'void solve(long long int)':
Main.cpp:72:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i=0;i<e[cen].size();i++){
| ~^~~~~~~~~~~~~~
Main.cpp:91:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
Main.cpp:104:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i=0;i<e[cen].size();i++){
| ~^~~~~~~~~~~~~~
Main.cpp:121:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
Main.cpp:139:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for(int i=0;i<e[cen].size();i++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12052 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12244 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
8 ms |
12244 KB |
Output is correct |
6 |
Correct |
8 ms |
12312 KB |
Output is correct |
7 |
Correct |
8 ms |
12328 KB |
Output is correct |
8 |
Correct |
8 ms |
12244 KB |
Output is correct |
9 |
Correct |
7 ms |
12216 KB |
Output is correct |
10 |
Correct |
8 ms |
12244 KB |
Output is correct |
11 |
Correct |
7 ms |
12244 KB |
Output is correct |
12 |
Correct |
8 ms |
12244 KB |
Output is correct |
13 |
Correct |
8 ms |
12244 KB |
Output is correct |
14 |
Correct |
8 ms |
12192 KB |
Output is correct |
15 |
Correct |
7 ms |
12244 KB |
Output is correct |
16 |
Correct |
8 ms |
12180 KB |
Output is correct |
17 |
Correct |
8 ms |
12244 KB |
Output is correct |
18 |
Correct |
8 ms |
12192 KB |
Output is correct |
19 |
Correct |
8 ms |
12196 KB |
Output is correct |
20 |
Correct |
9 ms |
12192 KB |
Output is correct |
21 |
Correct |
7 ms |
12244 KB |
Output is correct |
22 |
Correct |
10 ms |
12372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
12056 KB |
Output is correct |
3 |
Correct |
6 ms |
12116 KB |
Output is correct |
4 |
Correct |
8 ms |
12244 KB |
Output is correct |
5 |
Correct |
41 ms |
13952 KB |
Output is correct |
6 |
Correct |
208 ms |
20980 KB |
Output is correct |
7 |
Correct |
403 ms |
29644 KB |
Output is correct |
8 |
Correct |
430 ms |
29628 KB |
Output is correct |
9 |
Correct |
407 ms |
29540 KB |
Output is correct |
10 |
Correct |
427 ms |
29644 KB |
Output is correct |
11 |
Correct |
416 ms |
29672 KB |
Output is correct |
12 |
Correct |
426 ms |
29608 KB |
Output is correct |
13 |
Correct |
411 ms |
29576 KB |
Output is correct |
14 |
Correct |
426 ms |
29808 KB |
Output is correct |
15 |
Correct |
424 ms |
29624 KB |
Output is correct |
16 |
Correct |
429 ms |
29872 KB |
Output is correct |
17 |
Correct |
430 ms |
29544 KB |
Output is correct |
18 |
Correct |
430 ms |
29612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12052 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12244 KB |
Output is correct |
4 |
Correct |
9 ms |
12244 KB |
Output is correct |
5 |
Correct |
8 ms |
12244 KB |
Output is correct |
6 |
Correct |
8 ms |
12312 KB |
Output is correct |
7 |
Correct |
8 ms |
12328 KB |
Output is correct |
8 |
Correct |
8 ms |
12244 KB |
Output is correct |
9 |
Correct |
7 ms |
12216 KB |
Output is correct |
10 |
Correct |
8 ms |
12244 KB |
Output is correct |
11 |
Correct |
7 ms |
12244 KB |
Output is correct |
12 |
Correct |
8 ms |
12244 KB |
Output is correct |
13 |
Correct |
8 ms |
12244 KB |
Output is correct |
14 |
Correct |
8 ms |
12192 KB |
Output is correct |
15 |
Correct |
7 ms |
12244 KB |
Output is correct |
16 |
Correct |
8 ms |
12180 KB |
Output is correct |
17 |
Correct |
8 ms |
12244 KB |
Output is correct |
18 |
Correct |
8 ms |
12192 KB |
Output is correct |
19 |
Correct |
8 ms |
12196 KB |
Output is correct |
20 |
Correct |
9 ms |
12192 KB |
Output is correct |
21 |
Correct |
7 ms |
12244 KB |
Output is correct |
22 |
Correct |
10 ms |
12372 KB |
Output is correct |
23 |
Correct |
5 ms |
11988 KB |
Output is correct |
24 |
Correct |
5 ms |
12056 KB |
Output is correct |
25 |
Correct |
6 ms |
12116 KB |
Output is correct |
26 |
Correct |
8 ms |
12244 KB |
Output is correct |
27 |
Correct |
41 ms |
13952 KB |
Output is correct |
28 |
Correct |
208 ms |
20980 KB |
Output is correct |
29 |
Correct |
403 ms |
29644 KB |
Output is correct |
30 |
Correct |
430 ms |
29628 KB |
Output is correct |
31 |
Correct |
407 ms |
29540 KB |
Output is correct |
32 |
Correct |
427 ms |
29644 KB |
Output is correct |
33 |
Correct |
416 ms |
29672 KB |
Output is correct |
34 |
Correct |
426 ms |
29608 KB |
Output is correct |
35 |
Correct |
411 ms |
29576 KB |
Output is correct |
36 |
Correct |
426 ms |
29808 KB |
Output is correct |
37 |
Correct |
424 ms |
29624 KB |
Output is correct |
38 |
Correct |
429 ms |
29872 KB |
Output is correct |
39 |
Correct |
430 ms |
29544 KB |
Output is correct |
40 |
Correct |
430 ms |
29612 KB |
Output is correct |
41 |
Correct |
5 ms |
11988 KB |
Output is correct |
42 |
Correct |
406 ms |
30800 KB |
Output is correct |
43 |
Correct |
425 ms |
31352 KB |
Output is correct |
44 |
Correct |
408 ms |
31056 KB |
Output is correct |
45 |
Correct |
426 ms |
31228 KB |
Output is correct |
46 |
Correct |
406 ms |
30864 KB |
Output is correct |
47 |
Correct |
438 ms |
31316 KB |
Output is correct |
48 |
Correct |
412 ms |
31152 KB |
Output is correct |
49 |
Correct |
427 ms |
31172 KB |
Output is correct |
50 |
Correct |
431 ms |
31116 KB |
Output is correct |
51 |
Correct |
426 ms |
31212 KB |
Output is correct |
52 |
Correct |
248 ms |
23728 KB |
Output is correct |
53 |
Correct |
249 ms |
24116 KB |
Output is correct |
54 |
Correct |
218 ms |
24120 KB |
Output is correct |
55 |
Correct |
262 ms |
23924 KB |
Output is correct |
56 |
Correct |
257 ms |
23868 KB |
Output is correct |
57 |
Correct |
502 ms |
23296 KB |
Output is correct |
58 |
Correct |
497 ms |
23700 KB |
Output is correct |
59 |
Correct |
517 ms |
24248 KB |
Output is correct |
60 |
Correct |
516 ms |
23592 KB |
Output is correct |
61 |
Correct |
524 ms |
23880 KB |
Output is correct |
62 |
Correct |
429 ms |
23272 KB |
Output is correct |
63 |
Correct |
463 ms |
23872 KB |
Output is correct |
64 |
Correct |
472 ms |
23576 KB |
Output is correct |
65 |
Correct |
21 ms |
12628 KB |
Output is correct |
66 |
Correct |
5 ms |
12116 KB |
Output is correct |