Submission #216623

# Submission time Handle Problem Language Result Execution time Memory
216623 2020-03-27T16:38:21 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<int,int> pii;
 
int N, M;
LL ans, ret;
int P[1000010], W[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;
}
 
inline 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:62:32: warning: format '%lld' expects argument of type 'long long int*', but argument 3 has type 'int*' [-Wformat=]
   scanf("%d %lld", &P[i], &W[i]);
                           ~~~~~^
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 19 ms 23808 KB Output is correct
2 Correct 19 ms 23808 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 19 ms 23808 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 20 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 18 ms 23808 KB Output is correct
10 Correct 18 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 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23936 KB Output is correct
2 Correct 25 ms 24192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24704 KB Output is correct
2 Correct 34 ms 26364 KB Output is correct
3 Correct 29 ms 24704 KB Output is correct
4 Correct 23 ms 24320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 27384 KB Output is correct
2 Correct 55 ms 29044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 31500 KB Output is correct
2 Correct 102 ms 40164 KB Output is correct
3 Correct 132 ms 46324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 37752 KB Output is correct
2 Correct 206 ms 55304 KB Output is correct
3 Correct 232 ms 72668 KB Output is correct
4 Correct 280 ms 78812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 309 ms 36956 KB Output is correct
2 Correct 595 ms 96212 KB Output is correct
3 Correct 314 ms 47616 KB Output is correct
4 Correct 393 ms 80280 KB Output is correct
5 Correct 392 ms 78172 KB Output is correct
6 Correct 773 ms 51820 KB Output is correct
7 Correct 439 ms 110272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 408 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -