Submission #446004

# Submission time Handle Problem Language Result Execution time Memory
446004 2021-07-20T12:27:55 Z grt Janjetina (COCI21_janjetina) C++17
110 / 110
979 ms 23552 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ST first
#define ND second
#define PB push_back

using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

typedef tree<pi,null_type,less<pi>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int nax = 100 * 1000 + 10, INF = 1e9;
int n, k;
vector<pi>V[nax];
bool mark[nax];
int dp[nax];
vi all;
ll ans;
ordered_set S;

void dfs(int x, int p) {
	dp[x] = 1;
	all.PB(x);
	for(auto nbh : V[x]) if(nbh.ST != p && !mark[nbh.ST]) {
		dfs(nbh.ST, x);
		dp[x] += dp[nbh.ST];
	}
}

vector<pi>path;

void dfsC(int x, int p, int l, int mx) {
	path.emplace_back(mx, l);
	for(auto nbh : V[x]) if(nbh.ST != p && !mark[nbh.ST]) {
		dfsC(nbh.ST, x, l + 1, max(mx, nbh.ND));
	}
}


void centro(int x) {
	all.clear();
	path.clear();
	dfs(x, 0);
	int c = x;
	for(int y : all) if(dp[y] < dp[c] && 2 * dp[y] >= dp[x]) c = y;
	dfsC(c, 0, 0, 0);
	sort(path.begin(), path.end());
	int id = 1;
	for(auto [w, l] : path) {
		//cerr << c << ": " << w << " " << l << " " << S.order_of_key({w - l - k + 1, INF}) << "\n";
		ans += S.order_of_key({w - l - k, INF});
		S.insert({l, id++});
	}
	//cout << ans << "\n";
	S.clear();
	mark[c] = true;
	for(auto nbh : V[c]) if(!mark[nbh.ST]) {
		id = 1;
		path.clear();
		dfsC(nbh.ST, c, 1, nbh.ND);
		sort(path.begin(), path.end());
		for(auto [w, l] : path) {
			ans -= S.order_of_key({w - l - k, INF});
			S.insert({l,id++});
		}
		S.clear();
		centro(nbh.ST);
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> k;
	for(int a, b, c, i = 1; i < n; ++i) {
		cin >> a >> b >> c;
		V[a].emplace_back(b, c);
		V[b].emplace_back(a, c);
	}
	centro(1);
	cout << ans * 2;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 5 ms 2868 KB Output is correct
5 Correct 5 ms 2864 KB Output is correct
6 Correct 5 ms 2764 KB Output is correct
7 Correct 5 ms 2764 KB Output is correct
8 Correct 5 ms 2764 KB Output is correct
9 Correct 3 ms 2764 KB Output is correct
10 Correct 3 ms 2764 KB Output is correct
11 Correct 4 ms 2764 KB Output is correct
12 Correct 5 ms 2764 KB Output is correct
13 Correct 5 ms 2764 KB Output is correct
14 Correct 5 ms 2764 KB Output is correct
15 Correct 4 ms 2804 KB Output is correct
16 Correct 4 ms 2872 KB Output is correct
17 Correct 4 ms 2764 KB Output is correct
18 Correct 4 ms 2764 KB Output is correct
19 Correct 5 ms 2764 KB Output is correct
20 Correct 4 ms 2764 KB Output is correct
21 Correct 5 ms 2804 KB Output is correct
22 Correct 5 ms 2804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 5 ms 2764 KB Output is correct
5 Correct 50 ms 4708 KB Output is correct
6 Correct 397 ms 12944 KB Output is correct
7 Correct 773 ms 22964 KB Output is correct
8 Correct 965 ms 23364 KB Output is correct
9 Correct 814 ms 22976 KB Output is correct
10 Correct 963 ms 23236 KB Output is correct
11 Correct 772 ms 22952 KB Output is correct
12 Correct 919 ms 23552 KB Output is correct
13 Correct 803 ms 22968 KB Output is correct
14 Correct 924 ms 23492 KB Output is correct
15 Correct 938 ms 23076 KB Output is correct
16 Correct 896 ms 23212 KB Output is correct
17 Correct 883 ms 23228 KB Output is correct
18 Correct 850 ms 23156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 5 ms 2868 KB Output is correct
5 Correct 5 ms 2864 KB Output is correct
6 Correct 5 ms 2764 KB Output is correct
7 Correct 5 ms 2764 KB Output is correct
8 Correct 5 ms 2764 KB Output is correct
9 Correct 3 ms 2764 KB Output is correct
10 Correct 3 ms 2764 KB Output is correct
11 Correct 4 ms 2764 KB Output is correct
12 Correct 5 ms 2764 KB Output is correct
13 Correct 5 ms 2764 KB Output is correct
14 Correct 5 ms 2764 KB Output is correct
15 Correct 4 ms 2804 KB Output is correct
16 Correct 4 ms 2872 KB Output is correct
17 Correct 4 ms 2764 KB Output is correct
18 Correct 4 ms 2764 KB Output is correct
19 Correct 5 ms 2764 KB Output is correct
20 Correct 4 ms 2764 KB Output is correct
21 Correct 5 ms 2804 KB Output is correct
22 Correct 5 ms 2804 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 5 ms 2764 KB Output is correct
27 Correct 50 ms 4708 KB Output is correct
28 Correct 397 ms 12944 KB Output is correct
29 Correct 773 ms 22964 KB Output is correct
30 Correct 965 ms 23364 KB Output is correct
31 Correct 814 ms 22976 KB Output is correct
32 Correct 963 ms 23236 KB Output is correct
33 Correct 772 ms 22952 KB Output is correct
34 Correct 919 ms 23552 KB Output is correct
35 Correct 803 ms 22968 KB Output is correct
36 Correct 924 ms 23492 KB Output is correct
37 Correct 938 ms 23076 KB Output is correct
38 Correct 896 ms 23212 KB Output is correct
39 Correct 883 ms 23228 KB Output is correct
40 Correct 850 ms 23156 KB Output is correct
41 Correct 2 ms 2636 KB Output is correct
42 Correct 766 ms 22808 KB Output is correct
43 Correct 979 ms 23484 KB Output is correct
44 Correct 831 ms 22932 KB Output is correct
45 Correct 972 ms 23388 KB Output is correct
46 Correct 795 ms 22996 KB Output is correct
47 Correct 917 ms 23484 KB Output is correct
48 Correct 787 ms 22960 KB Output is correct
49 Correct 947 ms 23356 KB Output is correct
50 Correct 971 ms 23096 KB Output is correct
51 Correct 885 ms 23360 KB Output is correct
52 Correct 322 ms 16068 KB Output is correct
53 Correct 325 ms 16484 KB Output is correct
54 Correct 274 ms 16004 KB Output is correct
55 Correct 335 ms 16332 KB Output is correct
56 Correct 300 ms 16364 KB Output is correct
57 Correct 696 ms 15680 KB Output is correct
58 Correct 765 ms 15972 KB Output is correct
59 Correct 846 ms 16064 KB Output is correct
60 Correct 784 ms 16060 KB Output is correct
61 Correct 850 ms 15940 KB Output is correct
62 Correct 621 ms 15652 KB Output is correct
63 Correct 684 ms 16000 KB Output is correct
64 Correct 745 ms 15984 KB Output is correct
65 Correct 21 ms 3344 KB Output is correct
66 Correct 2 ms 2636 KB Output is correct