답안 #151593

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
151593 2019-09-03T15:53:45 Z luciocf Islands (IOI08_islands) C++14
100 / 100
1503 ms 131072 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
 
const int maxn = 1e6+1;
const ll inf = 1e15+10;
 
int n;
 
int peso[maxn];
 
int firstCycle, lastCycle, backEdge;
int pai[maxn], edgePai[maxn];

ll maiorDist;
int indDist;

bitset<maxn> mark, markCycle;
 
vector<pii> grafo[maxn];
 
void findCycle(int u, int p)
{
	mark[u] = 1;
 
	for (auto pp: grafo[u])
	{
		int v = pp.first, e = pp.second;
		if (e == p) continue;
 
		if (mark[v])
		{
			firstCycle = u, lastCycle = v;
			backEdge = peso[e];
			continue;
		}
 
		pai[v] = u, edgePai[v] = peso[e];
		findCycle(v, e);
	}
}
 
void dfs(int u, int p, ll dist)
{
	if (dist >= maiorDist) indDist = u;
 
	maiorDist = max(maiorDist, dist);
 
	for (auto pp: grafo[u])
	{
		int v = pp.first, e = pp.second;
		if (v == p || markCycle[v]) continue;
 
		dfs(v, u, dist+1ll*peso[e]);
	}
}
 
int main(void)
{
	scanf("%d", &n);
 
	for (int i = 1; i <= n; i++)
	{
		int u, w;
		scanf("%d %d", &u, &w);
 
		peso[i] = w;
 
		grafo[u].push_back({i, i});
		grafo[i].push_back({u, i});
	}
 
	ll ans = 0;
 
	for (int T = 1; T <= n; T++)
	{
		if (mark[T]) continue;
 
		findCycle(T, 0);
 
		int at = lastCycle;
		ll soma = backEdge;
 
		vector<int> ciclo;
 	
		while (true)
		{
			ciclo.push_back(at);
			markCycle[at] = 1;
 
			if (at == firstCycle) break;
 
			soma += 1ll*edgePai[at];
			at = pai[at];
		}
 
		reverse(ciclo.begin(), ciclo.end());
 
		ll M = 0, diam = 0, pref = 0;

		ll maxi1 = -inf, maxi2 = -inf;
		for (int i = 0; i < ciclo.size(); i++)
		{
			int u = ciclo[i];

			if (i != 0) pref += 1ll*edgePai[u];

			maiorDist = 0;
			dfs(u, 0, 0);
 
			M = max(M, maiorDist+pref + maxi1);
			maxi1 = max(maxi1, maiorDist-pref);
 
			M = max(M, maiorDist-pref + maxi2 + soma);
			maxi2 = max(maxi2, maiorDist+pref);

			markCycle[u] = 0;
			dfs(indDist, 0, 0);
			markCycle[u] = 1;

			diam = max(diam, maiorDist);
		}
 
		ans += max(M, diam);
	}
 
	printf("%lld\n", ans);
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:105:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < ciclo.size(); i++)
                   ~~^~~~~~~~~~~~~~
islands.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &w);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 24 ms 23928 KB Output is correct
3 Correct 24 ms 23800 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 24 ms 23800 KB Output is correct
6 Correct 24 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23864 KB Output is correct
9 Correct 24 ms 23772 KB Output is correct
10 Correct 24 ms 23928 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 25 ms 23928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 24056 KB Output is correct
2 Correct 26 ms 24232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 24940 KB Output is correct
2 Correct 55 ms 26872 KB Output is correct
3 Correct 39 ms 25080 KB Output is correct
4 Correct 31 ms 24516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 28124 KB Output is correct
2 Correct 70 ms 30856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 132 ms 37132 KB Output is correct
2 Correct 133 ms 40576 KB Output is correct
3 Correct 166 ms 48112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 214 ms 49144 KB Output is correct
2 Correct 263 ms 64976 KB Output is correct
3 Correct 286 ms 69748 KB Output is correct
4 Correct 360 ms 85096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 441 ms 88728 KB Output is correct
2 Correct 1020 ms 114364 KB Output is correct
3 Correct 413 ms 74388 KB Output is correct
4 Correct 530 ms 98848 KB Output is correct
5 Correct 501 ms 99824 KB Output is correct
6 Correct 1478 ms 80940 KB Output is correct
7 Correct 554 ms 120560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 630 ms 131072 KB Output is correct
2 Correct 622 ms 131072 KB Output is correct
3 Correct 564 ms 131072 KB Output is correct
4 Correct 544 ms 78180 KB Output is correct
5 Correct 506 ms 103556 KB Output is correct
6 Correct 492 ms 88316 KB Output is correct
7 Correct 1503 ms 86612 KB Output is correct