답안 #1054743

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1054743 2024-08-12T11:54:22 Z lovrot Islands (IOI08_islands) C++17
15 / 100
185 ms 131072 KB
#include <cstdio> 
#include <vector>
#include <algorithm> 

#define debug(...) //fprintf(stderr, __VA_ARGS__)
#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;

int n, weg[N]; 
vector<pii> g[N];

int bio[N];
vector<pii> cyc;

int dfs(int u, int p) { 
	bio[u] = 1;
	for(pii e : g[u]) { 
		int v = e.X, w = e.Y;
		if(w != p) { 
			if(bio[v]) { 
				cyc.PB({e.X, weg[e.Y]});
				return v;
			} else {
				int res = dfs(v, w);
				if(res) { 
					cyc.PB({e.X, weg[e.Y]});
					return res != u ? res : 0;
				}
			}
		}
	}
	bio[u] = 0;
	return 0;
}

void mark(int u) { 
	bio[u] = 2;
	for(pii e : g[u]) { 
		if(bio[e.X] != 2) { 
			mark(e.X);
		}
	}
}

ll dep[N];

ll depth(int u, int p) { 
	for(pii e : g[u]) { 
		int v = e.X, w = e.Y;
		if(w != p && !bio[v]) { 
			dep[u] = max(dep[u], (ll) weg[w] + depth(v, w));
		}
	}
	return dep[u];
}

int main() { 
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) { 
		int u, w;
		scanf("%d%d", &u, weg + i);
		g[i].PB({u, i});
		g[u].PB({i, i});
	}

	ll ans = 0;
	for(int i = 1; i <= n; ++i) { 
		if(bio[i]) { continue; }

		cyc.clear();
		dfs(i, 0);

		for(pii e : cyc) { 
			depth(e.X, 0);
			debug("%d(%d, %lld)\n", e.X, e.Y, dep[e.X]);
		}

		ll res = dep[cyc.front().X], xam = dep[cyc.front().X], prf = cyc.front().Y;
		for(int i = 1; i < cyc.size(); ++i) { 
			res = max(dep[cyc[i].X] + xam + prf, res);
			xam = max(dep[cyc[i].X] - prf, xam);
			prf += cyc[i].Y;
		}
	
		xam = dep[cyc.front().X];
		prf = 0;

		for(int i = (int) cyc.size() - 1; i > 0; --i) { 
			prf += cyc[i].Y;
			res = max(dep[cyc[i].X] + xam + prf, res);
			xam = max(dep[cyc[i].X] - prf, xam);
		}

		ans += res;
		mark(i);
		debug("+ %lld\n", res);
	}

	printf("%lld\n", ans);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:68:10: warning: unused variable 'w' [-Wunused-variable]
   68 |   int u, w;
      |          ^
islands.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for(int i = 1; i < cyc.size(); ++i) {
      |                  ~~^~~~~~~~~~~~
islands.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
islands.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |   scanf("%d%d", &u, weg + i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 26972 KB Output is correct
2 Incorrect 3 ms 26972 KB Output isn't correct
3 Incorrect 3 ms 26984 KB Output isn't correct
4 Correct 3 ms 26972 KB Output is correct
5 Correct 3 ms 27108 KB Output is correct
6 Correct 3 ms 26972 KB Output is correct
7 Incorrect 3 ms 27016 KB Output isn't correct
8 Incorrect 3 ms 26972 KB Output isn't correct
9 Incorrect 3 ms 26972 KB Output isn't correct
10 Incorrect 3 ms 26972 KB Output isn't correct
11 Correct 3 ms 27116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 26972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 26972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 27996 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 31956 KB Output is correct
2 Incorrect 20 ms 34256 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 42544 KB Output is correct
2 Correct 49 ms 47008 KB Output is correct
3 Incorrect 59 ms 59588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 70 ms 52980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 160 ms 86960 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 185 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -