#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
const int N=1e5;
typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
vector<pair<int,int>> e[N+10];
bool vis[N+10];
int siz[N+10];
int dep[N+10];
int par[N+10];
int mx[N+10];
ordered_set os;
ordered_set st[N+10];
priority_queue<pair<int,int>> pq;
void dfs_size(int x,int p)
{
siz[x]=1;
for(auto v:e[x])
{
if(v.fi!=p && !vis[v.fi])
{
dfs_size(v.fi,x);
siz[x]+=siz[v.fi];
}
}
return;
}
int find_centro(int x,int p,int size_all)
{
for(auto v:e[x])
{
if(v.fi!=p && !vis[v.fi])
{
int tmp=find_centro(v.fi,x,size_all);
if(tmp!=-1)
return tmp;
}
}
if(2*siz[x]>=size_all)
return x;
return -1;
}
long long centro(int x,int k)
{
dfs_size(x,0);
x=find_centro(x,0,siz[x]);
dfs_size(x,0);
//cerr<<"centro "<<x<<"\n";
vis[x]=true;
dep[x]=0;
mx[x]=0;
os.clear();
while(!pq.empty())
pq.pop();
os.insert({0,x});
for(auto [v,c]:e[x])
{
if(!vis[v])
{
dep[v]=1;
mx[v]=c;
par[v]=v;
st[v].clear();
pq.emplace(-mx[v],v);
}
}
long long ans=0;
while(!pq.empty())
{
int y=pq.top().se;
pq.pop();
int d=mx[y]-dep[y]-k;
//cerr<<y<<" "<<d<<"\n";
ans+=os.order_of_key({d+1,0})-st[par[y]].order_of_key({d+1,0});
os.insert({dep[y],y});
st[par[y]].insert({dep[y],y});
for(auto [v,c]:e[y])
{
if(!vis[v] && siz[v]<siz[y])
{
mx[v]=max(mx[y],c);
dep[v]=dep[y]+1;
par[v]=par[y];
pq.emplace(-mx[v],v);
}
}
}
for(auto [v,c]:e[x])
{
if(!vis[v])
ans+=centro(v,k);
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,k;
cin>>n>>k;
for(int i=1;i<n;i++)
{
int a,b,c;
cin>>a>>b>>c;
e[a].emplace_back(b,c);
e[b].emplace_back(a,c);
}
cout<<2*centro(1,k)<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
11980 KB |
Output is correct |
2 |
Correct |
10 ms |
12088 KB |
Output is correct |
3 |
Correct |
10 ms |
12108 KB |
Output is correct |
4 |
Correct |
14 ms |
12468 KB |
Output is correct |
5 |
Correct |
13 ms |
12464 KB |
Output is correct |
6 |
Correct |
13 ms |
12468 KB |
Output is correct |
7 |
Correct |
15 ms |
12344 KB |
Output is correct |
8 |
Correct |
14 ms |
12376 KB |
Output is correct |
9 |
Correct |
16 ms |
12236 KB |
Output is correct |
10 |
Correct |
12 ms |
12332 KB |
Output is correct |
11 |
Correct |
12 ms |
12328 KB |
Output is correct |
12 |
Correct |
13 ms |
12328 KB |
Output is correct |
13 |
Correct |
13 ms |
12296 KB |
Output is correct |
14 |
Correct |
14 ms |
12296 KB |
Output is correct |
15 |
Correct |
13 ms |
12236 KB |
Output is correct |
16 |
Correct |
13 ms |
12236 KB |
Output is correct |
17 |
Correct |
13 ms |
12228 KB |
Output is correct |
18 |
Correct |
13 ms |
12204 KB |
Output is correct |
19 |
Correct |
13 ms |
12236 KB |
Output is correct |
20 |
Correct |
13 ms |
12204 KB |
Output is correct |
21 |
Correct |
13 ms |
12236 KB |
Output is correct |
22 |
Correct |
13 ms |
12312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11980 KB |
Output is correct |
2 |
Correct |
10 ms |
12080 KB |
Output is correct |
3 |
Correct |
10 ms |
12100 KB |
Output is correct |
4 |
Correct |
14 ms |
12464 KB |
Output is correct |
5 |
Correct |
64 ms |
14720 KB |
Output is correct |
6 |
Correct |
461 ms |
59556 KB |
Output is correct |
7 |
Correct |
981 ms |
113136 KB |
Output is correct |
8 |
Correct |
1093 ms |
113580 KB |
Output is correct |
9 |
Correct |
973 ms |
113172 KB |
Output is correct |
10 |
Correct |
1106 ms |
113604 KB |
Output is correct |
11 |
Correct |
969 ms |
113228 KB |
Output is correct |
12 |
Correct |
1079 ms |
113732 KB |
Output is correct |
13 |
Correct |
963 ms |
113476 KB |
Output is correct |
14 |
Correct |
1120 ms |
113552 KB |
Output is correct |
15 |
Correct |
1118 ms |
113464 KB |
Output is correct |
16 |
Correct |
1094 ms |
113504 KB |
Output is correct |
17 |
Correct |
1072 ms |
113620 KB |
Output is correct |
18 |
Correct |
1060 ms |
113348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
11980 KB |
Output is correct |
2 |
Correct |
10 ms |
12088 KB |
Output is correct |
3 |
Correct |
10 ms |
12108 KB |
Output is correct |
4 |
Correct |
14 ms |
12468 KB |
Output is correct |
5 |
Correct |
13 ms |
12464 KB |
Output is correct |
6 |
Correct |
13 ms |
12468 KB |
Output is correct |
7 |
Correct |
15 ms |
12344 KB |
Output is correct |
8 |
Correct |
14 ms |
12376 KB |
Output is correct |
9 |
Correct |
16 ms |
12236 KB |
Output is correct |
10 |
Correct |
12 ms |
12332 KB |
Output is correct |
11 |
Correct |
12 ms |
12328 KB |
Output is correct |
12 |
Correct |
13 ms |
12328 KB |
Output is correct |
13 |
Correct |
13 ms |
12296 KB |
Output is correct |
14 |
Correct |
14 ms |
12296 KB |
Output is correct |
15 |
Correct |
13 ms |
12236 KB |
Output is correct |
16 |
Correct |
13 ms |
12236 KB |
Output is correct |
17 |
Correct |
13 ms |
12228 KB |
Output is correct |
18 |
Correct |
13 ms |
12204 KB |
Output is correct |
19 |
Correct |
13 ms |
12236 KB |
Output is correct |
20 |
Correct |
13 ms |
12204 KB |
Output is correct |
21 |
Correct |
13 ms |
12236 KB |
Output is correct |
22 |
Correct |
13 ms |
12312 KB |
Output is correct |
23 |
Correct |
12 ms |
11980 KB |
Output is correct |
24 |
Correct |
10 ms |
12080 KB |
Output is correct |
25 |
Correct |
10 ms |
12100 KB |
Output is correct |
26 |
Correct |
14 ms |
12464 KB |
Output is correct |
27 |
Correct |
64 ms |
14720 KB |
Output is correct |
28 |
Correct |
461 ms |
59556 KB |
Output is correct |
29 |
Correct |
981 ms |
113136 KB |
Output is correct |
30 |
Correct |
1093 ms |
113580 KB |
Output is correct |
31 |
Correct |
973 ms |
113172 KB |
Output is correct |
32 |
Correct |
1106 ms |
113604 KB |
Output is correct |
33 |
Correct |
969 ms |
113228 KB |
Output is correct |
34 |
Correct |
1079 ms |
113732 KB |
Output is correct |
35 |
Correct |
963 ms |
113476 KB |
Output is correct |
36 |
Correct |
1120 ms |
113552 KB |
Output is correct |
37 |
Correct |
1118 ms |
113464 KB |
Output is correct |
38 |
Correct |
1094 ms |
113504 KB |
Output is correct |
39 |
Correct |
1072 ms |
113620 KB |
Output is correct |
40 |
Correct |
1060 ms |
113348 KB |
Output is correct |
41 |
Correct |
10 ms |
12068 KB |
Output is correct |
42 |
Correct |
945 ms |
113152 KB |
Output is correct |
43 |
Correct |
1100 ms |
113672 KB |
Output is correct |
44 |
Correct |
974 ms |
113220 KB |
Output is correct |
45 |
Correct |
1127 ms |
113716 KB |
Output is correct |
46 |
Correct |
956 ms |
113192 KB |
Output is correct |
47 |
Correct |
1104 ms |
113584 KB |
Output is correct |
48 |
Correct |
970 ms |
113260 KB |
Output is correct |
49 |
Correct |
1116 ms |
113540 KB |
Output is correct |
50 |
Correct |
1114 ms |
113416 KB |
Output is correct |
51 |
Correct |
1071 ms |
113492 KB |
Output is correct |
52 |
Correct |
567 ms |
43556 KB |
Output is correct |
53 |
Correct |
527 ms |
43972 KB |
Output is correct |
54 |
Correct |
518 ms |
43332 KB |
Output is correct |
55 |
Correct |
536 ms |
43844 KB |
Output is correct |
56 |
Correct |
634 ms |
43844 KB |
Output is correct |
57 |
Correct |
1178 ms |
38240 KB |
Output is correct |
58 |
Correct |
1024 ms |
40776 KB |
Output is correct |
59 |
Correct |
1102 ms |
38840 KB |
Output is correct |
60 |
Correct |
1136 ms |
38340 KB |
Output is correct |
61 |
Correct |
1214 ms |
40036 KB |
Output is correct |
62 |
Correct |
906 ms |
39744 KB |
Output is correct |
63 |
Correct |
995 ms |
43052 KB |
Output is correct |
64 |
Correct |
1036 ms |
44508 KB |
Output is correct |
65 |
Correct |
32 ms |
13136 KB |
Output is correct |
66 |
Correct |
10 ms |
12072 KB |
Output is correct |