답안 #103660

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
103660 2019-04-01T20:40:04 Z KCSC Islands (IOI08_islands) C++14
100 / 100
746 ms 80620 KB
// just because dfs doesn't work :<
#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;
long long deg[N], dm[N];
pll par[N], dp[N];
bitset<N> vis;
 
long long solve(int u) {
	long long ret = 0;
	int sz;
	vector<pll> V;
	deque<pll> Q;
 
	V.emplace_back(0, 0);
	while(deg[u]) {
		deg[u]--;
		dm[u] = max(dm[u], dp[u].x + dp[u].y);
		ret = max(ret, dm[u]);
		V.emplace_back(u, par[u].y);
		u = par[u].x;
	}
 
 	sz = V.size() - 1;
	for(int i = 1; i <= sz; i++) V[i].y += V[i - 1].y;
	for(int i = 2; i <= sz; i++) {
		while(!Q.empty() && Q.back().x < V[i - 1].y + dp[V[i].x].x) Q.pop_back();
		Q.emplace_back(V[i - 1].y + dp[V[i].x].x, i);
	}
	ret = max(ret, dp[V[1].x].x + Q.front().x);
	for(int i = 1; i < sz; i++) {
		if(Q.front().y == i + 1) Q.pop_front();
		while(!Q.empty() && Q.back().x < V[sz].y + V[i - 1].y + dp[V[i].x].x) Q.pop_back();
		Q.emplace_back(V[sz].y + V[i - 1].y + dp[V[i].x].x, i);
		ret = max(ret, dp[V[i + 1].x].x + Q.front().x - V[i].y);
	}
 
	return ret;
}
 
int main() {
	scanf("%d", &n);
	for(int i = 1, v, w; i <= n; i++) {
		scanf("%d %d", &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();
 
		long long p = par[now].x, w = par[now].y;
		dm[now] = max(dm[now], dp[now].x + dp[now].y);
		dm[p] = max(dm[p], dm[now]);
 
		long long ret = dp[now].x + w;
		if(ret > dp[p].x) swap(ret, dp[p].x);
		if(ret > dp[p].y) swap(ret, dp[p].y);
		if(!--deg[p]) Q.emplace(p);
	}
 
	long long ans = 0;
	for(int i = 1; i <= n; i++) if(deg[i]) ans += solve(i);
 
	printf("%lld", ans);
 
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:52:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 1152 KB Output is correct
2 Correct 14 ms 2404 KB Output is correct
3 Correct 9 ms 1408 KB Output is correct
4 Correct 6 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 3452 KB Output is correct
2 Correct 32 ms 5444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 10696 KB Output is correct
2 Correct 86 ms 12124 KB Output is correct
3 Correct 84 ms 16620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 18468 KB Output is correct
2 Correct 181 ms 29008 KB Output is correct
3 Correct 167 ms 28664 KB Output is correct
4 Correct 259 ms 39404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 28000 KB Output is correct
2 Correct 436 ms 61284 KB Output is correct
3 Correct 265 ms 48708 KB Output is correct
4 Correct 331 ms 69256 KB Output is correct
5 Correct 352 ms 69756 KB Output is correct
6 Correct 746 ms 62076 KB Output is correct
7 Correct 338 ms 80620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 301 ms 62744 KB Output is correct
2 Correct 301 ms 62868 KB Output is correct
3 Correct 318 ms 63064 KB Output is correct
4 Correct 392 ms 46712 KB Output is correct
5 Correct 300 ms 70256 KB Output is correct
6 Correct 332 ms 62872 KB Output is correct
7 Correct 714 ms 62836 KB Output is correct