Submission #53749

# Submission time Handle Problem Language Result Execution time Memory
53749 2018-07-01T06:33:27 Z 강한육군장홍준(#2037) Islands (IOI08_islands) C++11
80 / 100
1034 ms 99632 KB
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>edge[1212121];
long long RD[1212121], D[1212121], X[1212121], Y[1212121], indeg[1212121], SV[1212121], L[1212121], V[1212121];
long long max(long long a, long long b) { if (a < b)return b; return a; }
bool sort_DY(int a, int b) { return D[a] + Y[a] > D[b] + Y[b]; }
int get_RD(int x) {
	if (edge[x].size() < 2)return 0;
	sort(edge[x].begin(), edge[x].end(), sort_DY);
	return D[edge[x][0]] + D[edge[x][1]] + Y[edge[x][0]] + Y[edge[x][1]];
}
int main() {
	vector<long long>Q;
	long long n, i, j; scanf("%lld", &n);
	for (i = 1; i <= n; i++) scanf("%lld%lld", &X[i], &Y[i]), indeg[X[i]]++, edge[X[i]].push_back(i);
	for (i = 1; i <= n; i++)if (indeg[i] == 0)Q.push_back(i);
	while (!Q.empty()) {
		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]);
		X[now] = 0;
	}
	long long ans = 0;
	for (i = 1; i <= n; i++) if(X[i]){
		long long now = i, S = 0, sz = 0, res = 0, mx1 = -1e18,mx2 = -1e18;
		while (X[now]) {
			L[++sz] = D[now]; V[sz] = Y[now];
			res = max(res, max(RD[now], get_RD(now)));
			S += Y[now];
			long long tmp = now; now = X[now]; X[tmp] = 0;
		}
		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;
	}
	printf("%lld", ans);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:16:18: warning: unused variable 'j' [-Wunused-variable]
  long long n, i, j; scanf("%lld", &n);
                  ^
islands.cpp:16:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  long long n, i, j; scanf("%lld", &n);
                     ~~~~~^~~~~~~~~~~~
islands.cpp:17:73: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (i = 1; i <= n; i++) scanf("%lld%lld", &X[i], &Y[i]), indeg[X[i]]++, edge[X[i]].push_back(i);
                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 28792 KB Output is correct
2 Correct 27 ms 28904 KB Output is correct
3 Correct 27 ms 29084 KB Output is correct
4 Correct 26 ms 29084 KB Output is correct
5 Correct 26 ms 29084 KB Output is correct
6 Correct 33 ms 29084 KB Output is correct
7 Correct 27 ms 29084 KB Output is correct
8 Correct 32 ms 29084 KB Output is correct
9 Correct 32 ms 29084 KB Output is correct
10 Correct 25 ms 29084 KB Output is correct
11 Correct 25 ms 29084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 29264 KB Output is correct
2 Correct 30 ms 29264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 29264 KB Output is correct
2 Correct 28 ms 29356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 30056 KB Output is correct
2 Correct 51 ms 31588 KB Output is correct
3 Correct 43 ms 31588 KB Output is correct
4 Correct 32 ms 31588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 32636 KB Output is correct
2 Correct 73 ms 35448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 41808 KB Output is correct
2 Correct 106 ms 44708 KB Output is correct
3 Correct 145 ms 48120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 53488 KB Output is correct
2 Correct 251 ms 63332 KB Output is correct
3 Correct 245 ms 69292 KB Output is correct
4 Correct 307 ms 77288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 648 ms 77288 KB Output is correct
2 Correct 840 ms 92388 KB Output is correct
3 Correct 462 ms 92388 KB Output is correct
4 Correct 535 ms 96764 KB Output is correct
5 Correct 427 ms 96764 KB Output is correct
6 Incorrect 1034 ms 96764 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 431 ms 99632 KB Output is correct
2 Incorrect 421 ms 99632 KB Output isn't correct
3 Halted 0 ms 0 KB -