Submission #58265

# Submission time Handle Problem Language Result Execution time Memory
58265 2018-07-17T10:24:27 Z PeppaPig Islands (IOI08_islands) C++14
90 / 100
690 ms 131524 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;
	for(int i = 2; i <= sz; i++) {
		while(!Q.empty() && Q.back().x < V[i - 1].y + dp[V[i].x].x) Q.pop_back();
		Q.emplace_back(V[i - 1].y + dp[V[i].x].x, i);
	}
	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 < V[sz].y + V[i - 1].y + dp[V[i].x].x) Q.pop_back();
		Q.emplace_back(V[sz].y + V[i - 1].y + dp[V[i].x].x, i);
		ret = max(ret, dp[V[i + 1].x].x + Q.front().x - V[i].y);
	}

	return ret;
}

long long solve2(int 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);
		}
 
		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 += solve2(i);

	printf("%lld", ans);

	return 0;
}

Compilation message

islands.cpp: In function 'long long int solve2(int)':
islands.cpp:60:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 1; i < cyc.size(); i++) {
                  ~~^~~~~~~~~~~~
islands.cpp:66:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < cyc.size() - 1; i++) {
                  ~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:81: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 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 520 KB Output is correct
4 Correct 3 ms 520 KB Output is correct
5 Correct 2 ms 520 KB Output is correct
6 Correct 2 ms 520 KB Output is correct
7 Correct 2 ms 572 KB Output is correct
8 Correct 2 ms 572 KB Output is correct
9 Correct 3 ms 572 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 724 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 724 KB Output is correct
2 Correct 4 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1388 KB Output is correct
2 Correct 24 ms 2668 KB Output is correct
3 Correct 10 ms 2668 KB Output is correct
4 Correct 6 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3560 KB Output is correct
2 Correct 36 ms 5664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 10980 KB Output is correct
2 Correct 72 ms 12416 KB Output is correct
3 Correct 121 ms 16860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 18612 KB Output is correct
2 Correct 194 ms 29408 KB Output is correct
3 Correct 177 ms 29408 KB Output is correct
4 Correct 268 ms 39680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 358 ms 39680 KB Output is correct
2 Correct 566 ms 46300 KB Output is correct
3 Correct 289 ms 46300 KB Output is correct
4 Correct 415 ms 56840 KB Output is correct
5 Correct 383 ms 57716 KB Output is correct
6 Correct 690 ms 57716 KB Output is correct
7 Correct 434 ms 80820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 422 ms 80820 KB Output is correct
2 Correct 414 ms 80820 KB Output is correct
3 Correct 448 ms 93788 KB Output is correct
4 Correct 550 ms 93788 KB Output is correct
5 Runtime error 362 ms 131524 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
6 Halted 0 ms 0 KB -