답안 #982140

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
982140 2024-05-13T23:57:55 Z Ivo_12 Janjetina (COCI21_janjetina) C++
110 / 110
374 ms 22332 KB
#include <bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pll pair < ll, ll >

using namespace std;

const int N = 1e5+10;
int n, k;
vector < pll > edges[N];
ll maxmass[N];
ll siz[N];
ll dep[N];
bool rip[N];
ll fenwick[N];
vector < pll > querries;

void add( int x, ll v ) {
	for(x; x < N; x += x & -x) fenwick[x] += v;
	return;
}

ll sum( int x ) {
	ll out = 0;
	for(x; x > 0; x -= x & -x) out += fenwick[x];
	return out;
}

void dfs( int x, int par ) {
	
	if(par!=-1) dep[x] = dep[par] + 1;
	else dep[x] = 0;
	if(par==-1) maxmass[x] = 0;
	siz[x] = 1;
	for(int i = 0; i < (int) edges[x].size(); i++) {
		if(edges[x][i].F != par && !rip[edges[x][i].F]) {
			maxmass[edges[x][i].F] = max(maxmass[x], edges[x][i].S);
			dfs(edges[x][i].F, x);
			siz[x] += siz[edges[x][i].F];
		}
	}
	return;
	
}

int find_centroid( int x, int par, int treesiz ) {
	for(int i = 0; i < (int) edges[x].size(); i++) if(!rip[edges[x][i].F] && siz[edges[x][i].F] > treesiz/2 && edges[x][i].F != par) return find_centroid(edges[x][i].F, x, treesiz);
	return x;
}

void add_to_q( int x, int par ) {
	if(dep[x]!=0) querries.pb(mp(maxmass[x], dep[x]));
	for(int i = 0; i < (int) edges[x].size(); i++) {
		if(edges[x][i].F != par && (!rip[edges[x][i].F])) add_to_q(edges[x][i].F, x);
	}
	return;
}

ll dac( int x ) {
	ll sol = 0;
	dfs(x, -1);
	if(siz[x] == 1) return 0;
	x = find_centroid(x, -1, siz[x]);
	dfs(x, -1);
	add_to_q(x, -1);
	sort(querries.begin(), querries.end());
	for(int i = 0; i < (int) querries.size(); i++) {
		sol += sum(querries[i].F - querries[i].S - k) + (int) (querries[i].F - querries[i].S >= k);
		add(querries[i].S, 1);
	}
	for(int i = 0; i < (int) querries.size(); i++) add(querries[i].S, -1);
	querries.clear();
	rip[x] = 1;
	for(int i = 0; i < (int) edges[x].size(); i++) {
		if(!rip[edges[x][i].F]) {
			add_to_q(edges[x][i].F, x);
			sort(querries.begin(), querries.end());
			for(int j = 0; j < querries.size(); j++) {
				sol -= sum(querries[j].F - querries[j].S - k);
				add(querries[j].S, 1);
			}
			for(int j = 0; j < querries.size(); j++) {
				add(querries[j].S, -1);
			}
			querries.clear();
			sol += dac(edges[x][i].F);
		}
	}
	return sol;
}

int main( void ) {
	
	FIO
	cin >> n >> k;
	int tempa, tempb, tempc;
	for(int i = 0; i < n - 1; i++) {
		cin >> tempa >> tempb >> tempc;
		edges[--tempa].pb(mp(--tempb, tempc));
		edges[tempb].pb(mp(tempa, tempc));
	}
	cout << dac(0) * 2;
	
	return 0;
}

Compilation message

Main.cpp: In function 'void add(int, long long int)':
Main.cpp:23:6: warning: statement has no effect [-Wunused-value]
   23 |  for(x; x < N; x += x & -x) fenwick[x] += v;
      |      ^
Main.cpp: In function 'long long int sum(int)':
Main.cpp:29:6: warning: statement has no effect [-Wunused-value]
   29 |  for(x; x > 0; x -= x & -x) out += fenwick[x];
      |      ^
