답안 #216571

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
216571 2020-03-27T14:31:07 Z songc Islands (IOI08_islands) C++14
90 / 100
773 ms 131076 KB
#include <bits/stdc++.h>
#define pb push_back
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define INF (1ll<<60)
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;

int N, M;
LL ans, ret, W[1000010];
int P[1000010];
bool chk[1000010];
vector<int> adj[1000010];
vector<LL> V, S;

int cyc(int u){
	if (chk[u]) return u;
	chk[u] = true;
	int r = cyc(P[u]);
	if (r){
		V.pb(u), S.pb(W[u]);
		return (r==u)?0:r;
	}
	chk[u] = false;
	return 0;
}

LL dfs(int u){
	LL mx=0;
	chk[u] = true;
	for (int v : adj[u]){
		if (chk[v]) continue;
		LL r = dfs(v) + W[v];
		ret = max(ret, mx+r);
		mx = max(mx, r);
	}
	return mx;
}

void dia(int u){
	V.clear(), S.clear();
	cyc(u);
	reverse(all(V)); reverse(all(S));
	for (LL &v : V) v = dfs(v);
	M = V.size();
	for (int i=M-1; i>0; i--) swap(S[i], S[i-1]);
	for (int i=1; i<M; i++) S[i] += S[i-1];
	LL m1=-INF, m2=-INF;
	for (int i=0; i<M; i++){
		ret = max(ret, m1+S[i]+V[i]);
		ret = max(ret, m2-S[i]+V[i]+S[M-1]);
		m1 = max(m1, V[i]-S[i]);
		m2 = max(m2, V[i]+S[i]);
	}
}

int main(){
	scanf("%d", &N);
	for (int i=1; i<=N; i++){
		scanf("%d %lld", &P[i], &W[i]);
		adj[P[i]].pb(i);
	}
	for (int i=1; i<=N; i++){
		if (chk[i]) continue;
		dia(i);
		ans+=ret, ret=0;
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
islands.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %lld", &P[i], &W[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 22 ms 23936 KB Output is correct
3 Correct 18 ms 23936 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 18 ms 23808 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 21 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23936 KB Output is correct
2 Correct 18 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 23936 KB Output is correct
2 Correct 19 ms 24192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 24704 KB Output is correct
2 Correct 34 ms 26496 KB Output is correct
3 Correct 26 ms 24824 KB Output is correct
4 Correct 23 ms 24448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 27640 KB Output is correct
2 Correct 54 ms 30072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 33896 KB Output is correct
2 Correct 102 ms 41828 KB Output is correct
3 Correct 131 ms 46684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 41048 KB Output is correct
2 Correct 198 ms 56104 KB Output is correct
3 Correct 227 ms 72796 KB Output is correct
4 Correct 279 ms 77276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 302 ms 42844 KB Output is correct
2 Correct 600 ms 94300 KB Output is correct
3 Correct 324 ms 52344 KB Output is correct
4 Correct 380 ms 81372 KB Output is correct
5 Correct 380 ms 79196 KB Output is correct
6 Correct 773 ms 57464 KB Output is correct
7 Correct 430 ms 106740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 469 ms 131072 KB Output is correct
2 Correct 408 ms 106744 KB Output is correct
3 Runtime error 403 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Halted 0 ms 0 KB -