답안 #1067928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067928 2024-08-21T05:38:39 Z 김은성(#11126) Paths (RMI21_paths) C++17
36 / 100
600 ms 816 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){
	lazy[v] = 0;
	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;
		par[r] = cost[r] =0;
		cnt = 0;
		tour.clear();
		tour.push_back(-1);
		settree(r);
		init(1,1,n);
		ll ans = 0;
		k = min(k, 100);
		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:78:12: warning: unused variable 'j' [-Wunused-variable]
   78 |  int k, i, j, a, b;
      |            ^
Main.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  scanf("%d %d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |   scanf("%d %d %lld", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 8 ms 600 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 11 ms 592 KB Output is correct
7 Correct 7 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 8 ms 600 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 11 ms 592 KB Output is correct
7 Correct 7 ms 596 KB Output is correct
8 Correct 200 ms 700 KB Output is correct
9 Correct 222 ms 604 KB Output is correct
10 Correct 175 ms 600 KB Output is correct
11 Correct 162 ms 604 KB Output is correct
12 Correct 120 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 8 ms 600 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 11 ms 592 KB Output is correct
7 Correct 7 ms 596 KB Output is correct
8 Correct 200 ms 700 KB Output is correct
9 Correct 222 ms 604 KB Output is correct
10 Correct 175 ms 600 KB Output is correct
11 Correct 162 ms 604 KB Output is correct
12 Correct 120 ms 716 KB Output is correct
13 Execution timed out 676 ms 816 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 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 8 ms 600 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 11 ms 592 KB Output is correct
7 Correct 7 ms 596 KB Output is correct
8 Correct 200 ms 700 KB Output is correct
9 Correct 222 ms 604 KB Output is correct
10 Correct 175 ms 600 KB Output is correct
11 Correct 162 ms 604 KB Output is correct
12 Correct 120 ms 716 KB Output is correct
13 Execution timed out 676 ms 816 KB Time limit exceeded
14 Halted 0 ms 0 KB -