답안 #441590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441590 2021-07-05T13:23:59 Z rainboy Islands (IOI08_islands) C
100 / 100
446 ms 43076 KB
#include <stdio.h>

#define N	1000000
#define INF	0x3f3f3f3f3f3f3f3f

long long max(long long a, long long b) { return a > b ? a : b; }

int main() {
	static int pp[N], ww[N], in[N];
	static long long dd1[N], dd2[N];
	int n, i, j;
	long long ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d%d", &pp[i], &ww[i]), pp[i]--;
		in[pp[i]]++;
	}
	for (i = 0; i < n; i++)
		for (j = i; in[j] == 0; j = pp[j]) {
			int p = pp[j];

			dd2[p] = max(max(dd2[p], dd2[j]), dd1[p] + dd1[j] + ww[j]);
			dd1[p] = max(dd1[p], dd1[j] + ww[j]);
			in[j] = -1, in[p]--;
		}
	ans = 0;
	for (i = 0; i < n; i++)
		if (in[i] > 0) {
			long long d, e, f, w, x, y;

			d = e = f = -INF, w = 0, x = y = -INF, j = i;
			for (j = i; in[j] > 0; j = pp[j]) {
				in[j] = 0;
				d = max(d, dd2[j]);
				if (x != -INF)
					e = max(e, x + dd1[j]);
				if (y != -INF)
					f = max(f, y + dd1[j]);
				w += ww[j], x = max(x, dd1[j]) + ww[j], y = max(y, dd1[j]) - ww[j];
			}
			ans += max(d, max(e, f + w));
		}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

islands.c: In function 'main':
islands.c:14:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
islands.c:16:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   scanf("%d%d", &pp[i], &ww[i]), pp[i]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 716 KB Output is correct
2 Correct 13 ms 1580 KB Output is correct
3 Correct 6 ms 1100 KB Output is correct
4 Correct 4 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 1580 KB Output is correct
2 Correct 26 ms 3788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 5680 KB Output is correct
2 Correct 51 ms 6480 KB Output is correct
3 Correct 71 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 9960 KB Output is correct
2 Correct 123 ms 13868 KB Output is correct
3 Correct 149 ms 14076 KB Output is correct
4 Correct 178 ms 27208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 251 ms 11972 KB Output is correct
2 Correct 294 ms 19752 KB Output is correct
3 Correct 207 ms 21028 KB Output is correct
4 Correct 254 ms 27548 KB Output is correct
5 Correct 303 ms 27612 KB Output is correct
6 Correct 424 ms 41480 KB Output is correct
7 Correct 293 ms 42688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 267 ms 27664 KB Output is correct
2 Correct 281 ms 43076 KB Output is correct
3 Correct 257 ms 27352 KB Output is correct
4 Correct 261 ms 26916 KB Output is correct
5 Correct 258 ms 40388 KB Output is correct
6 Correct 254 ms 40104 KB Output is correct
7 Correct 446 ms 42020 KB Output is correct