답안 #312177

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
312177 2020-10-12T14:53:10 Z Akashi Islands (IOI08_islands) C++14
66 / 100
1048 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
 
#define x first
#define y second
 
const int DIM = 1e6 + 1;
 
int n;
long long ans;
bool viz[DIM];
int tt[DIM], ct[DIM];
long long s[DIM], dp[DIM], sp[DIM], mx[DIM];
 
vector <pair <int, int>> v[DIM];
 
int nr;
pair <int, int> cyc[DIM];
bool onCyc[DIM];
 
bool find_cycle(int nod) {
	viz[nod] = true;
	
	bool found = false;
	bool foundPapa = false;
	for (auto it : v[nod]) {
		if (it.x == tt[nod] && !foundPapa) {
			foundPapa = true;
			continue ;
		}
 
		if (!found && viz[it.x]) {
			int now = nod;
			
			while (now != tt[it.x]) {
				if (now != it.x) cyc[++nr] = {now, ct[now]};
				else cyc[++nr] = {now, it.y};
				onCyc[now] = true;
				now = tt[now];
			}
			found = true;
			continue ;
		}
 
		if (viz[it.x]) continue ;
		
		ct[it.x] = it.y; tt[it.x] = nod;
		bool ok = find_cycle(it.x);
		found |= ok;
	}
 
	return found;
}
 
void dfs(int nod) {
	for (auto it : v[nod]) {
		if (onCyc[it.x] || it.x == tt[nod]) continue ;
		tt[it.x] = nod;
		dfs(it.x);
		ans = max(ans, dp[nod] + dp[it.x] + it.y);
		dp[nod] = max(dp[nod], dp[it.x] + it.y);
	}
}
 
int main() {
	scanf("%d", &n);
	int x, y;
	for (int i = 1; i <= n ; ++i) {
		scanf("%d%d", &x, &y);
 
		v[i].push_back({x, y});
		v[x].push_back({i, y});
	}
 
	long long sum, sol = 0;
	int nod;
	for (int i = 1; i <= n ; ++i) {
		if (viz[i]) continue ;
		nr = 0; ans = 0;
		find_cycle(i);
		
		sum = 0;
		for (int i = 1; i <= nr ; ++i) {
			nod = cyc[i].x;
			sp[nod] = sum;
			for (auto it : v[nod]) {
				if (!onCyc[it.x]) {
					dfs(it.x);
					ans = max(ans, mx[nod] + dp[it.x] + it.y);
					mx[nod] = max(mx[nod], dp[it.x] + it.y);
				}
			}
			s[nod] = mx[nod] + sp[nod];
			sum = sum + cyc[i].y;
			ans = max(ans, mx[nod]);
		}
 
		for (int i = nr - 1; i >= 1 ; --i) {
			nod = cyc[i].x;
			ans = max(ans, mx[nod] - sp[nod] + s[cyc[i + 1].x]);
			s[nod] = max(s[nod], s[cyc[i + 1].x]);
		}
		
		for (int i = 1; i <= nr ; ++i) {
			nod = cyc[i].x;
			s[nod] = mx[nod] + sum - sp[nod];
		}
		
		for (int i = nr - 1; i >= 1 ; --i) {
			nod = cyc[i].x;
			ans = max(ans, mx[nod] + sp[nod] + s[cyc[i + 1].x]);
			s[nod] = max(s[nod], s[cyc[i + 1].x]);
		}
		
 
		sol += ans;
	}
 
	printf("%lld\n", sol);
 
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   66 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
islands.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |   scanf("%d%d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23936 KB Output is correct
3 Correct 15 ms 23936 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 15 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23936 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 24064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 25216 KB Output is correct
2 Correct 39 ms 27768 KB Output is correct
3 Incorrect 28 ms 25472 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 29176 KB Output is correct
2 Correct 63 ms 31992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 40952 KB Output is correct
2 Correct 118 ms 41720 KB Output is correct
3 Correct 151 ms 52176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 196 ms 53376 KB Output is correct
2 Correct 252 ms 77620 KB Output is correct
3 Correct 265 ms 75772 KB Output is correct
4 Correct 347 ms 96248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 432 ms 71760 KB Output is correct
2 Correct 913 ms 119480 KB Output is correct
3 Correct 378 ms 85112 KB Output is correct
4 Correct 493 ms 120056 KB Output is correct
5 Correct 486 ms 129272 KB Output is correct
6 Incorrect 1048 ms 81400 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 523 ms 131072 KB Output isn't correct
2 Halted 0 ms 0 KB -