제출 #109950

#제출 시각아이디문제언어결과실행 시간메모리
109950MetBIslands (IOI08_islands)C++14
80 / 100
1334 ms132096 KiB
#include <iostream>
#include <cstdlib>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <queue>
#include <math.h>
#include <stack>
#include <vector>
#include <string.h>
#include <random>
 
typedef long long ll;
 
const ll MOD = 1e9 + 7, INF = 1e18;
 
using namespace std;

int n, m, p[1000001], cost[1000001];
bitset <1000000> is_cycle, u, c;
ll d[1000001][2];
ll pref[2000001];
deque < pair <ll, int> > q_max;

vector < pair <int, int> > g[1000001];
pair <int, int> g_un[1000001];

vector < pair <int, int> > cycle[1000001];

void bfs_traverse (ll x)
{
	queue <int> q;

	q.push (x);
	c[x] = 1;

	while (!q.empty ())
	{
		int x = q.front ();
		q.pop ();

		for (auto to : g[x])
			if (!c[to.first])
			{
				c[to.first] = 1;
				q.push (to.first);
			}
	}
}
 
bool bfs_cycle (ll x, ll color)
{
	while (!u[x])
	{
		u[x] = 1;
		p[g_un[x].first] = x;
		cost[g_un[x].first] = g_un[x].second;
		if (u[g_un[x].first] == 1) break;
		x = g_un[x].first;
	}

	int v = x;

	auto to = g_un[x];

	while (v != to.first)
	{
		cycle[color].emplace_back (v, cost[v]);
		is_cycle[v] = true;
		v = p[v];
	}
	cycle[color].emplace_back (to.first, to.second);
	is_cycle[to.first] = true;
	return true;
}

ll find_diameter (ll x, ll p)
{
	ll sum = 0, mx = 0, smx = 0;

	for (auto to : g[x])
		if (to.first != p && !is_cycle[to.first])
		{
			sum = max (sum, find_diameter (to.first, x));

			if (mx <= d[to.first][0] + to.second)
			{
				smx = mx;
				mx = d[to.first][0] + to.second;
			}
			else smx = max (smx, d[to.first][0] + to.second);
		}

	d[x][0] = mx;
	d[x][1] = smx;
	return max (sum, mx + smx);
}

ll process (vector < pair <int, int> > & cycle)
{
	ll diameter = 0;
	for (auto x : cycle)
		diameter = max (diameter, find_diameter (x.first, -1));

	ll s = cycle.size ();

	for (ll i = 2 * cycle.size () - 1; i >= 0; i--)
		pref[i] = cycle[i % s].second + (i != 2 * cycle.size () - 1 ? pref[i+1] : 0);

	q_max.clear ();

	for (ll i = 0; i < s - 1; i++)
	{
		while (!q_max.empty () && q_max.back ().first <= pref[i] + d[cycle[i % s].first][0])
			q_max.pop_back ();

		q_max.emplace_back (pref[i] + d[cycle[i % s].first][0], i);
	}

	for (ll i = s - 1; i < 2 * cycle.size (); i++)
	{
		ll k = q_max.front ().first;
		diameter = max (diameter, k - pref[i] + d[cycle[i % s].first][0]);

		while (!q_max.empty () && q_max.back ().first <= pref[i] + d[cycle[i % s].first][0])
			q_max.pop_back ();

		q_max.emplace_back (pref[i] + d[cycle[i % s].first][0], i);

		while (!q_max.empty () && q_max.front ().second <= i - s + 1)
			q_max.pop_front ();
	}

	return diameter;
}

int main ()
{
	cin >> n;

	for (ll i = 0; i < n; i++)
	{
		ll x, l;

		scanf ("%lld%lld", &x, &l);

		g[i].emplace_back (x - 1, l);
		g[x - 1].emplace_back (i, l);

		g_un[i] = {x - 1, l};
	}

	ll color = 0;

	for (ll i = 0; i < n; i++)
		if (!u[i] && !c[i])
		{
			bfs_cycle (i, ++color);
			bfs_traverse (i);
		}


	ll ans = 0;

	for (ll i = 1; i <= color; i++)
		ans += process (cycle[i]);

	cout << ans;
}

컴파일 시 표준 에러 (stderr) 메시지

islands.cpp: In function 'll process(std::vector<std::pair<int, int> >&)':
islands.cpp:110:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   pref[i] = cycle[i % s].second + (i != 2 * cycle.size () - 1 ? pref[i+1] : 0);
                                    ~~^~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp:122:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (ll i = s - 1; i < 2 * cycle.size (); i++)
                     ~~^~~~~~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:147:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%lld%lld", &x, &l);
   ~~~~~~^~~~~~~~~~~~~~~~~~~~
#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...