Main.cpp: In function 'long long int dac(int)':
Main.cpp:82:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |    for(int j = 0; j < querries.size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~~
Main.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |    for(int j = 0; j < querries.size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5720 KB Output is correct
2 Correct 1 ms 5724 KB Output is correct
3 Correct 1 ms 5724 KB Output is correct
4 Correct 3 ms 5980 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 3 ms 5868 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5976 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5724 KB Output is correct
11 Correct 3 ms 5724 KB Output is correct
12 Correct 3 ms 5980 KB Output is correct
13 Correct 3 ms 5980 KB Output is correct
14 Correct 3 ms 5980 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 3 ms 5864 KB Output is correct
19 Correct 4 ms 5980 KB Output is correct
20 Correct 3 ms 5980 KB Output is correct
21 Correct 3 ms 5980 KB Output is correct
22 Correct 3 ms 5980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5724 KB Output is correct
2 Correct 2 ms 5720 KB Output is correct
3 Correct 1 ms 5724 KB Output is correct
4 Correct 3 ms 5980 KB Output is correct
5 Correct 22 ms 7380 KB Output is correct
6 Correct 169 ms 13052 KB Output is correct
7 Correct 270 ms 20432 KB Output is correct
8 Correct 321 ms 20388 KB Output is correct
9 Correct 283 ms 20420 KB Output is correct
10 Correct 368 ms 20432 KB Output is correct
11 Correct 263 ms 20432 KB Output is correct
12 Correct 346 ms 20420 KB Output is correct
13 Correct 311 ms 20420 KB Output is correct
14 Correct 350 ms 20432 KB Output is correct
15 Correct 366 ms 20432 KB Output is correct
16 Correct 339 ms 20744 KB Output is correct
17 Correct 344 ms 20428 KB Output is correct
18 Correct 334 ms 20500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5720 KB Output is correct
2 Correct 1 ms 5724 KB Output is correct
3 Correct 1 ms 5724 KB Output is correct
4 Correct 3 ms 5980 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 3 ms 5868 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5976 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5724 KB Output is correct
11 Correct 3 ms 5724 KB Output is correct
12 Correct 3 ms 5980 KB Output is correct
13 Correct 3 ms 5980 KB Output is correct
14 Correct 3 ms 5980 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 3 ms 5864 KB Output is correct
19 Correct 4 ms 5980 KB Output is correct
20 Correct 3 ms 5980 KB Output is correct
21 Correct 3 ms 5980 KB Output is correct
22 Correct 3 ms 5980 KB Output is correct
23 Correct 1 ms 5724 KB Output is correct
24 Correct 2 ms 5720 KB Output is correct
25 Correct 1 ms 5724 KB Output is correct
26 Correct 3 ms 5980 KB Output is correct
27 Correct 22 ms 7380 KB Output is correct
28 Correct 169 ms 13052 KB Output is correct
29 Correct 270 ms 20432 KB Output is correct
30 Correct 321 ms 20388 KB Output is correct
31 Correct 283 ms 20420 KB Output is correct
32 Correct 368 ms 20432 KB Output is correct
33 Correct 263 ms 20432 KB Output is correct
34 Correct 346 ms 20420 KB Output is correct
35 Correct 311 ms 20420 KB Output is correct
36 Correct 350 ms 20432 KB Output is correct
37 Correct 366 ms 20432 KB Output is correct
38 Correct 339 ms 20744 KB Output is correct
39 Correct 344 ms 20428 KB Output is correct
40 Correct 334 ms 20500 KB Output is correct
41 Correct 1 ms 5724 KB Output is correct
42 Correct 267 ms 21668 KB Output is correct
43 Correct 340 ms 22212 KB Output is correct
44 Correct 299 ms 21960 KB Output is correct
45 Correct 333 ms 22280 KB Output is correct
46 Correct 272 ms 21700 KB Output is correct
47 Correct 374 ms 22212 KB Output is correct
48 Correct 277 ms 21976 KB Output is correct
49 Correct 318 ms 22332 KB Output is correct
50 Correct 350 ms 21956 KB Output is correct
51 Correct 335 ms 22216 KB Output is correct
52 Correct 115 ms 14940 KB Output is correct
53 Correct 131 ms 15044 KB Output is correct
54 Correct 94 ms 14532 KB Output is correct
55 Correct 139 ms 15044 KB Output is correct
56 Correct 128 ms 15056 KB Output is correct
57 Correct 262 ms 14584 KB Output is correct
58 Correct 319 ms 14884 KB Output is correct
59 Correct 276 ms 14848 KB Output is correct
60 Correct 298 ms 14800 KB Output is correct
61 Correct 296 ms 14792 KB Output is correct
62 Correct 210 ms 14540 KB Output is correct
63 Correct 250 ms 14964 KB Output is correct
64 Correct 276 ms 14796 KB Output is correct
65 Correct 11 ms 6248 KB Output is correct
66 Correct 1 ms 5720 KB Output is correct