Submission #216617

# Submission time Handle Problem Language Result Execution time Memory
216617 2020-03-27T16:19:58 Z songc Islands (IOI08_islands) C++14
90 / 100
803 ms 131072 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<pii> V;

int cyc(int u){
	if (chk[u]) return u;
	chk[u] = true;
	int r = cyc(P[u]);
	if (r){
		V.pb(pii(u, 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();
	cyc(u);
	reverse(all(V));
	for (pii &v : V) v.first = dfs(v.first);
	M = V.size();
	for (int i=M-1; i>0; i--) swap(V[i].se, V[i-1].se);
	for (int i=1; i<M; i++) V[i].se += V[i-1].se;
	LL m1=-INF, m2=-INF;
	for (int i=0; i<M; i++){
		ret = max(ret, m1+V[i].se+V[i].fi);
		ret = max(ret, m2-V[i].se+V[i].fi+V[M-1].se);
		m1 = max(m1, V[i].fi-V[i].se);
		m2 = max(m2, V[i].fi+V[i].se);
	}
}

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]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 19 ms 23936 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 21 ms 23808 KB Output is correct
6 Correct 19 ms 23784 KB Output is correct
7 Correct 19 ms 23808 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 19 ms 23808 KB Output is correct
10 Correct 19 ms 23808 KB Output is correct
11 Correct 19 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23936 KB Output is correct
2 Correct 20 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23936 KB Output is correct
2 Correct 21 ms 24192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 24956 KB Output is correct
2 Correct 35 ms 26744 KB Output is correct
3 Correct 29 ms 24808 KB Output is correct
4 Correct 29 ms 24320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 28152 KB Output is correct
2 Correct 55 ms 30456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 34548 KB Output is correct
2 Correct 104 ms 43244 KB Output is correct
3 Correct 133 ms 51308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 44408 KB Output is correct
2 Correct 203 ms 63592 KB Output is correct
3 Correct 235 ms 79836 KB Output is correct
4 Correct 302 ms 92388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 320 ms 56028 KB Output is correct
2 Correct 620 ms 115044 KB Output is correct
3 Correct 305 ms 61048 KB Output is correct
4 Correct 413 ms 98408 KB Output is correct
5 Correct 384 ms 95972 KB Output is correct
6 Correct 803 ms 70264 KB Output is correct
7 Correct 455 ms 131072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 407 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -