#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <math.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <iomanip>
#include <random>
#include <chrono>
#include <cassert>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pb push_back
#define US freopen(".in", "r", stdin); freopen("j.out", "w", stdout);
ll gcd(ll a, ll b)
{
if (a == 0 || b == 0) {
return max(a, b);
}
if (a <= b) {
return gcd(a, b % a);
}
else {
return gcd(a % b, b);
}
}
ll lcm(ll a, ll b) {
return (a / gcd(a, b)) * b;
}
const int N = 70005;
const ll oo = 100000000000000000, MOD = 1000000007;
ll cnt[N];
int n, k, sz[N];
vector<pair<int, int>> g[N];
void dfs(int u, int p,int sm) {
//cout << u << endl;
for (int i = 0; i < g[u].size(); ++i) {
int h = g[u][i].fr;
int w = g[u][i].sc;
if (h != p) {
if (sm >= w) {
dfs(h, u, sm - w);
}
else {
cnt[u] += sz[h];
dfs(h, u, k-w);
}
}
}
}
void dfs1(int u, int p) {
for (int i = 0; i < g[u].size(); ++i) {
int h = g[u][i].fr;
if (h != p) {
dfs1(h, u);
sz[u] += sz[h];
}
}
++sz[u];
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].pb({ v,w });
g[v].pb({ u,w });
}
for (int i = 0; i < n; ++i) {
//cout << i << endl;
for (int j = 0; j < n; ++j) {
sz[j] = 0;
}
dfs1(i, -1);
dfs(i, -1, k);
}
for (int i = 0; i < n; ++i) {
cout << cnt[i] << endl;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//US
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
Compilation message
Main.cpp: In function 'void dfs(int, int, int)':
Main.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < g[u].size(); ++i) {
| ~~^~~~~~~~~~~~~
Main.cpp: In function 'void dfs1(int, int)':
Main.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < g[u].size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
9 ms |
2652 KB |
Output is correct |
4 |
Correct |
22 ms |
2724 KB |
Output is correct |
5 |
Correct |
19 ms |
2648 KB |
Output is correct |
6 |
Correct |
23 ms |
2796 KB |
Output is correct |
7 |
Correct |
21 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
13 ms |
2652 KB |
Output is correct |
10 |
Correct |
17 ms |
2652 KB |
Output is correct |
11 |
Correct |
13 ms |
2648 KB |
Output is correct |
12 |
Correct |
17 ms |
2728 KB |
Output is correct |
13 |
Correct |
13 ms |
2652 KB |
Output is correct |
14 |
Correct |
13 ms |
2732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Execution timed out |
3569 ms |
10540 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Execution timed out |
3569 ms |
10540 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Execution timed out |
3559 ms |
4956 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Execution timed out |
3559 ms |
4956 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
9 ms |
2652 KB |
Output is correct |
4 |
Correct |
22 ms |
2724 KB |
Output is correct |
5 |
Correct |
19 ms |
2648 KB |
Output is correct |
6 |
Correct |
23 ms |
2796 KB |
Output is correct |
7 |
Correct |
21 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
13 ms |
2652 KB |
Output is correct |
10 |
Correct |
17 ms |
2652 KB |
Output is correct |
11 |
Correct |
13 ms |
2648 KB |
Output is correct |
12 |
Correct |
17 ms |
2728 KB |
Output is correct |
13 |
Correct |
13 ms |
2652 KB |
Output is correct |
14 |
Correct |
13 ms |
2732 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Execution timed out |
3569 ms |
10540 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |