Submission #198997

# Submission time Handle Problem Language Result Execution time Memory
198997 2020-01-28T15:06:57 Z MetB Islands (IOI08_islands) C++14
100 / 100
1040 ms 130528 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(auto 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 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;
		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.clear(); D.clear();
		ll s=0; val=0;
 
		D.push_back(0);
		for(auto it : cyc) A.push_back(dfs(it, 0));
		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: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 20 ms 23800 KB Output is correct
3 Correct 19 ms 23800 KB Output is correct
4 Correct 19 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 19 ms 23804 KB Output is correct
8 Correct 21 ms 23800 KB Output is correct
9 Correct 21 ms 23800 KB Output is correct
10 Correct 22 ms 23800 KB Output is correct
11 Correct 21 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 23800 KB Output is correct
2 Correct 22 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 24312 KB Output is correct
2 Correct 37 ms 25336 KB Output is correct
3 Correct 31 ms 24440 KB Output is correct
4 Correct 28 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 25720 KB Output is correct
2 Correct 60 ms 27480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 30068 KB Output is correct
2 Correct 106 ms 35056 KB Output is correct
3 Correct 140 ms 35540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 36672 KB Output is correct
2 Correct 215 ms 42336 KB Output is correct
3 Correct 229 ms 53304 KB Output is correct
4 Correct 295 ms 52444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 367 ms 40656 KB Output is correct
2 Correct 790 ms 65640 KB Output is correct
3 Correct 332 ms 46968 KB Output is correct
4 Correct 413 ms 60768 KB Output is correct
5 Correct 410 ms 59104 KB Output is correct
6 Correct 1024 ms 52984 KB Output is correct
7 Correct 449 ms 70356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 492 ms 130528 KB Output is correct
2 Correct 467 ms 95480 KB Output is correct
3 Correct 471 ms 88156 KB Output is correct
4 Correct 432 ms 63864 KB Output is correct
5 Correct 408 ms 58844 KB Output is correct
6 Correct 398 ms 57836 KB Output is correct
7 Correct 1040 ms 53368 KB Output is correct