Submission #372908

# Submission time Handle Problem Language Result Execution time Memory
372908 2021-03-02T08:54:47 Z sam571128 Janjetina (COCI21_janjetina) C++14
110 / 110
609 ms 18268 KB
#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 1e5+5;
vector<pair<int,int>> adj[N];
vector<pair<int,int>> vv;
int ans, nowsz;
int n,k;
int bit[N], sz[N], vis[N], dep[N];

void update(int pos, int val){
	pos++;
	while(pos < N){
		bit[pos] += val;
		pos += pos&-pos;
	}
}

int query(int pos){
	pos++;
	int res = 0;
	while(pos>0){
		res += bit[pos];
		pos -= pos&-pos;
	}
	return res;
}

void dfs(int u, int par){
	nowsz++;
	sz[u] = 1;
	for(auto [v,w] : adj[u]){
		if(v==par||vis[v]) continue;
		dep[v] = dep[u]+1;
		dfs(v,u);
		sz[u] += sz[v];
	}
}

int dfs2(int u, int par){
	for(auto [v,w] : adj[u]){
		if(v==par||vis[v]) continue;
		if(sz[v]*2 > nowsz) return dfs2(v,u);
	}
	return u;
}

void dfs3(int u, int par, int mx){
	vv.push_back({mx,u});
	for(auto [v,w] : adj[u]){
		if(v==par||vis[v]) continue;
		dfs3(v,u,max(mx,w));
	}
}

void solve(int num){
	sort(vv.begin(),vv.end());
	for(auto [a,b] : vv){
		if(a-dep[b]-k>=0) ans += num*query(a-dep[b]-k);
		update(dep[b],1);
	}
	for(auto [a,b] : vv){
		update(dep[b],-1);
	}
	vv.clear();
}

void decompose(int u){
	nowsz = 0;
	dep[u] = 0;
	dfs(u,-1);
	int cen = dfs2(u,-1);
	dep[cen] = 0;
	nowsz = 0;
	dfs(cen,-1);
	dfs3(cen,cen,0);
	solve(1);
	for(auto [v,w] : adj[cen]){
		if(vis[v]) continue;
		dfs3(v,cen,w);
		solve(-1);
	}
	vis[cen] = 1;
	for(auto [v,w] : adj[cen]){
		if(vis[v]) continue;
		decompose(v);
	}
}

signed main(){
	fastio
	cin >> n >> k;
	for(int i = 1;i < n;i++){
		int u,v,w;
		cin >> u >> v >> w;
		adj[u].push_back({v,w});
		adj[v].push_back({u,w});
	}
	decompose(1);
	cout << ans*2 << "\n";
}

Compilation message

Main.cpp: In function 'void dfs(long long int, long long int)':
Main.cpp:36:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   36 |  for(auto [v,w] : adj[u]){
      |           ^
Main.cpp: In function 'long long int dfs2(long long int, long long int)':
Main.cpp:45:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   45 |  for(auto [v,w] : adj[u]){
      |           ^
Main.cpp: In function 'void dfs3(long long int, long long int, long long int)':
Main.cpp:54:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   54 |  for(auto [v,w] : adj[u]){
      |           ^
Main.cpp: In function 'void solve(long long int)':
Main.cpp:62:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   62 |  for(auto [a,b] : vv){
      |           ^
Main.cpp:66:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   66 |  for(auto [a,b] : vv){
      |           ^
Main.cpp: In function 'void decompose(long long int)':
Main.cpp:82:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |  for(auto [v,w] : adj[cen]){
      |           ^
Main.cpp:88:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   88 |  for(auto [v,w] : adj[cen]){
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 4 ms 2924 KB Output is correct
6 Correct 5 ms 2924 KB Output is correct
7 Correct 5 ms 2924 KB Output is correct
8 Correct 5 ms 2940 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 4 ms 2796 KB Output is correct
11 Correct 4 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 4 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 5 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 5 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 4 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 2796 KB Output is correct
2 Correct 3 ms 2796 KB Output is correct
3 Correct 3 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 36 ms 4332 KB Output is correct
6 Correct 220 ms 10592 KB Output is correct
7 Correct 393 ms 18160 KB Output is correct
8 Correct 441 ms 18264 KB Output is correct
9 Correct 449 ms 18152 KB Output is correct
10 Correct 506 ms 18140 KB Output is correct
11 Correct 390 ms 18268 KB Output is correct
12 Correct 437 ms 18140 KB Output is correct
13 Correct 480 ms 18140 KB Output is correct
14 Correct 479 ms 18268 KB Output is correct
15 Correct 502 ms 18140 KB Output is correct
16 Correct 482 ms 18140 KB Output is correct
17 Correct 474 ms 18144 KB Output is correct
18 Correct 480 ms 18012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 4 ms 2924 KB Output is correct
6 Correct 5 ms 2924 KB Output is correct
7 Correct 5 ms 2924 KB Output is correct
8 Correct 5 ms 2940 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 4 ms 2796 KB Output is correct
11 Correct 4 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 4 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 5 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 5 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 4 ms 2796 KB Output is correct
22 Correct 4 ms 2796 KB Output is correct
23 Correct 2 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 5 ms 2924 KB Output is correct
27 Correct 36 ms 4332 KB Output is correct
28 Correct 220 ms 10592 KB Output is correct
29 Correct 393 ms 18160 KB Output is correct
30 Correct 441 ms 18264 KB Output is correct
31 Correct 449 ms 18152 KB Output is correct
32 Correct 506 ms 18140 KB Output is correct
33 Correct 390 ms 18268 KB Output is correct
34 Correct 437 ms 18140 KB Output is correct
35 Correct 480 ms 18140 KB Output is correct
36 Correct 479 ms 18268 KB Output is correct
37 Correct 502 ms 18140 KB Output is correct
38 Correct 482 ms 18140 KB Output is correct
39 Correct 474 ms 18144 KB Output is correct
40 Correct 480 ms 18012 KB Output is correct
41 Correct 2 ms 2796 KB Output is correct
42 Correct 387 ms 18268 KB Output is correct
43 Correct 445 ms 18140 KB Output is correct
44 Correct 457 ms 18160 KB Output is correct
45 Correct 525 ms 18140 KB Output is correct
46 Correct 386 ms 18156 KB Output is correct
47 Correct 473 ms 18268 KB Output is correct
48 Correct 455 ms 18268 KB Output is correct
49 Correct 488 ms 18268 KB Output is correct
50 Correct 494 ms 18140 KB Output is correct
51 Correct 478 ms 18140 KB Output is correct
52 Correct 241 ms 12284 KB Output is correct
53 Correct 260 ms 12252 KB Output is correct
54 Correct 239 ms 13404 KB Output is correct
55 Correct 249 ms 13788 KB Output is correct
56 Correct 255 ms 13932 KB Output is correct
57 Correct 596 ms 13408 KB Output is correct
58 Correct 559 ms 13752 KB Output is correct
59 Correct 609 ms 13788 KB Output is correct
60 Correct 558 ms 13796 KB Output is correct
61 Correct 599 ms 13664 KB Output is correct
62 Correct 489 ms 13412 KB Output is correct
63 Correct 508 ms 13668 KB Output is correct
64 Correct 574 ms 13664 KB Output is correct
65 Correct 16 ms 3436 KB Output is correct
66 Correct 2 ms 2796 KB Output is correct