This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a; --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(v) (int)v.size()
#define fi first
#define se second
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} else return false;
}
template<class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int bit[N];
void update(int i, int val) {
for(; i < N; i += i & -i) bit[i] += val;
}
int get(int i) {
int res(0);
for(; i; i -= i & -i) res += bit[i];
return res;
}
int n, k;
int sz[N], cnt[N];
bool del[N];
vector<pii> g[N], res;
ll ans;
int dfs_size(int u, int p = 0) {
sz[u] = 1;
for(auto [v, w] : g[u]) if(v != p && !del[v]) sz[u] += dfs_size(v, u);
return sz[u];
}
int find_centroid(int u, int p, int num) {
for(auto [v, w] : g[u]) if(v != p && !del[v] && sz[v] > num / 2)
return find_centroid(v, u, num);
return u;
}
void dfs(int u, int p, int dep, int val) {
res.emplace_back(val, dep);
for(auto [v, w] : g[u]) if(v != p && !del[v])
dfs(v, u, dep + 1, max(val, w));
}
ll compute(vector<pii> &res) {
sort(all(res));
ll ans(0);
for(auto [w, len] : res) {
if(w >= len) ans += get(w - len);
update(k + len, 1);
}
for(auto [w, len] : res) {
update(k + len, -1);
}
return ans;
}
void decomposition(int u) {
u = find_centroid(u, 0, dfs_size(u));
del[u] = 1;
res.clear();
dfs(u, 0, 0, 0);
ans += compute(res);
for(auto [v, w] : g[u]) if(!del[v]) {
res.clear();
dfs(v, u, 1, w);
ans -= compute(res);
}
for(auto [v, w] : g[u]) if(!del[v])
decomposition(v);
}
void process() {
cin >> n >> k;
REP(i, 1, n) {
int u, v, w; cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
decomposition(1);
cout << ans * 2 << '\n';
}
signed main() {
if(fopen("COCI21_janjetina.inp","r")) {
freopen("COCI21_janjetina.inp","r",stdin);
freopen("COCI21_janjetina.out","w",stdout);
}
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// clock_t start = clock();
int ntests = 1;
// cin >> ntests;
while(ntests--) process();
// cerr << "Time elapsed: " << clock() - start << " ms!\n";
return 0;
}
// https://oj.uz/problem/view/COCI21_janjetina
Compilation message (stderr)
Main.cpp: In function 'int dfs_size(int, int)':
Main.cpp:64:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | for(auto [v, w] : g[u]) if(v != p && !del[v]) sz[u] += dfs_size(v, u);
| ^
Main.cpp: In function 'int find_centroid(int, int, int)':
Main.cpp:69:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
69 | for(auto [v, w] : g[u]) if(v != p && !del[v] && sz[v] > num / 2)
| ^
Main.cpp: In function 'void dfs(int, int, int, int)':
Main.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | for(auto [v, w] : g[u]) if(v != p && !del[v])
| ^
Main.cpp: In function 'long long int compute(std::vector<std::pair<int, int> >&)':
Main.cpp:83:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | for(auto [w, len] : res) {
| ^
Main.cpp:87:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
87 | for(auto [w, len] : res) {
| ^
Main.cpp: In function 'void decomposition(int)':
Main.cpp:99:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
99 | for(auto [v, w] : g[u]) if(!del[v]) {
| ^
Main.cpp:104:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
104 | for(auto [v, w] : g[u]) if(!del[v])
| ^
Main.cpp: In function 'int main()':
Main.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen("COCI21_janjetina.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen("COCI21_janjetina.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |