Submission #379341

# Submission time Handle Problem Language Result Execution time Memory
379341 2021-03-18T02:27:35 Z pavement Islands (IOI08_islands) C++17
100 / 100
1330 ms 131072 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

ll ans, ed, total, wtpref, fs, snd, ldpath, lpath;
int N, st, rn, tort, hare, link[1000005];
ii nxt[1000005];
bitset<1000005> oncyc;
vector<ii> adj[1000005];

int find(int x) {
	if (x == link[x]) return x;
	return link[x] = find(link[x]);
}

void unite(int a, int b) {
	a = find(a);
	b = find(b);
	if (a == b) return;
	if (rng() & 1ll) swap(a, b);
	link[b] = a;
}

void find_diam(int n, int e = -1, ll d = 0) {
	if (d > ed) {
		ed = d;
		st = n;
	}
	for (auto u : adj[n])
		if ((!oncyc[u.first] || u.first == rn) && u.first != e)
			find_diam(u.first, n, d + (ll)u.second);
}

main() {
	oncyc.reset();
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> N;
	for (int i = 1; i <= N; i++) link[i] = i;
	for (int i = 1; i <= N; i++) {
		cin >> rn >> st;
		nxt[i] = mp(rn, st);
		adj[i].eb(rn, st);
		adj[rn].eb(i, st);
		unite(i, rn);
	}
	for (int i = 1; i <= N; i++) {
		if (i == find(i)) {
		    lpath = 0;
			tort = hare = i;
			do {
				tort = nxt[tort].first;
				hare = nxt[nxt[hare].first].first;
			} while (tort != hare);
			total = wtpref = 0;
			fs = snd = -1;
			do {
				total += nxt[tort].second;
				oncyc[tort] = 1;
				tort = nxt[tort].first;
			} while (tort != hare);
			do {
				rn = tort;
				ed = st = -1;
				find_diam(tort);
				ldpath = ed;
				ed = -1;
				find_diam(st);
				lpath = max(lpath, ed);
				if (fs != -1) lpath = max({lpath, fs + ldpath + wtpref, snd + ldpath - wtpref + total});
				fs = max(fs, ldpath - wtpref);
				snd = max(snd, ldpath + wtpref);
				wtpref += nxt[tort].second;
				tort = nxt[tort].first;
			} while (tort != hare);
			ans += lpath;
		}
	}
	cout << ans << '\n';
}

Compilation message

islands.cpp:56:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   56 | main() {
      |      ^
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23916 KB Output is correct
2 Correct 18 ms 23996 KB Output is correct
3 Correct 18 ms 24044 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 19 ms 23916 KB Output is correct
6 Correct 17 ms 24044 KB Output is correct
7 Correct 16 ms 23916 KB Output is correct
8 Correct 17 ms 23916 KB Output is correct
9 Correct 17 ms 23916 KB Output is correct
10 Correct 17 ms 23916 KB Output is correct
11 Correct 20 ms 24044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 17 ms 24044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 20 ms 24172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24940 KB Output is correct
2 Correct 36 ms 26220 KB Output is correct
3 Correct 30 ms 25324 KB Output is correct
4 Correct 22 ms 24556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 27244 KB Output is correct
2 Correct 55 ms 29548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 35328 KB Output is correct
2 Correct 109 ms 35692 KB Output is correct
3 Correct 135 ms 40172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 43756 KB Output is correct
2 Correct 211 ms 56080 KB Output is correct
3 Correct 233 ms 54508 KB Output is correct
4 Correct 307 ms 65132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 396 ms 85704 KB Output is correct
2 Correct 864 ms 80836 KB Output is correct
3 Correct 320 ms 63468 KB Output is correct
4 Correct 412 ms 80620 KB Output is correct
5 Correct 413 ms 83820 KB Output is correct
6 Correct 1319 ms 70916 KB Output is correct
7 Correct 469 ms 89052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 519 ms 131072 KB Output is correct
2 Correct 521 ms 131072 KB Output is correct
3 Correct 440 ms 97640 KB Output is correct
4 Correct 455 ms 82040 KB Output is correct
5 Correct 426 ms 96372 KB Output is correct
6 Correct 409 ms 87964 KB Output is correct
7 Correct 1330 ms 86452 KB Output is correct