답안 #53777

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
53777 2018-07-01T07:21:25 Z ics0503 Islands (IOI08_islands) C++17
90 / 100
2000 ms 103552 KB
#include<iostream>
#include<vector>
using namespace std;
vector<int>edge[1212121];
long long RD[1212121], D[1212121], Y[1212121], SV[1212121], L[1212121], V[1212121];
int X[1212121], indeg[1212121], ck[1212121];
long long max(long long a, long long b) { if (a < b)return b; return a; }
void myassert(int x) {
	if (x == 1e14)return;
	printf("1");
	myassert(x + 1);
}
long long get_RD(int x) { // O(E)
	if (edge[x].size() < 2)return 0;
	long long mx1 = 0, mxw, mx2 = 0;
	for (int &nxt : edge[x])if (mx1 < D[nxt] + Y[nxt])mx1 = D[nxt] + Y[nxt], mxw = nxt;
	for (int &nxt : edge[x])if (nxt != mxw && mx2 < D[nxt] + Y[nxt])mx2 = D[nxt] + Y[nxt];
	return mx1 + mx2;
}
int main() {
	ios_base::sync_with_stdio(false);
	vector<long long>Q;
	long long n, i, j; cin >> n;
	for (i = 1; i <= n; i++) {
		cin >> X[i] >> Y[i], indeg[X[i]]++, edge[X[i]].push_back(i); // E == N
		if (X[i] == 0)myassert(-10);
	}
	for (i = 1; i <= n; i++)if (indeg[i] == 0)Q.push_back(i);
	while (!Q.empty()) { // O(N)
		long long now = Q.back(); Q.pop_back();
		RD[now] = max(RD[now], get_RD(now));
		RD[X[now]] = max(RD[X[now]], RD[now]);
		D[X[now]] = max(D[X[now]], D[now] + Y[now]);
		if (--indeg[X[now]] == 0)Q.push_back(X[now]);
		ck[now] = 1;
	}
	long long ans = 0;
	for (i = 1; i <= n; i++) if (!ck[i]) { // O(N)
		long long now = i, S = 0, sz = 0, res = 0, mx1 = -1e18, mx2 = -1e18;
		while (!ck[now]) { // O(Cyc)
			L[++sz] = D[now]; V[sz] = Y[now];
			res = max(res, max(RD[now], get_RD(now)));
			S += Y[now];
			ck[now] = 1; now = X[now];
		}
		for (i = 1; i <= sz; i++)SV[i] = SV[i - 1] + V[i]; 
		for (i = 1; i <= sz; i++) {
			res = max(res, max(L[i] + SV[i - 1] + mx1, L[i] + S - SV[i - 1] + mx2));
			mx1 = max(mx1, L[i] - SV[i - 1]);
			mx2 = max(mx2, L[i] + SV[i - 1]);
		}
		ans += res;
	}
	cout << ans;
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:23:18: warning: unused variable 'j' [-Wunused-variable]
  long long n, i, j; cin >> n;
                  ^
islands.cpp: In function 'long long int get_RD(int)':
islands.cpp:17:26: warning: 'mxw' may be used uninitialized in this function [-Wmaybe-uninitialized]
  for (int &nxt : edge[x])if (nxt != mxw && mx2 < D[nxt] + Y[nxt])mx2 = D[nxt] + Y[nxt];
                          ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 28792 KB Output is correct
2 Correct 27 ms 29028 KB Output is correct
3 Correct 26 ms 29108 KB Output is correct
4 Correct 26 ms 29108 KB Output is correct
5 Correct 25 ms 29108 KB Output is correct
6 Correct 28 ms 29108 KB Output is correct
7 Correct 29 ms 29248 KB Output is correct
8 Correct 29 ms 29248 KB Output is correct
9 Correct 26 ms 29248 KB Output is correct
10 Correct 30 ms 29248 KB Output is correct
11 Correct 28 ms 29248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 29248 KB Output is correct
2 Correct 32 ms 29248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 29292 KB Output is correct
2 Correct 30 ms 29380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 30060 KB Output is correct
2 Correct 43 ms 31596 KB Output is correct
3 Correct 39 ms 31596 KB Output is correct
4 Correct 30 ms 31596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 32444 KB Output is correct
2 Correct 63 ms 35064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 40944 KB Output is correct
2 Correct 103 ms 43772 KB Output is correct
3 Correct 134 ms 47084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 296 ms 52004 KB Output is correct
2 Correct 246 ms 61296 KB Output is correct
3 Correct 210 ms 66940 KB Output is correct
4 Correct 275 ms 74856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 328 ms 74856 KB Output is correct
2 Correct 742 ms 90296 KB Output is correct
3 Correct 407 ms 90296 KB Output is correct
4 Correct 497 ms 92972 KB Output is correct
5 Correct 490 ms 92972 KB Output is correct
6 Correct 1055 ms 92972 KB Output is correct
7 Correct 520 ms 100064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 483 ms 100064 KB Output is correct
2 Correct 553 ms 100064 KB Output is correct
3 Correct 526 ms 103552 KB Output is correct
4 Execution timed out 2094 ms 103552 KB Time limit exceeded
5 Halted 0 ms 0 KB -