Submission #796481

# Submission time Handle Problem Language Result Execution time Memory
796481 2023-07-28T12:25:30 Z rxlfd314 Islands (IOI08_islands) C++17
100 / 100
573 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<int> 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 (int nf : adj[f]) {
		if (in_cyc[nf]) continue;
		ll bruh = dfs(nf, d+F[nf][1]);
		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);
	}

	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 cyc_len = 0;
		do {
			P.push_back({cyc_len, dfs(cur, 0ll)});
			cyc_len += F[cur][1];
			cur = F[cur][0];
		} while (cur != cys);

		deque<int> dq;
		for (int j = 0; j < P.size()<<1; j++) {
			auto [p, v] = P[j%P.size()];
			p += j >= P.size() ? cyc_len : 0ll;
			while (dq.size() && dq.front() <= j-P.size()) {
				dq.pop_front();
			}
			if (!dq.size() && j >= P.size() || dq.size() && dq.front() >= P.size()) break;	
			res = max(res, (dq.size() ? P[dq.front()][1] - P[dq.front()][0] + p : 0ll) + v);
			while (dq.size() && P[dq.back()][1] - P[dq.back()][0] <= v-p) {
				dq.pop_back();
			}
			if (j < P.size()) {
				dq.push_back(j);
			}
		}

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

		ans += res;
	}

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

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:69: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]
   69 |   for (int j = 0; j < P.size()<<1; j++) {
      |                   ~~^~~~~~~~~~~~~
islands.cpp:71:11: 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]
   71 |    p += j >= P.size() ? cyc_len : 0ll;
      |         ~~^~~~~~~~~~~
islands.cpp:72:35: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |    while (dq.size() && dq.front() <= j-P.size()) {
      |                        ~~~~~~~~~~~^~~~~~~~~~~~~
islands.cpp:75:24: 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]
   75 |    if (!dq.size() && j >= P.size() || dq.size() && dq.front() >= P.size()) break;
      |                      ~~^~~~~~~~~~~
islands.cpp:75:63: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |    if (!dq.size() && j >= P.size() || dq.size() && dq.front() >= P.size()) break;
      |                                                    ~~~~~~~~~~~^~~~~~~~~~~
islands.cpp:75:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   75 |    if (!dq.size() && j >= P.size() || dq.size() && dq.front() >= P.size()) break;
      |        ~~~~~~~~~~~^~~~~~~~~~~~~~~~
islands.cpp:80:10: 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]
   80 |    if (j < P.size()) {
      |        ~~^~~~~~~~~~
islands.cpp:88:11: 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]
   88 |    p += j >= P.size() ? cyc_len : 0ll;
      |         ~~^~~~~~~~~~~
islands.cpp:89:35: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |    while (dq.size() && dq.front() >= j+P.size()) {
      |                        ~~~~~~~~~~~^~~~~~~~~~~~~
islands.cpp:92:24: 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]
   92 |    if (!dq.size() && j < P.size() || dq.size() && dq.front() < P.size()) break;
      |                      ~~^~~~~~~~~~
islands.cpp:92:62: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |    if (!dq.size() && j < P.size() || dq.size() && dq.front() < P.size()) break;
      |                                                   ~~~~~~~~~~~^~~~~~~~~~
islands.cpp:92:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   92 |    if (!dq.size() && j < P.size() || dq.size() && dq.front() < P.size()) break;
      |        ~~~~~~~~~~~^~~~~~~~~~~~~~~
islands.cpp:97:10: 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]
   97 |    if (j >= P.size()) {
      |        ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23748 KB Output is correct
3 Correct 13 ms 23828 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 13 ms 23820 KB Output is correct
7 Correct 12 ms 23752 KB Output is correct
8 Correct 13 ms 23816 KB Output is correct
9 Correct 12 ms 23824 KB Output is correct
10 Correct 12 ms 23708 KB Output is correct
11 Correct 12 ms 23780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23812 KB Output is correct
2 Correct 12 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23960 KB Output is correct
2 Correct 13 ms 24052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24532 KB Output is correct
2 Correct 22 ms 25428 KB Output is correct
3 Correct 17 ms 24660 KB Output is correct
4 Correct 14 ms 24276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 26388 KB Output is correct
2 Correct 34 ms 28812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 32276 KB Output is correct
2 Correct 59 ms 38332 KB Output is correct
3 Correct 72 ms 38808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 96 ms 41912 KB Output is correct
2 Correct 113 ms 46316 KB Output is correct
3 Correct 134 ms 61700 KB Output is correct
4 Correct 157 ms 59960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 52168 KB Output is correct
2 Correct 387 ms 73836 KB Output is correct
3 Correct 178 ms 57716 KB Output is correct
4 Correct 217 ms 71424 KB Output is correct
5 Correct 214 ms 69164 KB Output is correct
6 Correct 573 ms 67104 KB Output is correct
7 Correct 245 ms 83560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 242 ms 131072 KB Output is correct
2 Correct 226 ms 110632 KB Output is correct
3 Correct 257 ms 99944 KB Output is correct
4 Correct 257 ms 79748 KB Output is correct
5 Correct 217 ms 71100 KB Output is correct
6 Correct 208 ms 68672 KB Output is correct
7 Correct 568 ms 67920 KB Output is correct