Submission #53822

# Submission time Handle Problem Language Result Execution time Memory
53822 2018-07-01T08:54:53 Z 0^0(#1430) Islands (IOI08_islands) C++11
64 / 100
656 ms 145384 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<tuple<int,int,int> > adj[1000005];
int trip[1000005];
bool chk[1000005];
vector<int> cycle,len;
vector<ll> d;
ll dd[1000005];
int vn;
int dfs(int now, int par,int pw,int pi) {
	int ret = trip[now] = ++vn;
	for (auto &e : adj[now]) {
		int there, weight, idx;
		tie(there, weight, idx) = e;
		if (idx==pi)continue;
		if (!trip[there])ret = min(ret, dfs(there, now, weight, idx));
		else if(!chk[there]){
			ret = min(ret, trip[there]);
			cycle.push_back(there);
			chk[there] = true;
			len.push_back(weight);
		}
	}
	if (ret < trip[now]) {
		chk[now] = true;
		cycle.push_back(now);
		len.push_back(pw);
	}
	return ret;
}
void dfs2(int now, int par) {
	for (auto &e : adj[now]) {
		int there, weight, idx;
		tie(there, weight, idx) = e;
		if (there == par || chk[there])continue;
		dfs2(there, now);
		dd[now] = max(dd[now], dd[there] + weight);
	}
}
ll calc(vector<ll> d, vector<int> len, int m) {
	vector<ll> ds(m + 1);
	for (int i = 1; i <= m; i++)
		ds[i] = ds[i - 1] + len[i - 1];
	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;
	for (int i = 0; i < m; i++) {
		ll temp = d[i] + sgt.query(i + 1, i + m - 1) - ds[i];
		ret = max(ret, temp);
	}
	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, b, i);
		adj[a].emplace_back(i, b, i);
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		if (!trip[i]) {
			cycle.clear();
			len.clear();
			d.clear();
			dfs(i, 0, 0, 0);
			int m = cycle.size();
			d.resize(m);
			for (int i = 0; i < cycle.size(); i++) {
				dfs2(cycle[i], 0);
				d[i] = dd[cycle[i]];
			}
			ll now = calc(d, len, m);
			reverse(len.begin(), len.end() - 1);
			reverse(d.begin(), d.end());
			now = max(now, calc(d, len, m));
			ans += now;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:99:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < cycle.size(); i++) {
                    ~~^~~~~~~~~~~~~~
islands.cpp:83:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:86: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 20 ms 23800 KB Output is correct
2 Incorrect 20 ms 23800 KB Output isn't correct
3 Correct 21 ms 23944 KB Output is correct
4 Correct 21 ms 23944 KB Output is correct
5 Correct 24 ms 23944 KB Output is correct
6 Correct 21 ms 23944 KB Output is correct
7 Correct 21 ms 23960 KB Output is correct
8 Correct 20 ms 23960 KB Output is correct
9 Correct 26 ms 24056 KB Output is correct
10 Incorrect 21 ms 24056 KB Output isn't correct
11 Correct 21 ms 24056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 24120 KB Output is correct
2 Correct 22 ms 24120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 24140 KB Output is correct
2 Correct 23 ms 24524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 25392 KB Output is correct
2 Correct 70 ms 28372 KB Output is correct
3 Correct 35 ms 28372 KB Output is correct
4 Incorrect 28 ms 28372 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 30292 KB Output is correct
2 Correct 79 ms 32432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 38268 KB Output is correct
2 Correct 174 ms 46040 KB Output is correct
3 Correct 206 ms 63672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 244 ms 63672 KB Output is correct
2 Correct 352 ms 83728 KB Output is correct
3 Correct 427 ms 84620 KB Output is correct
4 Correct 526 ms 120656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 528 ms 120656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 656 ms 145384 KB Output is correct
2 Incorrect 634 ms 145384 KB Output isn't correct
3 Halted 0 ms 0 KB -