Submission #312155

# Submission time Handle Problem Language Result Execution time Memory
312155 2020-10-12T14:04:36 Z Akashi Islands (IOI08_islands) C++14
9 / 100
517 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.begin()->x);	
			ans = max(ans, mx[nod] + sp[nod] + s2.begin()->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);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23808 KB Output isn't correct
2 Incorrect 17 ms 23808 KB Output isn't correct
3 Incorrect 17 ms 23936 KB Output isn't correct
4 Correct 16 ms 23808 KB Output is correct
5 Correct 17 ms 23808 KB Output is correct
6 Incorrect 17 ms 23808 KB Output isn't correct
7 Incorrect 17 ms 23808 KB Output isn't correct
8 Incorrect 16 ms 23808 KB Output isn't correct
9 Incorrect 18 ms 23936 KB Output isn't correct
10 Incorrect 18 ms 23808 KB Output isn't correct
11 Correct 17 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 24064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 24064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 25600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 60 ms 32500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 143 ms 44920 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 278 ms 58104 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 442 ms 87512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 517 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -