답안 #397064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
397064 2021-05-01T09:06:06 Z Berted Islands (IOI08_islands) C++14
90 / 100
1035 ms 131072 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;
vector<pii> adj[1000001];

bitset<1000001> bt; vector<int> vis;
int d1 = -1; 
vector<pii> cyc;
ll DP[1000001], D2[1000001];
deque<pair<ll, int>> dq;
 
ll tmp[2], ans = 0;
 
inline void DFS1(int u, pii p)
{
	bt[u] = 1;
	vis.push_back(u);
	//cerr << "D1: " << u << " " << H[u] << "\n";
	for (const pii &v : adj[u])
	{
		if (p == v) {p = {-1, -1}; continue;}
		if (!bt[v.fst]) DFS1(v.fst, {u, v.snd});
		else {d1 = v.fst;}

		if (d1 == -2) break;
		if (d1 != -1)
		{
			cyc.push_back({u, v.snd});
			if (d1 == u) d1 = -2;
			break;
		}
	}
}
 
inline void DFS2(int u)
{
	bt[u] = 1;
	for (const pii &v : adj[u])
	{
		if (!bt[v.fst])
		{
			DFS2(v.fst);
			D2[u] = max(D2[u], D2[v.fst]);
			DP[u] = max(DP[u], DP[v.fst] + v.snd);
			bt[v.fst] = 0;
		}
	}
	tmp[0] = tmp[1] = 0;
	for (const pii &v : adj[u])
	{
		if (!bt[v.fst])
		{
			bt[v.fst] = 1;
			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]);
}
 
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});
		adj[i].push_back({u, c});
	}

	//cerr << "adj\n";
 
	for (int k = 0; k < N; k++)
	{
		if (!bt[k])
		{
			ll rr = 0; d1 = -1;
			DFS1(k, {-1, -1});

			//cerr << "D1done\n";

			for (const auto &u : vis) bt[u] = 0;
			vis.clear();
			
			for (const auto &u : cyc) bt[u.fst] = 1;
			for (const auto &u : cyc) {DFS2(u.fst); rr = max(rr, D2[u.fst]);}
 
			//cerr << k << ": " << rr << " " << cyc.size() << "\n";
 
			ll s = 0, t = 0;
			for (int i = 1; i < cyc.size(); i++)
			{
				//cerr << "id: " << i << " " << cyc[i].fst << " - " << DP[cyc[i].fst] << "\n";
				s += cyc[i].snd;
				while (dq.size() && dq.back().fst <= s + DP[cyc[i].fst]) 
				{
					//cerr << "Popped\n";
					dq.pop_back();
				}
				//cerr << "Pushed: " << s + DP[cyc[i].fst] << ", " << i << "\n";
				dq.push_back({s + DP[cyc[i].fst], i});
			}
			s += cyc[0].snd;
 
			for (int i = 0; i < cyc.size(); i++)
			{
				if (dq.front().snd == i) dq.pop_front();
				//cerr << i << " " << " " << cyc[i].fst << " " << dq.front().fst << " " << t << " " << DP[cyc[i].fst] << "\n";
				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();
 
			//cerr << k << ": " << rr << "\n";
 
			ans += rr;
		}
	}
	
	cout << ans << "\n";
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:105: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]
  105 |    for (int i = 1; i < cyc.size(); i++)
      |                    ~~^~~~~~~~~~~~
islands.cpp:119: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]
  119 |    for (int i = 0; i < cyc.size(); i++)
      |                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Correct 16 ms 23724 KB Output is correct
3 Correct 13 ms 23756 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 13 ms 23756 KB Output is correct
6 Correct 13 ms 23728 KB Output is correct
7 Correct 13 ms 23704 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 14 ms 23800 KB Output is correct
10 Correct 13 ms 23756 KB Output is correct
11 Correct 13 ms 23756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23884 KB Output is correct
2 Correct 14 ms 23824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23884 KB Output is correct
2 Correct 15 ms 24208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 25036 KB Output is correct
2 Correct 31 ms 27640 KB Output is correct
3 Correct 25 ms 25276 KB Output is correct
4 Correct 19 ms 24464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 29128 KB Output is correct
2 Correct 50 ms 31756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 38816 KB Output is correct
2 Correct 98 ms 42684 KB Output is correct
3 Correct 125 ms 54452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 50464 KB Output is correct
2 Correct 217 ms 75440 KB Output is correct
3 Correct 241 ms 77152 KB Output is correct
4 Correct 319 ms 102184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 354 ms 93036 KB Output is correct
2 Correct 745 ms 131072 KB Output is correct
3 Correct 303 ms 77508 KB Output is correct
4 Correct 400 ms 116460 KB Output is correct
5 Correct 396 ms 116908 KB Output is correct
6 Correct 1035 ms 90724 KB Output is correct
7 Correct 447 ms 131072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 369 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -