Submission #198791

# Submission time Handle Problem Language Result Execution time Memory
198791 2020-01-27T17:09:58 Z arnold518 Islands (IOI08_islands) C++14
67 / 100
1960 ms 131076 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<pll> adj[MAXN+10];
pll to[MAXN+10];
ll ans;

bool vis[MAXN+10], chk[MAXN+10];

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

	pll t(0, 0);

	for(auto nxt : adj[now])
	{
		if(vis[nxt.first]) continue;
		ret=max(ret, dfs(nxt.first, dep+nxt.second, tt));
		if(dep+nxt.second>t.first) t.second=t.first, t.first=dep+nxt.second;
		else if(dep+nxt.second>t.second) t.second=dep+nxt.second;
	}
	tt=max(tt, t.first+t.second);
	return ret;
}

int main()
{
	int i, j;

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

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

		int now=i;
		vector<int> cyc;
		while(!chk[now])
		{
			cyc.push_back(now);
			chk[now]=1;
			now=to[now].first;
		}

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

		vector<ll> A, D;
		ll val=0, s=0;

		D.push_back(0);
		for(auto it : cyc) vis[it]=1;
		for(auto it : cyc) A.push_back(dfs(it, 0, val));
		for(auto it : cyc) D.push_back(D.back()+to[it].second), s+=to[it].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:75:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0; j<cyc.size(); j++)
            ~^~~~~~~~~~~
islands.cpp:82:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0; j<cyc.size(); j++)
            ~^~~~~~~~~~~
islands.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
islands.cpp:43: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 Incorrect 20 ms 23800 KB Output isn't correct
3 Correct 21 ms 23928 KB Output is correct
4 Correct 19 ms 23804 KB Output is correct
5 Correct 19 ms 23800 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 20 ms 23800 KB Output is correct
8 Correct 20 ms 23800 KB Output is correct
9 Correct 19 ms 23800 KB Output is correct
10 Correct 21 ms 23800 KB Output is correct
11 Correct 19 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23928 KB Output is correct
2 Correct 21 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 23928 KB Output is correct
2 Correct 21 ms 24064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 24568 KB Output is correct
2 Correct 38 ms 25888 KB Output is correct
3 Correct 33 ms 24696 KB Output is correct
4 Incorrect 26 ms 24312 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 26612 KB Output is correct
2 Correct 61 ms 28728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 33268 KB Output is correct
2 Correct 107 ms 37364 KB Output is correct
3 Correct 133 ms 39400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 42024 KB Output is correct
2 Correct 229 ms 50532 KB Output is correct
3 Correct 234 ms 60104 KB Output is correct
4 Correct 300 ms 61768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 352 ms 56276 KB Output is correct
2 Correct 752 ms 98016 KB Output is correct
3 Correct 345 ms 69696 KB Output is correct
4 Correct 432 ms 88284 KB Output is correct
5 Correct 412 ms 87520 KB Output is correct
6 Incorrect 919 ms 81600 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1960 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -