Submission #63744

# Submission time Handle Problem Language Result Execution time Memory
63744 2018-08-02T16:58:30 Z aome Islands (IOI08_islands) C++17
80 / 100
1622 ms 132096 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1000005;
const long long INF = 1e18;

int n;
int deg[N];
int a[N], w[N];
bool visit[N];
long long mx, res;
long long f[N], g[N];
vector<int> G[N], vec;

void dfs1(int u, int p) {
	for (auto v : G[u]) {
		if (deg[v] == 2 || v == p) continue;
		dfs1(v, u), f[u] = max(f[u], f[v] + w[v]);
	}
}

void dfs2(int u, int p) {
	mx = max(mx, max(f[u], g[u]));
	long long mx1, mx2;
	mx1 = mx2 = -INF;
	for (auto v : G[u]) {
		if (deg[v] == 2 || v == p) continue;
		if (mx1 < f[v] + w[v]) mx2 = mx1, mx1 = f[v] + w[v];
		else if (mx2 < f[v] + w[v]) mx2 = f[v] + w[v];
	}
	for (auto v : G[u]) {
		if (deg[v] == 2 || v == p) continue;
		g[v] = g[u] + w[v];
		if (mx1 == f[v] + w[v]) g[v] = max(g[v], mx2 + w[v]);
		else g[v] = max(g[v], mx1 + w[v]);
		dfs2(v, u);
	}
}

void solve() {
	queue<int> qu;
	for (auto i : vec) {
		deg[i] = G[i].size();
		if (deg[i] == 1) qu.push(i);
	}
	while (qu.size()) {
		int u = qu.front(); qu.pop();
		if (--deg[a[u]] == 1) qu.push(a[u]);

	}
	vector<int> cir;
	for (auto i : vec) {
		if (deg[i] != 2) continue;
		int u = i, prv = 0;
		while (1) {
			cir.push_back(u), u = a[u];
			if (u == i) break;
		}
		break;
	}
	mx = 0;
	for (auto i : cir) dfs1(i, i), dfs2(i, i);
	int sz = cir.size();
	long long sumAll = 0;
	for (auto i : cir) sumAll += w[i];
	long long sum = 0;
	long long mx1 = -INF, mx2 = -INF;
	for (auto u : cir) {
		mx = max(mx, f[u] + sum + mx1);
		mx = max(mx, f[u] - sum + sumAll + mx2);
		mx1 = max(mx1, f[u] - sum);
		mx2 = max(mx2, f[u] + sum);
		sum += w[u];
	}
	res += mx;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i] >> w[i];
		G[a[i]].push_back(i);
		G[i].push_back(a[i]);
	}
	for (int i = 1; i <= n; ++i) {
		if (visit[i]) continue;
		vec.clear();
		queue<int> qu;
		qu.push(i), visit[i] = 1;
		while (qu.size()) {
			int u = qu.front(); qu.pop();
			vec.push_back(u);
			for (auto v : G[u]) {
				if (!visit[v]) {
					visit[v] = 1, qu.push(v);
				}
			}
		}
		solve();
	}
	cout << res;
}

Compilation message

islands.cpp: In function 'void solve()':
islands.cpp:55:14: warning: unused variable 'prv' [-Wunused-variable]
   int u = i, prv = 0;
              ^~~
islands.cpp:64:6: warning: unused variable 'sz' [-Wunused-variable]
  int sz = cir.size();
      ^~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Correct 25 ms 23912 KB Output is correct
3 Correct 26 ms 23952 KB Output is correct
4 Correct 27 ms 23960 KB Output is correct
5 Correct 27 ms 23964 KB Output is correct
6 Correct 24 ms 23964 KB Output is correct
7 Correct 24 ms 24080 KB Output is correct
8 Correct 28 ms 24080 KB Output is correct
9 Correct 31 ms 24084 KB Output is correct
10 Correct 27 ms 24084 KB Output is correct
11 Correct 26 ms 24084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 24212 KB Output is correct
2 Correct 30 ms 24212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 24516 KB Output is correct
2 Correct 34 ms 24652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 25404 KB Output is correct
2 Correct 53 ms 27384 KB Output is correct
3 Correct 55 ms 27384 KB Output is correct
4 Correct 35 ms 27384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 29524 KB Output is correct
2 Correct 90 ms 33292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 43088 KB Output is correct
2 Correct 199 ms 44872 KB Output is correct
3 Correct 214 ms 53384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 302 ms 64020 KB Output is correct
2 Correct 421 ms 80880 KB Output is correct
3 Correct 398 ms 83352 KB Output is correct
4 Correct 619 ms 106144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 666 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1622 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
2 Halted 0 ms 0 KB -