답안 #312161

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
312161 2020-10-12T14:15:26 Z Akashi Islands (IOI08_islands) C++14
54 / 100
666 ms 131076 KB
#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second

const int DIM = 1e6 + 5;

int n;
long long ans;
bool viz[DIM];
int tt[DIM], ct[DIM];
long long dp[DIM], sp[DIM], mx[DIM];

vector <pair <int, int>> v[DIM];

vector <pair <int, int>> cyc;
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.push_back({now, ct[now]});
				else cyc.push_back({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, int papa = 0) {
	for (auto it : v[nod]) {
		if (onCyc[it.x] || it.x == papa) continue ;
		dfs(it.x, nod);
		ans = max(ans, dp[nod] + 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 sol = 0;
	for (int i = 1; i <= n ; ++i) {
		if (viz[i]) continue ;
		cyc.clear();
		ans = 0;
		find_cycle(i);

		set <pair <long long, int>> s1;
		set <pair <long long, int>> s2;

		long long sum = 0;
		for (auto elem : cyc) {
			int nod = elem.x;
			sp[nod] = sum;
			for (auto it : v[nod]) {
				if (!onCyc[it.x]) {
					dfs(it.x);
					mx[nod] = max(mx[nod], dp[it.x] + it.y);
				}
			}
			s1.insert({mx[nod] + sp[nod], nod});
			sum = sum + elem.y;
			ans = max(ans, mx[nod]);
		}

		for (auto elem : cyc) {
			int nod = elem.x;
			s2.insert({mx[nod] + sum - sp[nod], nod});
		}

		for (auto elem : cyc) {
			int nod = elem.x;	
			s1.erase({mx[nod] + sp[nod], nod});
			s2.erase({mx[nod] + sum - sp[nod], nod});
	
			if (s1.empty()) break ;
			ans = max(ans, mx[nod] - sp[nod] + s1.rbegin()->x);	
			ans = max(ans, mx[nod] + sp[nod] + s2.rbegin()->x);	
		}
	
		sol += ans;
	}

	printf("%lld\n", sol);

	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   64 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
islands.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |   scanf("%d%d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Incorrect 15 ms 23808 KB Output isn't correct
3 Correct 16 ms 23936 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 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 Incorrect 15 ms 23808 KB Output isn't correct
11 Correct 15 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 24096 KB Output is correct
2 Correct 17 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23936 KB Output is correct
2 Correct 19 ms 24576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 25344 KB Output is correct
2 Correct 48 ms 30328 KB Output is correct
3 Correct 29 ms 25600 KB Output is correct
4 Incorrect 22 ms 24704 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 31860 KB Output is correct
2 Correct 89 ms 35364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 42488 KB Output is correct
2 Correct 216 ms 54512 KB Output is correct
3 Correct 271 ms 77412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 265 ms 52728 KB Output is correct
2 Correct 395 ms 110532 KB Output is correct
3 Correct 666 ms 117928 KB Output is correct
4 Runtime error 466 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Incorrect 433 ms 71760 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 488 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -