Submission #57246

# Submission time Handle Problem Language Result Execution time Memory
57246 2018-07-14T10:51:10 Z PeppaPig Islands (IOI08_islands) C++14
31 / 100
392 ms 68368 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];
bitset<N> vis;

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(!vis[k] && deg[k]) {
		int p = k;
		vector<pll> cyc;
		deque<pll> Q;
		while(!vis[p]) {
			vis[p] = true;
			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;
		for(int i = 1; i <= n; i++) ret = max(ret, max(dp[ret].x, dm[ret]));
		for(int i = 1; i < cyc.size(); i++) {
			while(!Q.empty() && Q.back().x < dis + dp[cyc[i].x].x + dp[cyc[0].x].x) Q.pop_back();
			Q.emplace_back(dis +  dp[cyc[i].x].x + dp[cyc[0].x].x, i);
			dis += cyc[i].y;
		}
		ret = max(ret, Q.front().x);
		for(int i = 0; i < cyc.size() - 1; i++) {
			dis -= cyc[i].y;
			while(!Q.empty() && Q.front().y <= i + 1) Q.pop_front();
			while(!Q.empty() && Q.back().x < dis + dp[cyc[i].x].x + dp[cyc[i + 1].x].x) Q.pop_back();
			Q.emplace_back(dis + dp[cyc[i].x].x + dp[cyc[i + 1].x].x, i);
			dis += cyc[i].y;
			ret = max(ret, Q.front().x);
		}
		
		ans += ret;
	}

	printf("%lld", ans);

	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 1; i < cyc.size(); i++) {
                  ~~^~~~~~~~~~~~
islands.cpp:56:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < cyc.size() - 1; i++) {
                  ~~^~~~~~~~~~~~~~~~
islands.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld", &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Incorrect 2 ms 460 KB Output isn't correct
3 Correct 2 ms 564 KB Output is correct
4 Correct 3 ms 564 KB Output is correct
5 Correct 4 ms 628 KB Output is correct
6 Correct 4 ms 628 KB Output is correct
7 Correct 2 ms 628 KB Output is correct
8 Correct 4 ms 628 KB Output is correct
9 Correct 3 ms 628 KB Output is correct
10 Incorrect 3 ms 660 KB Output isn't correct
11 Correct 3 ms 664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 808 KB Output is correct
2 Correct 4 ms 968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1660 KB Output is correct
2 Incorrect 26 ms 3280 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 4828 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 77 ms 14012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 193 ms 26404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 392 ms 52912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 360 ms 68340 KB Output is correct
2 Incorrect 336 ms 68368 KB Output isn't correct
3 Halted 0 ms 0 KB -