답안 #1067916

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067916 2024-08-21T05:32:22 Z 김은성(#11126) Paths (RMI21_paths) C++17
36 / 100
600 ms 23520 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
typedef long long ll;
vector<pair<int, ll> > graph[MAXN];
vector<int> child[MAXN];
bool ch[MAXN];
int par[MAXN], euler[MAXN], ed[MAXN], cnt=0;
ll cost[MAXN];	//euler tour only for cost
ll tcost[1<<18];
pair<ll, int> tree[1<<18];
int n, lazy[1<<18];
vector<int> tour;
void settree(int v){
	euler[v] = ++cnt;
	tour.push_back(v);
	//printf("v=%d cost[euler[v]]=%d\n",v, cost[euler[v]]);
	for(auto [u, c]: graph[v]){
		if(par[v] == u)
			continue;
		par[u] = v;
		cost[cnt+1] = cost[euler[v]] + c;
		child[v].push_back(u);
		settree(u);
	}
	ed[v] = cnt;
}
void init(int v, int l, int r){
	if(l==r){
		tcost[v] = cost[l];
		tree[v] = make_pair(cost[l], l);
	}
	else{
		int mid = (l+r)>>1;
		init(2*v, l, mid);
		init(2*v+1, mid+1, r);
		tree[v] = max(tree[2*v], tree[2*v+1]);
		tcost[v] = max(tcost[2*v], tcost[2*v+1]);
	}
}
void update(int v, int l, int r, int s, int e, int p){
	tree[v].first = min(tree[v].first, tcost[v] - cost[lazy[v]]);
	if(l != r){
		lazy[2*v] = max(lazy[2*v], lazy[v]);
		lazy[2*v+1] = max(lazy[2*v+1], lazy[v]);
	}
	if(e<l || r<s)
		return;
	if(s<=l && r<=e){
		tree[v].first = min(tree[v].first, tcost[v] - cost[p]);
		if(l != r){
			lazy[2*v] = max(lazy[2*v], p);
			lazy[2*v+1] = max(lazy[2*v+1], p);
		}
	}
	else{
		int mid = (l+r)/2;
		update(2*v, l ,mid, s, e, p);
		update(2*v+1, mid+1, r, s, e, p);
		tree[v] = max(tree[2*v], tree[2*v+1]);
	}
}
void follow(int v){
	if(ch[v])
		return;
	//printf("v=%d euler[v]=%d ed[v]=%d\n", v, euler[v], ed[v]);
	update(1, 1, n, euler[v], euler[v], euler[v]);
	for(int u: child[v]){
		if(!ch[u])
			update(1, 1, n, euler[u], ed[u], euler[v]);
	}
	ch[v] = 1;
	follow(par[v]);
}
int main(){
	int k, i, j, a, b;
	ll c;
	scanf("%d %d", &n, &k);
	for(i=1; i<n; i++){
		scanf("%d %d %lld", &a, &b, &c);
		graph[a].push_back(make_pair(b, c));
		graph[b].push_back(make_pair(a, c));
	}
	for(int r=1; r<=n; r++){
		for(i=1; i<=n; i++)
			child[i].clear();
		memset(ch, 0, sizeof(ch));
		ch[0] = 1;
		memset(par, 0, sizeof(par));
		memset(cost, 0, sizeof(cost));
		memset(tree, 0, sizeof(tree));
		memset(lazy, 0, sizeof(lazy));
		cnt = 0;
		tour.clear();
		tour.push_back(-1);
		settree(r);
		init(1,1,n);
		ll ans = 0;
		for(i=1; i<=k; i++){
			ans += tree[1].first;
			if(r==4){
				//printf("tree[1].first=%lld second=%d\n", tree[1].first, tour[tree[1].second]);
			}
			follow(tour[tree[1].second]);
		}
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:91:31: warning: 'void* memset(void*, int, size_t)' clearing an object of type 'struct std::pair<long long int, int>' with no trivial copy-assignment; use assignment instead [-Wclass-memaccess]
   91 |   memset(tree, 0, sizeof(tree));
      |                               ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
                 from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from Main.cpp:1:
/usr/include/c++/10/bits/stl_pair.h:211:12: note: 'struct std::pair<long long int, int>' declared here
  211 |     struct pair
      |            ^~~~
Main.cpp:76:12: warning: unused variable 'j' [-Wunused-variable]
   76 |  int k, i, j, a, b;
      |            ^
Main.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |  scanf("%d %d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:80:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |   scanf("%d %d %lld", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 14168 KB Output is correct
2 Correct 4 ms 14172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 14168 KB Output is correct
2 Correct 4 ms 14172 KB Output is correct
3 Correct 37 ms 14172 KB Output is correct
4 Correct 33 ms 14172 KB Output is correct
5 Correct 30 ms 14168 KB Output is correct
6 Correct 33 ms 14168 KB Output is correct
7 Correct 39 ms 14172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 14168 KB Output is correct
2 Correct 4 ms 14172 KB Output is correct
3 Correct 37 ms 14172 KB Output is correct
4 Correct 33 ms 14172 KB Output is correct
5 Correct 30 ms 14168 KB Output is correct
6 Correct 33 ms 14168 KB Output is correct
7 Correct 39 ms 14172 KB Output is correct
8 Correct 350 ms 14424 KB Output is correct
9 Correct 345 ms 14424 KB Output is correct
10 Correct 314 ms 14424 KB Output is correct
11 Correct 310 ms 14676 KB Output is correct
12 Correct 256 ms 14424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 14168 KB Output is correct
2 Correct 4 ms 14172 KB Output is correct
3 Correct 37 ms 14172 KB Output is correct
4 Correct 33 ms 14172 KB Output is correct
5 Correct 30 ms 14168 KB Output is correct
6 Correct 33 ms 14168 KB Output is correct
7 Correct 39 ms 14172 KB Output is correct
8 Correct 350 ms 14424 KB Output is correct
9 Correct 345 ms 14424 KB Output is correct
10 Correct 314 ms 14424 KB Output is correct
11 Correct 310 ms 14676 KB Output is correct
12 Correct 256 ms 14424 KB Output is correct
13 Execution timed out 1029 ms 14680 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1029 ms 23520 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 14168 KB Output is correct
2 Correct 4 ms 14172 KB Output is correct
3 Correct 37 ms 14172 KB Output is correct
4 Correct 33 ms 14172 KB Output is correct
5 Correct 30 ms 14168 KB Output is correct
6 Correct 33 ms 14168 KB Output is correct
7 Correct 39 ms 14172 KB Output is correct
8 Correct 350 ms 14424 KB Output is correct
9 Correct 345 ms 14424 KB Output is correct
10 Correct 314 ms 14424 KB Output is correct
11 Correct 310 ms 14676 KB Output is correct
12 Correct 256 ms 14424 KB Output is correct
13 Execution timed out 1029 ms 14680 KB Time limit exceeded
14 Halted 0 ms 0 KB -