Submission #58263

# Submission time Handle Problem Language Result Execution time Memory
58263 2018-07-17T10:21:54 Z PeppaPig Islands (IOI08_islands) C++14
67 / 100
722 ms 57832 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;
long long deg[N], dm[N];
pll par[N], dp[N];
bitset<N> vis;

long long solve(int u) {
	long long ret = 0;
	int sz;
	vector<pll> V;
	deque<pll> Q;

	V.emplace_back(0, 0);
	while(deg[u]) {
		deg[u]--;
		dm[u] = max(dm[u], dp[u].x + dp[u].y);
		ret = max(ret, dm[u]);
		V.emplace_back(u, par[u].y);
		u = par[u].x;
	}

 	sz = V.size() - 1;
	//for(int i = 1; i <= sz; i++) V[i].y += V[i - 1].y;
	long long dis = V[1].y, pdis = 0;
	for(int i = 2; i <= sz; i++) {
		while(!Q.empty() && Q.back().x < dis + dp[V[i].x].x) Q.pop_back();
		Q.emplace_back(dis + dp[V[i].x].x, i);
		dis += V[i].y;
	}
	ret = max(ret, dp[V[1].x].x + Q.front().x);
	for(int i = 1; i < sz; i++) {
		if(Q.front().y == i) Q.pop_front();
		while(!Q.empty() && Q.back().x < dis + dp[V[i].x].x) Q.pop_back();
		Q.emplace_back(dis + dp[V[i].x].x, i);
		dis += V[i].y, pdis += V[i].y;
		ret = max(ret, dp[V[i + 1].x].x + Q.front().x - pdis);
	}

	return ret;
}

int main() {
	scanf("%d", &n);
	for(int i = 1, v, w; i <= n; i++) {
		scanf("%d %d", &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();

		long long p = par[now].x, w = par[now].y;
		dm[now] = max(dm[now], dp[now].x + dp[now].y);
		dm[p] = max(dm[p], dm[now]);

		long long ret = dp[now].x + w;
		if(ret > dp[p].x) swap(ret, dp[p].x);
		if(ret > dp[p].y) swap(ret, dp[p].y);
		if(!--deg[p]) Q.emplace(p);
	}

	long long ans = 0;
	for(int i = 1; i <= n; i++) if(deg[i]) ans += solve(i);

	printf("%lld", ans);

	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 376 KB Output isn't correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 428 KB Output is correct
5 Correct 2 ms 476 KB Output is correct
6 Correct 2 ms 480 KB Output is correct
7 Correct 2 ms 480 KB Output is correct
8 Correct 2 ms 584 KB Output is correct
9 Correct 2 ms 584 KB Output is correct
10 Correct 2 ms 584 KB Output is correct
11 Correct 3 ms 584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 584 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 4 ms 780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1292 KB Output is correct
2 Correct 24 ms 2616 KB Output is correct
3 Correct 12 ms 2616 KB Output is correct
4 Incorrect 8 ms 2616 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 3560 KB Output is correct
2 Correct 37 ms 5664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 10932 KB Output is correct
2 Correct 90 ms 12416 KB Output is correct
3 Correct 116 ms 16984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 18656 KB Output is correct
2 Correct 201 ms 29188 KB Output is correct
3 Correct 201 ms 29188 KB Output is correct
4 Correct 277 ms 39760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 412 ms 39760 KB Output is correct
2 Correct 510 ms 46224 KB Output is correct
3 Correct 360 ms 46224 KB Output is correct
4 Correct 449 ms 57000 KB Output is correct
5 Correct 408 ms 57832 KB Output is correct
6 Incorrect 722 ms 57832 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 535 ms 57832 KB Output isn't correct
2 Halted 0 ms 0 KB -