답안 #885654

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885654 2023-12-10T11:31:22 Z Tob Islands (IOI08_islands) C++14
90 / 100
547 ms 131072 KB
#include <bits/stdc++.h>

#define ll long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef pair <ll, ll> pii;

const ll N = 1e6 + 7, inf = 1e18;

ll n, ro, sp, res, R;
ll a[N], b[N], dp[N], dis[N];
int bio[N], cyc[N];
vector <pii> adj[N];

void Merge(pii& x, ll y) {
	if (y >= x.F) x = {y, x.F};
	else x = {x.F, max(x.S, y)};
}

void dfs(int x) {
	pii p1 = {0, 0};
	for (auto y : adj[x]) {
		if (cyc[y.F]) continue;
		dfs(y.F);
		Merge(p1, dp[y.F] + y.S);
	}
	dp[x] = p1.F;
	res = max(res, p1.F + p1.S);
}

int main () {
	FIO;
	cin >> n;
	
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
		adj[a[i]].pb({i, b[i]});
	}
	
	ll sum = 0;
	for (int i = 1; i <= n; i++) {
		if (bio[i]) continue;
		int x = a[i], y = i;
		bio[i] = i;
		while (!bio[x]) {
			bio[x] = i;
			y = x;
			x = a[x];
		}
		if (bio[x] == i) {
			res = 0;
			cyc[y] = 1;
			vector <int> v; v = {0, y};
			while (x != y) {
				v.pb(x);
				cyc[x] = 1;
				x = a[x];
			}
			int siz = v.size();
			vector <ll> dis(siz+1, 0), rdis(siz+1, 0);
			vector <ll> pr(siz+1, -inf), suf(siz+1, -inf), pr2(siz+1, -inf), suf2(siz+1, -inf);
			ll suma = 0;
			for (int i = 1; i < siz; i++) {
				dfs(v[i]);
				dis[i] = dis[i-1] + b[v[i-1]];
			}
			for (int i = siz-1; i; i--) rdis[i] = rdis[i+1] + (i < siz-1)*b[v[i]];
			for (int i = 1; i < siz; i++) {
				pr[i] = max(pr[i-1], dp[v[i]] + dis[i]);
				pr2[i] = max(pr2[i-1], dp[v[i]] + rdis[i]);
			}
			for (int i = siz-1; i; i--) {
				suf[i] = max(suf[i+1], dp[v[i]] + dis[i]);
				suf2[i] = max(suf2[i+1], dp[v[i]] + rdis[i]);				
			}
			for (int i = 1; i < siz; i++) {
//				cout << i << "i " << v[i] << "x " << dis[i] << "d " << rdis[i] << "rd " << pr[i] << "p " << pr2[i] << "pp " << suf[i] << "s " << suf2[i] << "ss " << dp[v[i]] << "dp\n";
				res = max(res, dp[v[i]] + max(suf[i+1] - dis[i], pr[i-1] + rdis[i] + b[v.back()]));
				res = max(res, dp[v[i]] + max(pr2[i-1] - rdis[i], suf2[i+1] + dis[i] + b[v.back()]));
			}
			sum += res;
		}
	}
	
	cout << sum;

	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:68:7: warning: unused variable 'suma' [-Wunused-variable]
   68 |    ll suma = 0;
      |       ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31324 KB Output is correct
2 Correct 7 ms 31340 KB Output is correct
3 Correct 6 ms 31324 KB Output is correct
4 Correct 7 ms 31324 KB Output is correct
5 Correct 7 ms 31324 KB Output is correct
6 Correct 7 ms 31580 KB Output is correct
7 Correct 6 ms 31432 KB Output is correct
8 Correct 6 ms 31324 KB Output is correct
9 Correct 6 ms 31324 KB Output is correct
10 Correct 6 ms 31324 KB Output is correct
11 Correct 6 ms 31324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 31324 KB Output is correct
2 Correct 7 ms 31332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 31324 KB Output is correct
2 Correct 8 ms 33560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 34140 KB Output is correct
2 Correct 15 ms 35620 KB Output is correct
3 Correct 12 ms 34396 KB Output is correct
4 Correct 9 ms 33716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 36700 KB Output is correct
2 Correct 26 ms 40676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 47080 KB Output is correct
2 Correct 52 ms 51936 KB Output is correct
3 Correct 64 ms 56176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 63528 KB Output is correct
2 Correct 103 ms 75976 KB Output is correct
3 Correct 120 ms 90384 KB Output is correct
4 Correct 140 ms 100036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 86700 KB Output is correct
2 Correct 399 ms 125224 KB Output is correct
3 Correct 163 ms 86832 KB Output is correct
4 Correct 206 ms 106204 KB Output is correct
5 Correct 202 ms 107696 KB Output is correct
6 Correct 547 ms 96924 KB Output is correct
7 Correct 224 ms 128308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 211 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -