답안 #53882

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
53882 2018-07-01T13:51:59 Z baactree Islands (IOI08_islands) C++17
100 / 100
1527 ms 193420 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];
bool chk[1000005];
vector<ll> cycle(1000005);
vector<int> len(10000005);
ll dd[1000005], a[2000005];
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) {
	deque<int> dq;
	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:85:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < cycle.size(); i++) {
                    ~~^~~~~~~~~~~~~~
islands.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:72: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 58 ms 70776 KB Output is correct
2 Correct 59 ms 70888 KB Output is correct
3 Correct 57 ms 70960 KB Output is correct
4 Correct 60 ms 70960 KB Output is correct
5 Correct 58 ms 70960 KB Output is correct
6 Correct 58 ms 71140 KB Output is correct
7 Correct 58 ms 71140 KB Output is correct
8 Correct 60 ms 71140 KB Output is correct
9 Correct 58 ms 71144 KB Output is correct
10 Correct 59 ms 71144 KB Output is correct
11 Correct 55 ms 71144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 71144 KB Output is correct
2 Correct 60 ms 71144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 71188 KB Output is correct
2 Correct 61 ms 71396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 72072 KB Output is correct
2 Correct 79 ms 74340 KB Output is correct
3 Correct 72 ms 74340 KB Output is correct
4 Correct 63 ms 74340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 75568 KB Output is correct
2 Correct 113 ms 77932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 84204 KB Output is correct
2 Correct 168 ms 87916 KB Output is correct
3 Correct 200 ms 97308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 259 ms 97308 KB Output is correct
2 Correct 346 ms 115928 KB Output is correct
3 Correct 329 ms 118764 KB Output is correct
4 Correct 406 ms 138356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 485 ms 138356 KB Output is correct
2 Correct 1238 ms 164472 KB Output is correct
3 Correct 507 ms 164472 KB Output is correct
4 Correct 565 ms 164472 KB Output is correct
5 Correct 578 ms 164472 KB Output is correct
6 Correct 1458 ms 164472 KB Output is correct
7 Correct 607 ms 175612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 592 ms 184188 KB Output is correct
2 Correct 633 ms 184188 KB Output is correct
3 Correct 617 ms 193420 KB Output is correct
4 Correct 753 ms 193420 KB Output is correct
5 Correct 572 ms 193420 KB Output is correct
6 Correct 527 ms 193420 KB Output is correct
7 Correct 1527 ms 193420 KB Output is correct