답안 #256226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256226 2020-08-02T11:39:21 Z mode149256 Islands (IOI08_islands) C++14
100 / 100
1315 ms 120136 KB
/*input
7
3 8
7 2
4 2
1 4
1 9
3 4
2 3
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
 
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
 
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
 
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
 
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
 
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
 
template<typename T>
void print(vector<T> vec, string name = "") {
	cout << name;
	for (auto u : vec)
		cout << u << ' ';
	cout << '\n';
}
}
using namespace my_template;
 
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 1000000;
 
int N;
pi edges[MX];
vpi edgesVisos[MX];
bitset<MX> been;
 
vi circle; // lens
vi circleV;
 
ll ats = 0;
ll proc = 0;
ll maxDis = 0;
int kur;
 
void fill(int x) {
	been[x] = true;
 
	for (auto u : edgesVisos[x])
		if (!been[u.x])
			fill(u.x);
 
	auto u = edges[x];
	if (!been[u.x])
		fill(u.x);
}
 
// -1 ner rato
int find_circle(int x) {
	been[x] = true;
 
	auto u = edges[x];
	if (been[u.x]) {
		circleV.emplace_back(x);
		circle.emplace_back(u.y);
		been[x] = false;
		return u.x;
	}
 
	int k = find_circle(u.x);
 
	if (k == x) {
		circleV.emplace_back(x);
		circle.emplace_back(u.y);
		been[x] = false;
		return -1;
	}
	else if (k != -1) {
		circleV.emplace_back(x);
		circle.emplace_back(u.y);
		been[x] = false;
		return k;
	}
 
	been[x] = false;
	return -1;
}
 
void longest(int x, int p, ll dis) {
	if (dis > maxDis) {
		maxDis = dis;
		kur = x;
	}
 
	for (auto u : edgesVisos[x]) {
		if (u.x == p or been[u.x]) continue;
		longest(u.x, x, dis + u.y);
	}
	auto u = edges[x];
	if (u.x == p or been[u.x]) return;
	longest(u.x, x, dis + u.y);
}
 
void solve() {
	proc = 0;
	for (auto u : circleV)
		been[u] = true;

	int C = (int)circleV.size();

	vl D(C, 0);

	for (int i = 0; i < C; ++i)
	{
		int x = circleV[i];

		ll did = 0;
		ll did2 = 0;

		D[i] = 0;

		for (auto u : edgesVisos[x]) {
			if (been[u.x]) continue;
			maxDis = 0;
			longest(u.x, x, u.y);
			proc = max(proc, maxDis);

			if (maxDis >= did) {
				did2 = did;
				did = maxDis;
			} else if (maxDis > did2) {
				did2 = maxDis;
			}

			D[i] = max(D[i], maxDis);

			maxDis = 0;
			longest(kur, -1, 0);
			proc = max(proc, maxDis);
		}

		proc = max(proc, did + did2);
	}

	ll viso = 0;
	for (int i = 0; i < C; ++i) viso += (ll)edges[circleV[i]].y;

	ll suma = edges[circleV[0]].y;

	{
		vl big(C, 0);

		for (int i = 1; i < C; ++i)
		{
			// edges[circleV[i]].y += edges[i -.y 1];
			ll newVal = suma + D[i];

			big[i] = newVal;

			suma += (ll)edges[circleV[i]].y;
		}

		for (int i = C - 2; i >= 0; i--)
		{
			big[i] = max(big[i], big[i + 1]);
		}

		ll did = 0;

		suma = 0;
		for (int i = 0; i < C; ++i)
		{
			ll nxt = max((i + 1 < C ? big[i + 1] : 0), did);

			proc = max(proc, nxt + D[i] - suma);

			did = max(did, D[i] + viso + suma);
			suma += (ll)edges[circleV[i]].y;
		}
	}

	for (auto u : circleV)
		been[u] = false;

	ats += proc;
}
 
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		int a, w;
		cin >> a >> w; a--;
 
		edges[i] = {a, w};
 
		// edgesVisos[i].emplace_back(a, w);
		edgesVisos[a].emplace_back(i, w);
	}
 
	for (int i = 0; i < N; ++i)
	{
		if (been[i]) continue;
 
		circle.clear();
		circleV.clear();
 
		find_circle(i);
		reverse(circle.begin(), circle.end());
		reverse(circleV.begin(), circleV.end());
 
		solve();
		fill(i);
	}
 
	printf("%lld\n", ats);
}
 
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 14 ms 23808 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23936 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23936 KB Output is correct
2 Correct 16 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24576 KB Output is correct
2 Correct 30 ms 25848 KB Output is correct
3 Correct 24 ms 24704 KB Output is correct
4 Correct 21 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 26616 KB Output is correct
2 Correct 61 ms 28232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 30740 KB Output is correct
2 Correct 98 ms 37028 KB Output is correct
3 Correct 121 ms 39188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 169 ms 37496 KB Output is correct
2 Correct 191 ms 48016 KB Output is correct
3 Correct 215 ms 62192 KB Output is correct
4 Correct 262 ms 63452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 312 ms 40360 KB Output is correct
2 Correct 671 ms 80680 KB Output is correct
3 Correct 294 ms 47480 KB Output is correct
4 Correct 371 ms 67976 KB Output is correct
5 Correct 371 ms 66184 KB Output is correct
6 Correct 1129 ms 52924 KB Output is correct
7 Correct 397 ms 83532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 420 ms 116740 KB Output is correct
2 Correct 443 ms 92952 KB Output is correct
3 Correct 428 ms 120136 KB Output is correct
4 Correct 424 ms 66808 KB Output is correct
5 Correct 372 ms 68748 KB Output is correct
6 Correct 389 ms 63940 KB Output is correct
7 Correct 1315 ms 55980 KB Output is correct