답안 #1067920

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067920 2024-08-21T05:35:06 Z 김은성(#11126) Paths (RMI21_paths) C++17
36 / 100
600 ms 996 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2010;
const int SEGSZ = 1<<12;
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[SEGSZ];
pair<ll, int> tree[SEGSZ];
int n, lazy[SEGSZ];
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;
	ch[v] = 1;
	//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]);
	}
	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(tree[1].first == 0)
				break;
			follow(tour[tree[1].second]);
		}
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:92: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]
   92 |   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:77:12: warning: unused variable 'j' [-Wunused-variable]
   77 |  int k, i, j, a, b;
      |            ^
Main.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |  scanf("%d %d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |   scanf("%d %d %lld", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 7 ms 604 KB Output is correct
5 Correct 5 ms 672 KB Output is correct
6 Correct 7 ms 600 KB Output is correct
7 Correct 7 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 7 ms 604 KB Output is correct
5 Correct 5 ms 672 KB Output is correct
6 Correct 7 ms 600 KB Output is correct
7 Correct 7 ms 604 KB Output is correct
8 Correct 200 ms 980 KB Output is correct
9 Correct 184 ms 600 KB Output is correct
10 Correct 178 ms 776 KB Output is correct
11 Correct 163 ms 996 KB Output is correct
12 Correct 124 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 7 ms 604 KB Output is correct
5 Correct 5 ms 672 KB Output is correct
6 Correct 7 ms 600 KB Output is correct
7 Correct 7 ms 604 KB Output is correct
8 Correct 200 ms 980 KB Output is correct
9 Correct 184 ms 600 KB Output is correct
10 Correct 178 ms 776 KB Output is correct
11 Correct 163 ms 996 KB Output is correct
12 Correct 124 ms 748 KB Output is correct
13 Execution timed out 1071 ms 804 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 7 ms 604 KB Output is correct
5 Correct 5 ms 672 KB Output is correct
6 Correct 7 ms 600 KB Output is correct
7 Correct 7 ms 604 KB Output is correct
8 Correct 200 ms 980 KB Output is correct
9 Correct 184 ms 600 KB Output is correct
10 Correct 178 ms 776 KB Output is correct
11 Correct 163 ms 996 KB Output is correct
12 Correct 124 ms 748 KB Output is correct
13 Execution timed out 1071 ms 804 KB Time limit exceeded
14 Halted 0 ms 0 KB -