답안 #103659

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
103659 2019-04-01T20:37:31 Z KCSC Islands (IOI08_islands) C++14
80 / 100
1397 ms 132096 KB
// 22.48
#include <bits/stdc++.h>
using namespace std;

const int DIM = 1000005;

long long dst[DIM], dpl[DIM], dpr[DIM];
int oki[DIM], lev[DIM], cst[DIM], fth[DIM], cyc[DIM], val[DIM];

vector<pair<int, int>> edg[DIM];

int m;
void dfs1(int x, int f) {
	oki[x] = true; lev[x] = lev[f] + 1;
	bool ok = false;
	for (auto it : edg[x]) {
		int y = it.first, c = it.second;
		if (!oki[y]) {
			cst[y] = c; fth[y] = x; dfs1(y, x); }
		else if (lev[y] < lev[x] and (y != f or ok)) {
			for (int z = x; z != fth[y]; z = fth[z]) {
				oki[z] = 2; cyc[m] = z;  
				if (z == y) {
					val[m] = c; }
				else {
					val[m] = cst[z]; } 
				++m; } } 
		if (y == f) {
			ok = true; } } }

void dfs2(int x, int f, int &nd, long long &ds) {
	if (ds < dst[x]) {
		ds = dst[x]; nd = x; }
	for (auto it : edg[x]) {
		int y = it.first, c = it.second;
		if (oki[y] == 2 or y == f) {
			continue; }
		dst[y] = dst[x] + c; dfs2(y, x, nd, ds); } }

int main(void) {
#ifdef HOME
	freopen("islands.in", "r", stdin);
	freopen("islands.out", "w", stdout);
#endif
	int n; cin >> n;
	for (int i = 1; i <= n; ++i) {
		int y, c; cin >> y >> c;
		edg[i].push_back(make_pair(y, c)); 
		edg[y].push_back(make_pair(i, c)); }
	long long ans = 0;
	for (int k = 1; k <= n; ++k) {
		if (oki[k]) {
			continue; }
		m = 0; dfs1(k, 0); 
		long long aux = 0; 
		dpl[0] = 0;
		for (int i = 1; i < m; ++i) {
			dpl[i] = dpl[i - 1] + val[i - 1]; }
		dpr[m - 1] = val[m - 1];
		for (int i = m - 2; i >= 0; --i) {
				dpr[i] = dpr[i + 1] + val[i]; }
		long long cs1 = -1LL << 60, cs2 = -1LL << 60;
		for (int i = 0; i < m; ++i) {
			int x = cyc[i]; oki[x] = 1;
			int c1; long long mx1 = -1; dst[x] = 0; dfs2(x, 0, c1, mx1);
			int c2; long long mx2 = -1; dst[c1] = 0; dfs2(c1, 0, c2, mx2);
			aux = max(aux, mx2); oki[x] = 2; 
			if (i) {
				aux = max(aux, cs1 + dpl[i] + mx1);
				aux = max(aux, cs2 + dpr[i] + mx1); }
			cs1 = max(cs1, mx1 - dpl[i]);
			cs2 = max(cs2, mx1 + dpl[i]); }
		ans += aux; }
	cout << ans << endl;
	return 0; }
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 23936 KB Output is correct
2 Correct 24 ms 23936 KB Output is correct
3 Correct 23 ms 23936 KB Output is correct
4 Correct 23 ms 23808 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 25 ms 23936 KB Output is correct
7 Correct 23 ms 23936 KB Output is correct
8 Correct 23 ms 23908 KB Output is correct
9 Correct 22 ms 23936 KB Output is correct
10 Correct 22 ms 23936 KB Output is correct
11 Correct 23 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23936 KB Output is correct
2 Correct 24 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 24 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 25080 KB Output is correct
2 Correct 56 ms 27580 KB Output is correct
3 Correct 51 ms 25208 KB Output is correct
4 Correct 32 ms 24568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 28920 KB Output is correct
2 Correct 110 ms 31608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 194 ms 37992 KB Output is correct
2 Correct 208 ms 44240 KB Output is correct
3 Correct 389 ms 54392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 388 ms 49144 KB Output is correct
2 Correct 518 ms 74872 KB Output is correct
3 Correct 468 ms 83444 KB Output is correct
4 Correct 661 ms 102392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 830 ms 86608 KB Output is correct
2 Runtime error 1397 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1121 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -