Submission #397076

# Submission time Handle Problem Language Result Execution time Memory
397076 2021-05-01T09:44:26 Z Berted Islands (IOI08_islands) C++14
9 / 100
676 ms 83036 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <bitset>
#include <fstream>
#define ll long long
#define pii pair<int, int>
#define fst first
#define snd second
 
using namespace std;
 
int N, deg[1000001];
vector<pii> adj[1000001];

int d1 = -1; 
vector<pii> cyc;
ll DP[1000001], D2[1000001];
deque<pair<ll, int>> dq;
 
ll tmp[2], ans = 0;
 
int main()
{
	//freopen("islands.in", "r", stdin);
	//ifstream fin("islands.in");
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		int u, c; cin >> u >> c; u--;
		adj[u].push_back({i, c}); deg[u]++;
		adj[i].push_back({u, c}); deg[i]++;
	}

	vector<int> S;
	for (int i = 0; i < N; i++)
	{
		if (deg[i] == 1) S.push_back(i);
	}

	while (S.size())
	{
		int u = S.back(); S.pop_back();
		deg[u] = 0; tmp[0] = tmp[1] = 0;
		for (const auto &v : adj[u])
		{
			if (deg[v.fst]) 
			{
				deg[v.fst]--;
				if (deg[v.fst] < 2) S.push_back(v.fst);
			}
			else
			{
				DP[u] = max(DP[u], DP[v.fst] + v.snd);
				D2[u] = max(D2[u], D2[v.fst]);
				if (DP[v.fst] + v.snd > tmp[0]) {tmp[1] = tmp[0]; tmp[0] = DP[v.fst] + v.snd;}
				else if (DP[v.fst] + v.snd > tmp[1]) {tmp[1] = DP[v.fst] + v.snd;}
			}
		}
		D2[u] = max(D2[u], tmp[0] + tmp[1]);
		//cerr << u << " " << DP[u] << " " << D2[u] << "\n";
	}
 
	for (int k = 0; k < N; k++)
	{
		if (deg[k])
		{
			int u = k; ll rr = 0;
			while (u != -1)
			{
				//cerr << "Cur: " << u << "\n";
				tmp[0] = tmp[1] = 0;
				pii nex = {-1, -1};
				for (const auto &v : adj[u])
				{
					if (cyc.size() && v == cyc.back()) continue;
					if (deg[v.fst]) {nex = v;}
					else
					{
						DP[u] = max(DP[u], DP[v.fst] + v.snd);
						D2[u] = max(D2[u], D2[v.fst]);
						if (DP[v.fst] + v.snd > tmp[0]) {tmp[1] = tmp[0]; tmp[0] = DP[v.fst] + v.snd;}
						else if (DP[v.fst] + v.snd > tmp[1]) {tmp[1] = DP[v.fst] + v.snd;}
					}
				}
				D2[u] = max(D2[u], tmp[0] + tmp[1]); rr = max(rr, D2[u]);
				if (cyc.size() && cyc.front().fst == nex.fst) {nex.fst = -1;}
				else if (nex.fst == -1)
				{
					for (const auto &v : adj[u])
						if (v == cyc.back()) {nex = {-1, v.snd};}
				}
				//cerr << "Pushed: " << u << ", " << nex.snd << "\n";
				cyc.push_back({u, nex.snd});
				u = nex.fst;
			}
 
			//cerr << k << ": " << rr << " " << cyc.size() << "\n";
 
			ll s = 0, t = 0;
			for (int i = 1; i < cyc.size(); i++)
			{
				swap(cyc[i - 1].snd, cyc[i].snd);
				s += cyc[i].snd;
				while (dq.size() && dq.back().fst <= s + DP[cyc[i].fst]) {dq.pop_back();}
				dq.push_back({s + DP[cyc[i].fst], i});
			}
			s += cyc[0].snd;
 
			for (int i = 0; i < cyc.size(); i++)
			{
				deg[cyc[i].fst] = 0;
				if (dq.front().snd == i) dq.pop_front();
				rr = max(rr, dq.front().fst + t + DP[cyc[i].fst]);
				while (dq.size() && dq.back().fst + t <= s + DP[cyc[i].fst]) {dq.pop_back();}
				dq.push_back({s + DP[cyc[i].fst] - t, i});
				t -= cyc[(i + 1) % cyc.size()].snd;
			}
 
			dq.clear();
			cyc.clear();
 
			ans += rr;
		}
	}
	
	cout << ans << "\n";
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:103:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |    for (int i = 1; i < cyc.size(); i++)
      |                    ~~^~~~~~~~~~~~
islands.cpp:112:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |    for (int i = 0; i < cyc.size(); i++)
      |                    ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 23808 KB Output isn't correct
2 Incorrect 14 ms 23756 KB Output isn't correct
3 Incorrect 14 ms 23844 KB Output isn't correct
4 Correct 14 ms 23744 KB Output is correct
5 Incorrect 15 ms 23756 KB Output isn't correct
6 Incorrect 16 ms 23908 KB Output isn't correct
7 Incorrect 15 ms 23808 KB Output isn't correct
8 Incorrect 14 ms 23700 KB Output isn't correct
9 Incorrect 15 ms 23756 KB Output isn't correct
10 Correct 14 ms 23756 KB Output is correct
11 Correct 14 ms 23748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 23884 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 23924 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 24656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 35 ms 27108 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 91 ms 35864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 149 ms 45908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 339 ms 82804 KB Output is correct
2 Incorrect 676 ms 83036 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 355 ms 74892 KB Output is correct
2 Correct 395 ms 74744 KB Output is correct
3 Incorrect 388 ms 75052 KB Output isn't correct
4 Halted 0 ms 0 KB -