Submission #312172

# Submission time Handle Problem Language Result Execution time Memory
312172 2020-10-12T14:45:49 Z Akashi Islands (IOI08_islands) C++14
80 / 100
852 ms 131076 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];
 
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] + 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 sol = 0;
	for (int i = 1; i <= n ; ++i) {
		if (viz[i]) continue ;
		cyc.clear();
		ans = 0;
		find_cycle(i);
 
		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);
					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 + elem.y;
			ans = max(ans, mx[nod]);
		}
 
		for (int i = cyc.size() - 2; i >= 0 ; --i) {
			int 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 (auto elem : cyc) {
			int nod = elem.x;
			s[nod] = mx[nod] + sum - sp[nod];
		}
		
		for (int i = cyc.size() - 2; i >= 0 ; --i) {
			int 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:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
islands.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   68 |   scanf("%d%d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23936 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 23936 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 16 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23936 KB Output is correct
2 Correct 16 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24064 KB Output is correct
2 Correct 17 ms 24320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 25208 KB Output is correct
2 Correct 37 ms 28288 KB Output is correct
3 Correct 28 ms 25592 KB Output is correct
4 Correct 22 ms 24704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 29816 KB Output is correct
2 Correct 67 ms 32756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 41636 KB Output is correct
2 Correct 125 ms 44272 KB Output is correct
3 Correct 156 ms 57064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 54008 KB Output is correct
2 Correct 263 ms 83816 KB Output is correct
3 Correct 296 ms 84072 KB Output is correct
4 Correct 365 ms 108488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 447 ms 72016 KB Output is correct
2 Runtime error 852 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 497 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -