답안 #233634

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
233634 2020-05-21T06:29:09 Z dolphingarlic Islands (IOI08_islands) C++14
100 / 100
1022 ms 131072 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1000005;
const long long INF = 1e18;
 
int n;
int par[N];
int deg[N];
int a[N], w[N];
long long mx, res;
long long f[N], g[N];
long long sum[N];
vector<int> G[N], vec[N];
 
int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }
 
void join(int u, int v) { par[find(u)] = find(v); }
 
void solve(vector<int> &vec) {
	queue<int> qu;
	for (auto i : vec) {
		deg[i] = G[i].size();
		if (!deg[i]) qu.push(i);
	}
	while (qu.size()) {
		int u = qu.front(); qu.pop();
		if (!--deg[a[u]]) qu.push(a[u]);
		f[a[u]] = max(f[a[u]], f[u] + w[u]);
 
	}
	vector<int> cir;
	for (auto i : vec) {
		if (!deg[i]) 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) {
		stack<int> st; st.push(i);
		while (st.size()) {
			int u = st.top(); st.pop();
			mx = max(mx, max(f[u], g[u]));
			long long mx1, mx2;
			mx1 = mx2 = -INF;
			for (auto v : G[u]) {
				if (deg[v]) 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]) 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]);
				st.push(v);
			}
		}
	}
	int sz = cir.size();
	for (int i = 1; i <= sz; ++i) {
		sum[i] = sum[i - 1] + w[cir[i - 1]];
	}
	long long mx1 = -INF, mx2 = -INF;
	for (int i = 1; i <= sz; ++i) {
		int u = cir[i - 1];
		mx = max(mx, f[u] + sum[i - 1] + mx1);
		mx = max(mx, f[u] - sum[i - 1] + sum[sz] + mx2);
		mx1 = max(mx1, f[u] - sum[i - 1]);
		mx2 = max(mx2, f[u] + sum[i - 1]);
	}
	res += mx;
}
 
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; ++i) par[i] = i;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i] >> w[i];
		join(i, a[i]), G[a[i]].push_back(i);
	}
	for (int i = 1; i <= n; ++i) vec[find(i)].push_back(i);
	for (int i = 1; i <= n; ++i) if (find(i) == i) solve(vec[find(i)]);
	cout << res;
}

Compilation message

islands.cpp: In function 'void solve(std::vector<int>&)':
islands.cpp:36:14: warning: unused variable 'prv' [-Wunused-variable]
   int u = i, prv = 0;
              ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 47488 KB Output is correct
2 Correct 33 ms 47360 KB Output is correct
3 Correct 38 ms 47480 KB Output is correct
4 Correct 31 ms 47360 KB Output is correct
5 Correct 31 ms 47352 KB Output is correct
6 Correct 31 ms 47360 KB Output is correct
7 Correct 38 ms 47360 KB Output is correct
8 Correct 30 ms 47360 KB Output is correct
9 Correct 39 ms 47352 KB Output is correct
10 Correct 30 ms 47360 KB Output is correct
11 Correct 31 ms 47360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 47480 KB Output is correct
2 Correct 31 ms 47488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 47488 KB Output is correct
2 Correct 36 ms 47736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 48512 KB Output is correct
2 Correct 49 ms 49920 KB Output is correct
3 Correct 42 ms 48896 KB Output is correct
4 Correct 36 ms 48120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 50940 KB Output is correct
2 Correct 80 ms 53936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 59504 KB Output is correct
2 Correct 136 ms 63720 KB Output is correct
3 Correct 173 ms 65004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 203 ms 70332 KB Output is correct
2 Correct 277 ms 77672 KB Output is correct
3 Correct 310 ms 87944 KB Output is correct
4 Correct 359 ms 90788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 356 ms 84568 KB Output is correct
2 Correct 790 ms 115164 KB Output is correct
3 Correct 374 ms 100648 KB Output is correct
4 Correct 502 ms 117704 KB Output is correct
5 Correct 485 ms 117036 KB Output is correct
6 Correct 979 ms 116448 KB Output is correct
7 Correct 570 ms 127712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 460 ms 131072 KB Output is correct
2 Correct 428 ms 129372 KB Output is correct
3 Correct 634 ms 131072 KB Output is correct
4 Correct 748 ms 125048 KB Output is correct
5 Correct 517 ms 118364 KB Output is correct
6 Correct 449 ms 118624 KB Output is correct
7 Correct 1022 ms 117548 KB Output is correct