Submission #198879

# Submission time Handle Problem Language Result Execution time Memory
198879 2020-01-28T01:14:04 Z arnold518 Islands (IOI08_islands) C++14
90 / 100
2000 ms 130656 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e6;

int N;
vector<pii> adj[MAXN+1];
pii to[MAXN+1];
ll ans, val;

bool vis[MAXN+1];
vector<int> cyc;
vector<ll> A, D;

ll dfs(int now, ll dep)
{
	vis[now]=true;
	ll ret=dep;

	ll ta=0, tb=0;

	for(pii &nxt : adj[now])
	{
		if(vis[nxt.first]) continue;
		ll p=dfs(nxt.first, dep+nxt.second);
		ret=max(ret, p);
		if(p>ta) tb=ta, ta=p;
		else if(p>tb) tb=p;
	}
	val=max(val, ta+tb-dep-dep);
	return ret;
}

int main()
{
	int i, j;

	scanf("%d", &N);
	for(i=1; i<=N; i++)
	{
		int v, w;
		scanf("%d%d", &v, &w);
		to[i]={v, w};
		adj[v].push_back({i, w});
	}

	for(i=1; i<=N; i++)
	{
		if(vis[i]) continue;

		int now=i;
		cyc.clear();
		while(!vis[now])
		{
			cyc.push_back(now);
			vis[now]=1;
			now=to[now].first;
		}

		reverse(cyc.begin(), cyc.end());
		while(!cyc.empty() && cyc.back()!=now) vis[cyc.back()]=0, cyc.pop_back();
		reverse(cyc.begin(), cyc.end());

		A=D=vector<ll>(0);
		ll s=0; val=0;

		D.push_back(0);
		for(i=0; i<cyc.size(); i++) A.push_back(dfs(cyc[i], 0));
		for(i=0; i<cyc.size(); i++) D.push_back(D.back()+to[cyc[i]].second), s+=to[cyc[i]].second;
		D.pop_back();

		ll t=-1e18;
		for(j=0; j<cyc.size(); j++)
		{
			if(j) val=max(val, D[j]+A[j]+t);
			t=max(t, A[j]-D[j]);
		}

		t=-1e18;
		for(j=0; j<cyc.size(); j++)
		{
			if(j) val=max(val, s-D[j]+A[j]+t);
			t=max(t, D[j]+A[j]);
		}

		ans+=val;
	}
	printf("%lld", ans);
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:72:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=0; i<cyc.size(); i++) A.push_back(dfs(cyc[i], 0));
            ~^~~~~~~~~~~
islands.cpp:73:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=0; i<cyc.size(); i++) D.push_back(D.back()+to[cyc[i]].second), s+=to[cyc[i]].second;
            ~^~~~~~~~~~~
islands.cpp:77:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0; j<cyc.size(); j++)
            ~^~~~~~~~~~~
islands.cpp:84:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0; j<cyc.size(); j++)
            ~^~~~~~~~~~~
islands.cpp:42:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
islands.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Correct 19 ms 23800 KB Output is correct
4 Correct 18 ms 23800 KB Output is correct
5 Correct 19 ms 23800 KB Output is correct
6 Correct 19 ms 23800 KB Output is correct
7 Correct 18 ms 23800 KB Output is correct
8 Correct 19 ms 23864 KB Output is correct
9 Correct 19 ms 23800 KB Output is correct
10 Correct 20 ms 23800 KB Output is correct
11 Correct 19 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23800 KB Output is correct
2 Correct 20 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23800 KB Output is correct
2 Correct 21 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 24312 KB Output is correct
2 Correct 38 ms 25208 KB Output is correct
3 Correct 28 ms 24440 KB Output is correct
4 Correct 25 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 25720 KB Output is correct
2 Correct 61 ms 27548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 30196 KB Output is correct
2 Correct 110 ms 35272 KB Output is correct
3 Correct 141 ms 35560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 36620 KB Output is correct
2 Correct 217 ms 42236 KB Output is correct
3 Correct 228 ms 54672 KB Output is correct
4 Correct 291 ms 52316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 331 ms 40508 KB Output is correct
2 Correct 742 ms 65484 KB Output is correct
3 Correct 355 ms 47156 KB Output is correct
4 Correct 406 ms 60896 KB Output is correct
5 Correct 402 ms 59064 KB Output is correct
6 Correct 951 ms 52876 KB Output is correct
7 Correct 441 ms 70356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 500 ms 130656 KB Output is correct
2 Correct 472 ms 95480 KB Output is correct
3 Correct 477 ms 88016 KB Output is correct
4 Execution timed out 2090 ms 63340 KB Time limit exceeded
5 Halted 0 ms 0 KB -