Submission #796423

# Submission time Handle Problem Language Result Execution time Memory
796423 2023-07-28T11:29:22 Z rxlfd314 Islands (IOI08_islands) C++17
100 / 100
574 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ari2 = array<int, 2>;
using arl2 = array<ll, 2>;

#define cerr if (false) cout

constexpr int mxN = 1e6;
int N, F[mxN][2];
vector<ari2> adj[mxN];
bool seen[mxN], in_cyc[mxN];

ll res;
ll dfs(int f, ll d) {
	ll ret = d, ret2 = d;
	seen[f] = true;
	int cnt = 0;
	for (auto [nf, v] : adj[f]) {
		if (in_cyc[nf]) continue;
		ll bruh = dfs(nf, d+v);
		cnt++;
		ret2 = max(ret2, bruh);
		if (ret2 > ret) swap(ret2, ret);
	}
	res = max({res, cnt > 1 ? ret2 + ret - 2ll * d : 0ll, ret});
	return ret;
};

signed main() {
#ifdef cerr
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	//freopen("isl.in", "r", stdin);
	//freopen("isl.out", "w", stdout);
#endif
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> F[i][0] >> F[i][1];
		adj[--F[i][0]].push_back({i, F[i][1]});
	}

	ll ans = 0;
	for (int i = 0; i < N; i++) {
		if (seen[i]) continue;
		int cys = i;
		do {
			seen[cys] = true;
			cys = F[cys][0];
		} while (!seen[cys]);

		int cur = cys;
		do {
			in_cyc[cur] = true;
			cur = F[cur][0];
		} while (cur != cys);

		res = 0;

		vector<arl2> P;
		ll x = 0;
		for (bool _ : {0, 1}) {
			int ind = 0;
			do {
				P.push_back({x, _ ? P[ind++][1] : dfs(cur, 0ll)});
				x += F[cur][1];
				cur = F[cur][0];
			} while (cur != cys);
		}

		deque<pair<ll, int>> dq;
		for (int j = 0; j < P.size(); j++) {
			auto [p, v] = P[j];
			while (dq.size() && dq.front().second <= j-P.size()/2) {
				dq.pop_front();
			}
			res = max(res, (dq.size() ? dq.front().first + p : 0ll) + v);
			while (dq.size() && dq.back().first <= v-p) {
				dq.pop_back();
			}
			dq.push_back({v-p, j});
		}

		dq.clear();
		for (int j = P.size()-1; ~j; j--) {
			auto [p, v] = P[j];
			while (dq.size() && dq.front().second >= j+P.size()/2) {
				dq.pop_front();
			}
			res = max(res, (dq.size() ? dq.front().first - p : 0ll) + v);
			while (dq.size() && dq.back().first <= v+p) {
				dq.pop_back();
			}
			dq.push_back({v+p, j});
		}

		ans += res;
	}

	cout << ans << '\n';
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:72:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |   for (int j = 0; j < P.size(); j++) {
      |                   ~~^~~~~~~~~~
islands.cpp:74:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |    while (dq.size() && dq.front().second <= j-P.size()/2) {
      |                        ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
islands.cpp:87:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |    while (dq.size() && dq.front().second >= j+P.size()/2) {
      |                        ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 12 ms 23828 KB Output is correct
5 Correct 13 ms 23764 KB Output is correct
6 Correct 12 ms 23832 KB Output is correct
7 Correct 12 ms 23704 KB Output is correct
8 Correct 12 ms 23708 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23792 KB Output is correct
11 Correct 13 ms 23784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23844 KB Output is correct
2 Correct 12 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23840 KB Output is correct
2 Correct 13 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24604 KB Output is correct
2 Correct 22 ms 25756 KB Output is correct
3 Correct 18 ms 24788 KB Output is correct
4 Correct 16 ms 24288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 26964 KB Output is correct
2 Correct 35 ms 29716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 33224 KB Output is correct
2 Correct 64 ms 41616 KB Output is correct
3 Correct 75 ms 43000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 42672 KB Output is correct
2 Correct 121 ms 51156 KB Output is correct
3 Correct 151 ms 69992 KB Output is correct
4 Correct 164 ms 70476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 56040 KB Output is correct
2 Correct 423 ms 91316 KB Output is correct
3 Correct 178 ms 58616 KB Output is correct
4 Correct 234 ms 80740 KB Output is correct
5 Correct 225 ms 78792 KB Output is correct
6 Correct 551 ms 68384 KB Output is correct
7 Correct 253 ms 100052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 233 ms 131072 KB Output is correct
2 Correct 229 ms 110572 KB Output is correct
3 Correct 281 ms 128148 KB Output is correct
4 Correct 284 ms 79924 KB Output is correct
5 Correct 233 ms 79744 KB Output is correct
6 Correct 201 ms 72280 KB Output is correct
7 Correct 574 ms 69272 KB Output is correct