답안 #151550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
151550 2019-09-03T14:16:51 Z luciocf Islands (IOI08_islands) C++14
90 / 100
1443 ms 131076 KB
#include <bits/stdc++.h>

#define ff first
#define ss second
 
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 pref[maxn];
ll maiorDist[maxn], maiorDist2;
int indDist;
 
bitset<maxn> mark, markCycle;
 
vector<pii> grafo[maxn];
 
void findCycle(int u, int p)
{
	mark[u] = 1;
 
	for (auto v: grafo[u])
	{
		if (v.ss == p) continue;
 
		if (mark[v.ff])
		{
			firstCycle = u, lastCycle = v.ff;
			backEdge = peso[v.ss];
			continue;
		}
 
		pai[v.ff] = u, edgePai[v.ff] = peso[v.ss];
		findCycle(v.ff, v.ss);
	}
}
 
void dfs(int u, int p, int root, ll dist)
{
	if (dist >= maiorDist[root])
		indDist = u;
 
	maiorDist[root] = max(maiorDist[root], dist);
 
	for (auto v: grafo[u])
		if (v.ff != p && !markCycle[v.ff])
 			dfs(v.ff, u, root, dist+1ll*peso[v.ss]);
}
 
void dfs2(int u, int p, ll dist)
{
	maiorDist2 = max(maiorDist2, dist);
 
	for (auto v: grafo[u])
		if (v.ff != p && !markCycle[v.ff])
 			dfs2(v.ff, u, dist+1ll*peso[v.ss]);
}
 
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;
	vector<int> ciclo;
 
	for (int T = 1; T <= n; T++)
	{
		if (mark[T]) continue;
 
		findCycle(T, 0);
 
		int at = lastCycle;
		ll soma = backEdge;
 
		ciclo.clear();
 
		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;
 
		for (int i = 0; i < ciclo.size(); i++)
		{
			int c = ciclo[i];
 
			if (i > 0)
				pref[i] = pref[i-1] + 1ll*edgePai[c];
 
			dfs(c, 0, c, 0);
 
			maiorDist2 = 0;
			markCycle[c] = 0;
 
			dfs2(indDist, 0, 0);
			markCycle[c] = 1;
 
			diam = max(diam, maiorDist2);
		}
 
		ll maxi1 = -inf, maxi2 = -inf;
		for (int i = 0; i < ciclo.size(); i++)
		{
			int u = ciclo[i];
 
			M = max(M, maiorDist[u]+pref[i] + maxi1);
			maxi1 = max(maxi1, maiorDist[u]-pref[i]);
 
			M = max(M, maiorDist[u]-pref[i] + maxi2 + soma);
 
			maxi2 = max(maxi2, maiorDist[u]+pref[i]);
		}
 
		ans += max(M, diam);
	}
 
	printf("%lld\n", ans);
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:114:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < ciclo.size(); i++)
                   ~~^~~~~~~~~~~~~~
islands.cpp:133:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < ciclo.size(); i++)
                   ~~^~~~~~~~~~~~~~
islands.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:77: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 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 23 ms 23928 KB Output is correct
4 Correct 23 ms 23800 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 23 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 23 ms 23800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 25 ms 24200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 24768 KB Output is correct
2 Correct 50 ms 26864 KB Output is correct
3 Correct 40 ms 25080 KB Output is correct
4 Correct 30 ms 24440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 28048 KB Output is correct
2 Correct 70 ms 30892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 132 ms 36480 KB Output is correct
2 Correct 134 ms 39616 KB Output is correct
3 Correct 163 ms 47076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 47136 KB Output is correct
2 Correct 272 ms 64620 KB Output is correct
3 Correct 292 ms 69396 KB Output is correct
4 Correct 356 ms 83272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 419 ms 74984 KB Output is correct
2 Correct 949 ms 106728 KB Output is correct
3 Correct 394 ms 70236 KB Output is correct
4 Correct 527 ms 98632 KB Output is correct
5 Correct 500 ms 99252 KB Output is correct
6 Correct 1443 ms 72632 KB Output is correct
7 Correct 545 ms 116108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 564 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -