답안 #256125

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256125 2020-08-02T10:06:55 Z mode149256 Islands (IOI08_islands) C++14
30 / 100
1145 ms 131072 KB
/*input
17
3 100000000
13 1340230
10 100000000
15 29909322
12 100000000
16 1147894
9 37689485
4 11113028
7 17938034
1 100000000
16 91101071
14 100000000
17 100000000
5 100000000
8 67104679
11 22556013
13 100000000
*/
#include <bits/stdc++.h>
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 = 1000010;

int N;
vpi edges(MX);
vpii edgesVisos(MX);
vb been(MX, false);

vl circle; // lens
vi circleV;
vb inCircle(MX, false);

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);
}

// -1 ner rato
int find_circle(int x) {
	been[x] = true;

	auto u = edges[x];
	if (been[u.x]) {
		// printf("x = %d, u = %d %d\n", x, u.x, u.y);
		circleV.emplace_back(x);
		circle.emplace_back(u.y);
		been[x] = false;
		return u.x;
	}

	int k = find_circle(u.x);

	if (k == x) {
		// printf("x = %d, u = %d %d\n", x, u.x, u.y);
		circleV.emplace_back(x);
		circle.emplace_back(u.y);
		been[x] = false;
		return -1;
	}
	else if (k != -1) {
		// printf("x = %d, u = %d %d\n", x, u.x, u.y);
		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 inCircle[u.x]) continue;
		longest(u.x, x, dis + u.y);
	}
}

void solve() {
	proc = 0;
	for (auto u : circleV)
		inCircle[u] = true;

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

	vl D(C, 0);

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

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

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

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

			// printf("i = %d, d = %d\n", i, D[i]);
		}

		if (kokie.size() >= 2) {
			sort(kokie.rbegin(), kokie.rend());
			proc = max(proc, kokie[0] + kokie[1]);
		}
	}

	// for (int i = 0; i < C; ++i) printf("v = %d, dis = %lld\n", circleV[i]+1, D[i]);

	deque<pi> dq;
	ll viso = accumulate(circle.begin(), circle.end(), 0);

	{
		for (int i = 1; i < C; ++i)
		{
			circle[i] += circle[i - 1];
			ll newVal = circle[i - 1] + D[i];

			while (dq.size() and dq.back().x <= newVal)
				dq.pop_back();

			dq.push_back({newVal, i});
		}

		for (int i = 0; i < C; ++i)
		{
			while (dq.size() and dq.front().y <= i) {
				dq.pop_front();
			}

			assert(dq.size());
			proc = max(proc, dq.front().x + D[i] - (i ? circle[i - 1] : 0));

			ll newVal = D[i] + viso + (i ? circle[i - 1] : 0);
			while (dq.size() and dq.back().x <= newVal)
				dq.pop_back();

			dq.push_back({newVal, i + C});
		}
	}

	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[a].emplace_back(i, w);
		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);
		fill(i);
		reverse(circle.begin(), circle.end());
		reverse(circleV.begin(), circleV.end());

		// for (auto u : circleV)
		// 	printf("%d ", u + 1);
		// printf("\n");
		// for (auto u : circle)
		// 	printf("%lld ", u);
		// printf("\n");

		solve();
	}

	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 19 ms 31872 KB Output is correct
2 Correct 23 ms 31872 KB Output is correct
3 Incorrect 19 ms 32000 KB Output isn't correct
4 Correct 20 ms 31872 KB Output is correct
5 Correct 22 ms 31872 KB Output is correct
6 Correct 22 ms 31872 KB Output is correct
7 Correct 19 ms 31872 KB Output is correct
8 Correct 22 ms 31872 KB Output is correct
9 Correct 23 ms 31872 KB Output is correct
10 Correct 19 ms 31872 KB Output is correct
11 Correct 23 ms 31872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 32000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 32000 KB Output is correct
2 Incorrect 22 ms 32256 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 32632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 49 ms 35328 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 118 ms 40696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 182 ms 47224 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 522 ms 98260 KB Output is correct
2 Incorrect 1145 ms 118156 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 539 ms 131072 KB Output isn't correct
2 Halted 0 ms 0 KB -