Submission #366288

# Submission time Handle Problem Language Result Execution time Memory
366288 2021-02-13T19:28:27 Z model_code Janjetina (COCI21_janjetina) C++17
110 / 110
607 ms 16248 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
#define x first
#define y second
#define mp make_pair

const int MAXN = (int)1e5;
int n, k;
int ss[MAXN + 5], len[MAXN + 5], cur;
bool bio[MAXN + 5];
vector <pii> ms[MAXN + 5], v;
ll f[MAXN + 5], sol;

void dfs1(int x, int p){
	cur++;
	ss[x] = 1;
	for(vector <pii>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		pii o = *i;
		if(o.x == p || bio[o.x])
			continue;
		len[o.x] = len[x] + 1;
		dfs1(o.x, x);
		ss[x] += ss[o.x];
	}
}

int dfs2(int x, int p){
	for(vector <pii>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		int o = i->x;
		if(o == p || bio[o])
			continue;
		if(ss[o] * 2 > cur)
			return dfs2(o, x);
	}
	return x;
}

bool cmp(pii x, pii y){
	if(x.y != y.y)
		return x.y < y.y;
	return x.x < y.x;
}

void update(int x, ll y){
	x++;
	for(int i = x ; i <= MAXN + 2 ; i += (i & -i)){
		f[i] += y;
	}
}

ll query(int x){
	ll ret = 0;
	x++;
	for( ; x > 0 ; x -= (x & -x)){
		ret += f[x];
	}
	return ret;
}

void dfs3(int x, int p, int maxi){
	v.push_back(mp(x, maxi));
	for(vector <pii>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		pii o = *i;
		if(o.x == p || bio[o.x])
			continue;
		dfs3(o.x, x, max(maxi, o.y));
	}
}

void decompose(int root){
	cur = 0;
	len[root] = 0;
	dfs1(root, -1);
	int centroid = dfs2(root, -1);
	cur = 0;
	len[centroid] = 0;
	dfs1(centroid, -1);
	dfs3(centroid, -1, 0);
	sort(v.begin(), v.end(), cmp);
	for(int i = 0 ; i < (int)v.size(); ++i){
		pii o = v[i];
		sol += query(o.y - len[o.x] - k);
		update(len[o.x], 1);
	}
	for(int i = 0 ; i < (int)v.size() ; ++i){
		pii o = v[i];
		update(len[o.x], -1);
	}
	v.clear();
	for(vector <pii>::iterator i = ms[centroid].begin() ; i != ms[centroid].end() ; ++i){
		pii o = *i;
		if(bio[o.x])
			continue;
		dfs3(o.x, centroid, o.y);
		sort(v.begin(), v.end(), cmp);
		for(int j = 0 ; j < (int)v.size() ; ++j){
			pii u = v[j];
			sol -= query(u.y - len[u.x] - k);
			update(len[u.x], 1);
		}
		for(int j = 0 ; j < (int)v.size() ; ++j){
			pii u = v[j];
			update(len[u.x], -1);
		}
		v.clear();
	}
	bio[centroid] = 1;
	for(vector <pii>::iterator i = ms[centroid].begin() ; i != ms[centroid].end() ; ++i){
		int o = i->x;
		if(bio[o])
			continue;
		decompose(o);
	}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 0 ; i < n - 1 ; ++i){
	int x, y, w;
	cin >> x >> y >> w;
	ms[x].push_back(mp(y, w));
	ms[y].push_back(mp(x, w));
}
decompose(1);
sol = sol * 2LL;
cout << sol << endl;

return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 3 ms 2796 KB Output is correct
4 Correct 5 ms 2796 KB Output is correct
5 Correct 9 ms 2796 KB Output is correct
6 Correct 5 ms 2944 KB Output is correct
7 Correct 6 ms 2796 KB Output is correct
8 Correct 5 ms 2796 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 4 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 5 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 5 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 5 ms 2796 KB Output is correct
21 Correct 5 ms 2796 KB Output is correct
22 Correct 4 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2924 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 6 ms 2924 KB Output is correct
5 Correct 42 ms 4204 KB Output is correct
6 Correct 257 ms 9320 KB Output is correct
7 Correct 465 ms 15968 KB Output is correct
8 Correct 515 ms 16152 KB Output is correct
9 Correct 525 ms 15968 KB Output is correct
10 Correct 587 ms 16100 KB Output is correct
11 Correct 484 ms 15996 KB Output is correct
12 Correct 531 ms 16024 KB Output is correct
13 Correct 535 ms 15968 KB Output is correct
14 Correct 592 ms 16044 KB Output is correct
15 Correct 607 ms 15828 KB Output is correct
16 Correct 557 ms 16012 KB Output is correct
17 Correct 546 ms 15968 KB Output is correct
18 Correct 550 ms 15768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 3 ms 2796 KB Output is correct
4 Correct 5 ms 2796 KB Output is correct
5 Correct 9 ms 2796 KB Output is correct
6 Correct 5 ms 2944 KB Output is correct
7 Correct 6 ms 2796 KB Output is correct
8 Correct 5 ms 2796 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 4 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 5 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 5 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 5 ms 2796 KB Output is correct
21 Correct 5 ms 2796 KB Output is correct
22 Correct 4 ms 2796 KB Output is correct
23 Correct 2 ms 2924 KB Output is correct
24 Correct 2 ms 2796 KB Output is correct
25 Correct 2 ms 2796 KB Output is correct
26 Correct 6 ms 2924 KB Output is correct
27 Correct 42 ms 4204 KB Output is correct
28 Correct 257 ms 9320 KB Output is correct
29 Correct 465 ms 15968 KB Output is correct
30 Correct 515 ms 16152 KB Output is correct
31 Correct 525 ms 15968 KB Output is correct
32 Correct 587 ms 16100 KB Output is correct
33 Correct 484 ms 15996 KB Output is correct
34 Correct 531 ms 16024 KB Output is correct
35 Correct 535 ms 15968 KB Output is correct
36 Correct 592 ms 16044 KB Output is correct
37 Correct 607 ms 15828 KB Output is correct
38 Correct 557 ms 16012 KB Output is correct
39 Correct 546 ms 15968 KB Output is correct
40 Correct 550 ms 15768 KB Output is correct
41 Correct 2 ms 2668 KB Output is correct
42 Correct 455 ms 15840 KB Output is correct
43 Correct 512 ms 15840 KB Output is correct
44 Correct 532 ms 15972 KB Output is correct
45 Correct 581 ms 16248 KB Output is correct
46 Correct 450 ms 15840 KB Output is correct
47 Correct 516 ms 16084 KB Output is correct
48 Correct 540 ms 15840 KB Output is correct
49 Correct 574 ms 16064 KB Output is correct
50 Correct 568 ms 15840 KB Output is correct
51 Correct 555 ms 15968 KB Output is correct
52 Correct 236 ms 8804 KB Output is correct
53 Correct 259 ms 8804 KB Output is correct
54 Correct 215 ms 8804 KB Output is correct
55 Correct 258 ms 8932 KB Output is correct
56 Correct 262 ms 8804 KB Output is correct
57 Correct 515 ms 8428 KB Output is correct
58 Correct 543 ms 8428 KB Output is correct
59 Correct 573 ms 8428 KB Output is correct
60 Correct 578 ms 8428 KB Output is correct
61 Correct 568 ms 8424 KB Output is correct
62 Correct 461 ms 8420 KB Output is correct
63 Correct 490 ms 8420 KB Output is correct
64 Correct 557 ms 8348 KB Output is correct
65 Correct 19 ms 3052 KB Output is correct
66 Correct 2 ms 2668 KB Output is correct