답안 #198900

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
198900 2020-01-28T02:47:05 Z arnold518 Islands (IOI08_islands) C++14
90 / 100
2000 ms 130532 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;
ll A[MAXN+1], D[MAXN+1];
int AS, DS;

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=vector<int>(0);
		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());

		AS=DS=0;
		ll s=0; val=0;

		D[DS++]=0;
		for(i=0; i<cyc.size(); i++) A[AS++]=dfs(cyc[i], 0);
		for(i=0; i<cyc.size(); i++) D[DS++]=(D[DS-1]+to[cyc[i]].second), s+=to[cyc[i]].second;
		DS--;

		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:73:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=0; i<cyc.size(); i++) A[AS++]=dfs(cyc[i], 0);
            ~^~~~~~~~~~~
islands.cpp:74:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=0; i<cyc.size(); i++) D[DS++]=(D[DS-1]+to[cyc[i]].second), s+=to[cyc[i]].second;
            ~^~~~~~~~~~~
islands.cpp:74:35: warning: operation on 'DS' may be undefined [-Wsequence-point]
   for(i=0; i<cyc.size(); i++) D[DS++]=(D[DS-1]+to[cyc[i]].second), s+=to[cyc[i]].second;
                                 ~~^~
islands.cpp:78:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0; j<cyc.size(); j++)
            ~^~~~~~~~~~~
islands.cpp:85:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0; j<cyc.size(); j++)
            ~^~~~~~~~~~~
islands.cpp:43:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
islands.cpp:47:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23800 KB Output is correct
2 Correct 19 ms 23800 KB Output is correct
3 Correct 19 ms 23928 KB Output is correct
4 Correct 20 ms 23800 KB Output is correct
5 Correct 21 ms 23800 KB Output is correct
6 Correct 20 ms 23800 KB Output is correct
7 Correct 21 ms 23800 KB Output is correct
8 Correct 20 ms 23800 KB Output is correct
9 Correct 20 ms 23800 KB Output is correct
10 Correct 19 ms 23800 KB Output is correct
11 Correct 19 ms 23800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23928 KB Output is correct
2 Correct 21 ms 23928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23928 KB Output is correct
2 Correct 22 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 24312 KB Output is correct
2 Correct 41 ms 25080 KB Output is correct
3 Correct 30 ms 24440 KB Output is correct
4 Correct 28 ms 24092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 25592 KB Output is correct
2 Correct 58 ms 27384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 30072 KB Output is correct
2 Correct 104 ms 34772 KB Output is correct
3 Correct 134 ms 33776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 176 ms 36596 KB Output is correct
2 Correct 203 ms 40812 KB Output is correct
3 Correct 224 ms 53748 KB Output is correct
4 Correct 281 ms 49508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 353 ms 40660 KB Output is correct
2 Correct 704 ms 63336 KB Output is correct
3 Correct 367 ms 47064 KB Output is correct
4 Correct 404 ms 57412 KB Output is correct
5 Correct 390 ms 55400 KB Output is correct
6 Correct 922 ms 52984 KB Output is correct
7 Correct 434 ms 62816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 491 ms 130532 KB Output is correct
2 Correct 486 ms 95532 KB Output is correct
3 Correct 452 ms 83548 KB Output is correct
4 Execution timed out 2081 ms 63304 KB Time limit exceeded
5 Halted 0 ms 0 KB -