#include<bits/stdc++.h>
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout<<"\n";
#define int long long
#define inf (int)1e18
#define pii pair<int,int>
#define vii vector<pii>
#define lb(x) x&-x
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(1<<j))
#define vi vector<int>
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
struct BIT
{
int t[maxn];
void up(int x,int val)
{
for( ; x < maxn ; x += lb(x)) t[x] += val;
}
int get(int x)
{
int ans = 0;
for( ; x ; x -= lb(x)) ans += t[x];
return ans;
}
}t;
int n,k;
vii ke[maxn];
int ans;
// centroid decomposition
bool is_remove[maxn];
int tot = 0;
int sz[maxn];
int dfs_size(int u,int parent = -1)
{
sz[u] = 1;
for(auto [v,w] : ke[u])
{
if(v == parent or is_remove[v]) continue;
sz[u] += dfs_size(v,u);
}
return sz[u];
}
int dfs_centroid(int u,int tree_size,int parent = 0)
{
for(auto [v,w] : ke[u])
{
if(v == parent or is_remove[v]) continue;
if(2 * sz[v] > tree_size) return dfs_centroid(v,tree_size,u);
}
return u;
}
void dfs_dist(int u,int par,int update,int dist,int maxx)
{
for(auto [v,w] : ke[u])
{
if(v == par or is_remove[v]) continue;
dfs_dist(v,u,update,dist + 1,max(maxx,w));
}
if(update == 0)
{
if(maxx > k) ans += t.get(maxx - k),tot ++;
}
else t.up(dist,update);
}
void build_centroid(int u)
{
u = dfs_centroid(u,dfs_size(u));
is_remove[u] = 1;
// do sth
for(auto [v,w] : ke[u])
{
if(is_remove[v]) continue;
dfs_dist(v,u,0,1,w);
dfs_dist(v,u,1,1,w);
}
for(auto [v,w] : ke[u])
{
if(is_remove[v]) continue;
dfs_dist(v,u,-1,1,w);
}
fod(i,ke[u].size() - 1,0)
{
auto [v,w] = ke[u][i];
if(is_remove[v]) continue;
dfs_dist(v,u,0,1,w);
dfs_dist(v,u,1,1,w);
}
for(auto [v,w] : ke[u])
{
if(is_remove[v]) continue;
build_centroid(v);
}
}
main()
{
#define name "TASK"
if(fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> k;
fo(i,1,n - 1)
{
int u,v,w;
cin >> u >> v >> w;
ke[u].pb(v,w);
ke[v].pb(u,w);
}
build_centroid(1);
cout << ans + tot;
}
Compilation message
Main.cpp:106:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
106 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:111:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:112:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3516 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3516 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |