답안 #57253

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57253 2018-07-14T11:10:35 Z PeppaPig Islands (IOI08_islands) C++14
100 / 100
878 ms 68012 KB
#include <bits/stdc++.h>

#define pll pair<long long, long long>
#define x first
#define y second

using namespace std;

const int N = 1e6 + 5;

int n;
pll par[N], dp[N];
long long dm[N], ans;
int deg[N];

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		long long v, w;
		scanf("%lld%lld", &v, &w);
		par[i] = pll(v, w);
		deg[v]++;
	}
	queue<int> Q;
	for(int i = 1; i <= n; i++) if(!deg[i]) Q.emplace(i);
	while(!Q.empty()) {
		int now = Q.front();
		Q.pop();
		dm[now] = max(dm[now], dp[now].x + dp[now].y);
		dm[par[now].x] = max(dm[par[now].x], dm[now]);
		long long dis = dp[now].x + par[now].y;
		if(dis > dp[par[now].x].x) swap(dis, dp[par[now].x].x);
		if(dis > dp[par[now].x].y) swap(dis, dp[par[now].x].y);
		if(!--deg[par[now].x]) Q.emplace(par[now].x);
	}

	for(int k = 1; k <= n; k++) if(deg[k]) {
		int p = k;
		vector<pll> cyc;
		deque<pll> Q;
		while(deg[p]) {
			deg[p]--;
          	dm[p] = max(dm[p], dp[p].x + dp[p].y);
			cyc.emplace_back(p, par[p].y);
			p = par[p].x;
		}
		long long ret = 0, dis = cyc[0].y, pdis = 0;
		for(pll v : cyc) ret = max(ret, dm[v.x]);
		for(int i = 1; i < cyc.size(); i++) {
			while(!Q.empty() && Q.back().x < dis + dp[cyc[i].x].x) Q.pop_back();
			Q.emplace_back(dis +  dp[cyc[i].x].x, i);
			dis += cyc[i].y;
		}
		ret = max(ret, dp[cyc[0].x].x + Q.front().x);
		for(int i = 0; i < cyc.size() - 1; i++) {
			if(Q.front().y == i + 1) Q.pop_front();
			while(!Q.empty() && Q.back().x < dis + dp[cyc[i].x].x) Q.pop_back();
			Q.emplace_back(dis + dp[cyc[i].x].x, i);
			dis += cyc[i].y;
			pdis += cyc[i].y;
			ret = max(ret, dp[cyc[i + 1].x].x + Q.front().x - pdis);
		}

		ans += ret;
	}

	printf("%lld", ans);

	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:49:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 1; i < cyc.size(); i++) {
                  ~~^~~~~~~~~~~~
islands.cpp:55:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < cyc.size() - 1; i++) {
                  ~~^~~~~~~~~~~~~~~~
islands.cpp:17:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:20:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld", &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 452 KB Output is correct
4 Correct 2 ms 528 KB Output is correct
5 Correct 2 ms 528 KB Output is correct
6 Correct 2 ms 564 KB Output is correct
7 Correct 3 ms 564 KB Output is correct
8 Correct 4 ms 564 KB Output is correct
9 Correct 3 ms 564 KB Output is correct
10 Correct 3 ms 564 KB Output is correct
11 Correct 3 ms 564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 608 KB Output is correct
2 Correct 4 ms 608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 4 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 1404 KB Output is correct
2 Correct 18 ms 2560 KB Output is correct
3 Correct 15 ms 2560 KB Output is correct
4 Correct 7 ms 2560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 3768 KB Output is correct
2 Correct 37 ms 5616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 10368 KB Output is correct
2 Correct 116 ms 14044 KB Output is correct
3 Correct 103 ms 22108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 132 ms 23368 KB Output is correct
2 Correct 182 ms 33516 KB Output is correct
3 Correct 225 ms 33516 KB Output is correct
4 Correct 321 ms 43688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 332 ms 43688 KB Output is correct
2 Correct 515 ms 50376 KB Output is correct
3 Correct 264 ms 50376 KB Output is correct
4 Correct 397 ms 59284 KB Output is correct
5 Correct 397 ms 60152 KB Output is correct
6 Correct 683 ms 60152 KB Output is correct
7 Correct 413 ms 68012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 397 ms 68012 KB Output is correct
2 Correct 404 ms 68012 KB Output is correct
3 Correct 423 ms 68012 KB Output is correct
4 Correct 433 ms 68012 KB Output is correct
5 Correct 325 ms 68012 KB Output is correct
6 Correct 429 ms 68012 KB Output is correct
7 Correct 878 ms 68012 KB Output is correct