Submission #151580

#TimeUsernameProblemLanguageResultExecution timeMemory
151580luciocfIslands (IOI08_islands)C++14
90 / 100
1475 ms131072 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
 
const int maxn = 1e6+50;
const ll inf = 1e15+10;
 
int n;
 
int peso[maxn];

int in[maxn], tt;
 
int firstCycle, lastCycle, backEdge;
int pai[maxn], edgePai[maxn];

ll pref[maxn];
ll maiorDist[maxn], maiorDist2;
int indDist;

bool mark[maxn], markCycle[maxn];
 
vector<pii> grafo[maxn];
 
void findCycle(int u)
{
	mark[u] = 1;
	in[u] = ++tt;
 
	for (auto pp: grafo[u])
	{
		int v = pp.first, e = pp.second;
 
		if (in[v] > in[u])
		{
			firstCycle = u, lastCycle = v;
			backEdge = peso[e];
			continue;
		}
		else if (mark[v]) continue;
 
		pai[v] = u, edgePai[v] = peso[e];
		findCycle(v);
	}
}
 
void dfs(int u, int p, int root, ll dist)
{
	if (dist >= maiorDist[root])
		indDist = u;
 
	maiorDist[root] = max(maiorDist[root], dist);
 
	for (auto pp: grafo[u])
	{
		int v = pp.first, e = pp.second;
		if (v == p || markCycle[v]) continue;
 
		dfs(v, u, root, dist+1ll*peso[e]);
	}
}
 
void dfs2(int u, int p, ll dist)
{
	maiorDist2 = max(maiorDist2, dist);
 
	for (auto pp: grafo[u])
	{
		int v = pp.first, e = pp.second;
		if (v == p || markCycle[v]) continue;
 
		dfs2(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;
	vector<int> ciclo;
 
	for (int T = 1; T <= n; T++)
	{
		if (mark[T]) continue;
 
		findCycle(T);
 
		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 (stderr)

islands.cpp: In function 'int main()':
islands.cpp:123:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < ciclo.size(); i++)
                   ~~^~~~~~~~~~~~~~
islands.cpp:142:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < ciclo.size(); i++)
                   ~~^~~~~~~~~~~~~~
islands.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &w);
   ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...