Submission #53875

# Submission time Handle Problem Language Result Execution time Memory
53875 2018-07-01T13:17:29 Z baactree Islands (IOI08_islands) C++17
100 / 100
1549 ms 163920 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct SGT {
	vector<ll> tree;
	int sz;
	SGT(int n) {
		sz = 1;
		while (sz < n)sz *= 2;
		tree.resize(sz * 2);
	}
	ll query(int le, int ri) {
		le += sz;
		ri += sz;
		ll ret = 0;
		while (le <= ri) {
			if (le & 1)ret = max(ret, tree[le++]);
			if (!(ri & 1))ret = max(ret, tree[ri--]);
			le /= 2;
			ri /= 2;
		}
		return ret;
	}
};
int n;
vector<pair<int,int> > adj[1000005];
int trip[1000005], w[1000005];
bool chk[1000005];
vector<int> cycle,len;
ll dd[1000005];
vector<ll> d;
int r[1000005];
int vn;
void dfs(int now, int pi) {
	r[now] = trip[now] = ++vn;
	for (auto &e : adj[now]) {
		if (e.second==pi)continue;
		if (!trip[e.first]) {
			dfs(e.first, e.second);
			r[now] = min(r[now], r[e.first]);
		}
		else if(!chk[e.first]){
			r[now] = min(r[now], trip[e.first]);
			cycle.push_back(e.first);
			chk[e.first] = true;
			len.push_back(w[e.second]);
		}
	}
	if (r[now] < trip[now]) {
		chk[now] = true;
		cycle.push_back(now);
		len.push_back(w[pi]);
	}
}
ll val;
void dfs2(int now) {
	chk[now] = true;
	for (auto &e : adj[now]) {
		if (chk[e.first])continue;
		dfs2(e.first);
		val = max(val, dd[now] + dd[e.first] + w[e.second]);
		dd[now] = max(dd[now], dd[e.first] + w[e.second]);
	}
}
ll calc(vector<ll> &d, vector<int> &len, int m) {
	SGT sgt(m * 2);
	ll sum = len[0];
	for (int i = 1; i < m * 2; i++) {
		sgt.tree[i + sgt.sz] = sum + d[i%m];
		sum += len[i%m];
	}
	for (int i = sgt.sz - 1; i; i--)
		sgt.tree[i] = max(sgt.tree[i * 2], sgt.tree[i * 2 + 1]);
	ll ret = 0;
	sum = 0;
	for (int i = 0; i < m; i++) {
		ll temp = d[i] + sgt.query(i + 1, i + m - 1) - sum;
		ret = max(ret, temp);
		sum += len[i];
	}
	return ret;
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		adj[i].emplace_back(a,i);
		adj[a].emplace_back(i,i);
		w[i] = b;
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		if (!trip[i]) {
			cycle.clear();
			len.clear();
			dfs(i, 0);
			int m = cycle.size();
			d = vector<ll>(m);
			val = 0;
			for (int i = 0; i < cycle.size(); i++) {
				dfs2(cycle[i]);
				d[i] = dd[cycle[i]];
			}
			val = max(val,calc(d, len, m));
			reverse(len.begin(), len.end() - 1);
			reverse(d.begin(), d.end());
			val = max(val, calc(d, len, m));
			ans += val;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:101:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < cycle.size(); i++) {
                    ~~^~~~~~~~~~~~~~
islands.cpp:84:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23800 KB Output is correct
2 Correct 21 ms 23912 KB Output is correct
3 Correct 21 ms 23964 KB Output is correct
4 Correct 21 ms 23980 KB Output is correct
5 Correct 21 ms 23980 KB Output is correct
6 Correct 20 ms 24064 KB Output is correct
7 Correct 23 ms 24100 KB Output is correct
8 Correct 26 ms 24100 KB Output is correct
9 Correct 21 ms 24100 KB Output is correct
10 Correct 22 ms 24164 KB Output is correct
11 Correct 23 ms 24172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 24188 KB Output is correct
2 Correct 24 ms 24320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 24320 KB Output is correct
2 Correct 24 ms 24572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 25344 KB Output is correct
2 Correct 46 ms 27772 KB Output is correct
3 Correct 36 ms 27772 KB Output is correct
4 Correct 29 ms 27772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 29436 KB Output is correct
2 Correct 79 ms 31948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 37968 KB Output is correct
2 Correct 166 ms 43904 KB Output is correct
3 Correct 221 ms 56324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 245 ms 56324 KB Output is correct
2 Correct 360 ms 74232 KB Output is correct
3 Correct 461 ms 76012 KB Output is correct
4 Correct 459 ms 101984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 452 ms 101984 KB Output is correct
2 Correct 1418 ms 126292 KB Output is correct
3 Correct 479 ms 126292 KB Output is correct
4 Correct 614 ms 126292 KB Output is correct
5 Correct 608 ms 126292 KB Output is correct
6 Correct 1462 ms 126292 KB Output is correct
7 Correct 758 ms 152352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 590 ms 152352 KB Output is correct
2 Correct 542 ms 152352 KB Output is correct
3 Correct 954 ms 163920 KB Output is correct
4 Correct 591 ms 163920 KB Output is correct
5 Correct 632 ms 163920 KB Output is correct
6 Correct 695 ms 163920 KB Output is correct
7 Correct 1549 ms 163920 KB Output is correct