답안 #53885

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
53885 2018-07-01T14:00:00 Z baactree Islands (IOI08_islands) C++17
100 / 100
1587 ms 177156 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
vector<pair<int, int> > adj[1000005];
int trip[1000005], w[1000005];
bitset<1000005> chk;
vector<ll> cycle;
vector<int> len;
ll dd[1000005];
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] = 1;
			len.push_back(w[e.second]);
		}
	}
	if (r[now] < trip[now]) {
		chk[now] = 1;
		cycle.push_back(now);
		len.push_back(w[pi]);
	}
}
ll val;
void dfs2(int now) {
	chk[now] = 1;
	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) {
	deque<int> dq;
	vector<ll> a(m * 2);
	ll sum = len[0];
	for (int i = 1; i < m * 2; i++) {
		a[i] = sum + d[i%m];
		sum += len[i%m];
	}
	ll ret = 0;
	sum = 0;
	int ri;
	ri = 0;
	for (int i = 0; i < m; i++) {
		for (int j = ri; j < i + m; j++) {
			while (!dq.empty() && a[dq.back()] <= a[j])dq.pop_back();
			dq.push_back(j);
		}
		ri = i + m;
		while (!dq.empty() && dq.front() <= i)dq.pop_front();
		ll temp = d[i] + a[dq.front()] - 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();
			val = 0;
			for (int i = 0; i < cycle.size(); i++) {
				dfs2(cycle[i]);
				cycle[i] = dd[cycle[i]];
			}
			val = max(val, calc(cycle, len, m));
			reverse(len.begin(), len.end() - 1);
			reverse(cycle.begin(), cycle.end());
			val = max(val, calc(cycle, len, m));
			ans += val;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:86:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < cycle.size(); i++) {
                    ~~^~~~~~~~~~~~~~
islands.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23800 KB Output is correct
2 Correct 23 ms 24036 KB Output is correct
3 Correct 22 ms 24104 KB Output is correct
4 Correct 25 ms 24104 KB Output is correct
5 Correct 23 ms 24104 KB Output is correct
6 Correct 22 ms 24104 KB Output is correct
7 Correct 22 ms 24104 KB Output is correct
8 Correct 21 ms 24104 KB Output is correct
9 Correct 21 ms 24104 KB Output is correct
10 Correct 21 ms 24104 KB Output is correct
11 Correct 22 ms 24104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24164 KB Output is correct
2 Correct 23 ms 24172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 24172 KB Output is correct
2 Correct 24 ms 24556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 25212 KB Output is correct
2 Correct 48 ms 27780 KB Output is correct
3 Correct 38 ms 27780 KB Output is correct
4 Correct 29 ms 27780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 29240 KB Output is correct
2 Correct 73 ms 31516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 37696 KB Output is correct
2 Correct 146 ms 43296 KB Output is correct
3 Correct 188 ms 55408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 229 ms 55408 KB Output is correct
2 Correct 335 ms 75024 KB Output is correct
3 Correct 315 ms 79316 KB Output is correct
4 Correct 423 ms 103804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 430 ms 103804 KB Output is correct
2 Correct 1219 ms 132384 KB Output is correct
3 Correct 413 ms 132384 KB Output is correct
4 Correct 636 ms 132384 KB Output is correct
5 Correct 556 ms 132384 KB Output is correct
6 Correct 1539 ms 132384 KB Output is correct
7 Correct 666 ms 148056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 651 ms 151668 KB Output is correct
2 Correct 552 ms 151668 KB Output is correct
3 Correct 728 ms 177156 KB Output is correct
4 Correct 678 ms 177156 KB Output is correct
5 Correct 578 ms 177156 KB Output is correct
6 Correct 608 ms 177156 KB Output is correct
7 Correct 1587 ms 177156 KB Output is correct