#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
int n;int K;
int uf[200005];
int usz[200005];
pair<int,pair<int,int>>e[200005];
int fin(int x){
if(uf[x]==x){return x;}
uf[x]=fin(uf[x]);
return uf[x];
}
void mrg(int a,int b){
int pa=fin(a);int pb=fin(b);
uf[pa]=pb;
usz[pb]+=usz[pa];
}
int ans;
vector<pair<int,int>>ee[200005];
int dph[200005];
void dfs(int nw,int pa,int mxv){
if(pa&&mxv-dph[nw]>=K){
ans++;
}
for(int i=0;i<ee[nw].size();i++){
int v=ee[nw][i].first;
if(v==pa){continue;}
int w=ee[nw][i].second;
dph[v]=dph[nw]+1;
dfs(v,nw,max(mxv,w) );
}
}
void solve(){
ans=0;
for(int i=1;i<=n;i++){
dph[i]=0;
dfs(i,0,0);
}
cout<<ans<<endl;
exit(0);
}
signed main(){
cin>>n>>K;
for(int i=1;i<=n-1;i++){
cin>>e[i].second.first>>e[i].second.second>>e[i].first;
int a=e[i].second.first;int b=e[i].second.second;int c=e[i].first;
ee[a].push_back({b,c});
ee[b].push_back({a,c});
}
if(n<=1000){
solve();
}
sort(e+1,e+n-1+1);
for(int i=1;i<=n;i++){
uf[i]=i;
usz[i]=1;
}
ans=0;
for(int i=1;i<=n-1;i++){
int W=e[i].first;
//L<=W-K
int a=e[i].second.first;int b=e[i].second.second;
a=fin(a);b=fin(b);
if(usz[a]>usz[b]){
swap(a,b);
}
for(int j=1;j<=usz[a];j++){
ans+=max(0ll,min((W-K)-j+1ll,usz[b]));
}
mrg(a,b);
// cout<<W<<" "<<ans<<endl;
}
cout<<ans*2<<endl;
}
Compilation message
Main.cpp: In function 'void dfs(long long int, long long int, long long int)':
Main.cpp:27: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]
27 | for(int i=0;i<ee[nw].size();i++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
5 ms |
4912 KB |
Output is correct |
3 |
Correct |
4 ms |
4892 KB |
Output is correct |
4 |
Correct |
10 ms |
5140 KB |
Output is correct |
5 |
Correct |
10 ms |
5076 KB |
Output is correct |
6 |
Correct |
10 ms |
5080 KB |
Output is correct |
7 |
Correct |
10 ms |
5144 KB |
Output is correct |
8 |
Correct |
12 ms |
5244 KB |
Output is correct |
9 |
Correct |
10 ms |
4992 KB |
Output is correct |
10 |
Correct |
11 ms |
5076 KB |
Output is correct |
11 |
Correct |
10 ms |
5084 KB |
Output is correct |
12 |
Correct |
10 ms |
5076 KB |
Output is correct |
13 |
Correct |
10 ms |
5076 KB |
Output is correct |
14 |
Correct |
10 ms |
5092 KB |
Output is correct |
15 |
Correct |
11 ms |
5084 KB |
Output is correct |
16 |
Correct |
10 ms |
5088 KB |
Output is correct |
17 |
Correct |
10 ms |
5084 KB |
Output is correct |
18 |
Correct |
10 ms |
5084 KB |
Output is correct |
19 |
Correct |
11 ms |
5076 KB |
Output is correct |
20 |
Correct |
10 ms |
5076 KB |
Output is correct |
21 |
Correct |
11 ms |
5084 KB |
Output is correct |
22 |
Correct |
11 ms |
5024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
10 ms |
5144 KB |
Output is correct |
5 |
Correct |
13 ms |
5844 KB |
Output is correct |
6 |
Correct |
41 ms |
10060 KB |
Output is correct |
7 |
Correct |
63 ms |
14952 KB |
Output is correct |
8 |
Correct |
81 ms |
15416 KB |
Output is correct |
9 |
Correct |
76 ms |
14924 KB |
Output is correct |
10 |
Correct |
79 ms |
15288 KB |
Output is correct |
11 |
Correct |
76 ms |
14888 KB |
Output is correct |
12 |
Correct |
76 ms |
15384 KB |
Output is correct |
13 |
Correct |
92 ms |
15040 KB |
Output is correct |
14 |
Correct |
80 ms |
15260 KB |
Output is correct |
15 |
Correct |
76 ms |
15080 KB |
Output is correct |
16 |
Correct |
79 ms |
15344 KB |
Output is correct |
17 |
Correct |
80 ms |
15308 KB |
Output is correct |
18 |
Correct |
78 ms |
15192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
5 ms |
4912 KB |
Output is correct |
3 |
Correct |
4 ms |
4892 KB |
Output is correct |
4 |
Correct |
10 ms |
5140 KB |
Output is correct |
5 |
Correct |
10 ms |
5076 KB |
Output is correct |
6 |
Correct |
10 ms |
5080 KB |
Output is correct |
7 |
Correct |
10 ms |
5144 KB |
Output is correct |
8 |
Correct |
12 ms |
5244 KB |
Output is correct |
9 |
Correct |
10 ms |
4992 KB |
Output is correct |
10 |
Correct |
11 ms |
5076 KB |
Output is correct |
11 |
Correct |
10 ms |
5084 KB |
Output is correct |
12 |
Correct |
10 ms |
5076 KB |
Output is correct |
13 |
Correct |
10 ms |
5076 KB |
Output is correct |
14 |
Correct |
10 ms |
5092 KB |
Output is correct |
15 |
Correct |
11 ms |
5084 KB |
Output is correct |
16 |
Correct |
10 ms |
5088 KB |
Output is correct |
17 |
Correct |
10 ms |
5084 KB |
Output is correct |
18 |
Correct |
10 ms |
5084 KB |
Output is correct |
19 |
Correct |
11 ms |
5076 KB |
Output is correct |
20 |
Correct |
10 ms |
5076 KB |
Output is correct |
21 |
Correct |
11 ms |
5084 KB |
Output is correct |
22 |
Correct |
11 ms |
5024 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
10 ms |
5144 KB |
Output is correct |
27 |
Correct |
13 ms |
5844 KB |
Output is correct |
28 |
Correct |
41 ms |
10060 KB |
Output is correct |
29 |
Correct |
63 ms |
14952 KB |
Output is correct |
30 |
Correct |
81 ms |
15416 KB |
Output is correct |
31 |
Correct |
76 ms |
14924 KB |
Output is correct |
32 |
Correct |
79 ms |
15288 KB |
Output is correct |
33 |
Correct |
76 ms |
14888 KB |
Output is correct |
34 |
Correct |
76 ms |
15384 KB |
Output is correct |
35 |
Correct |
92 ms |
15040 KB |
Output is correct |
36 |
Correct |
80 ms |
15260 KB |
Output is correct |
37 |
Correct |
76 ms |
15080 KB |
Output is correct |
38 |
Correct |
79 ms |
15344 KB |
Output is correct |
39 |
Correct |
80 ms |
15308 KB |
Output is correct |
40 |
Correct |
78 ms |
15192 KB |
Output is correct |
41 |
Correct |
3 ms |
4948 KB |
Output is correct |
42 |
Correct |
81 ms |
14852 KB |
Output is correct |
43 |
Correct |
86 ms |
15444 KB |
Output is correct |
44 |
Correct |
76 ms |
14988 KB |
Output is correct |
45 |
Correct |
83 ms |
15308 KB |
Output is correct |
46 |
Correct |
92 ms |
14864 KB |
Output is correct |
47 |
Correct |
79 ms |
15444 KB |
Output is correct |
48 |
Correct |
73 ms |
14956 KB |
Output is correct |
49 |
Correct |
95 ms |
15288 KB |
Output is correct |
50 |
Correct |
96 ms |
15180 KB |
Output is correct |
51 |
Correct |
78 ms |
15244 KB |
Output is correct |
52 |
Correct |
75 ms |
15556 KB |
Output is correct |
53 |
Incorrect |
90 ms |
15876 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |