Submission #331040

# Submission time Handle Problem Language Result Execution time Memory
331040 2020-11-27T05:44:41 Z pavement Islands (IOI08_islands) C++17
9 / 100
324 ms 131076 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#ifdef TEST
#define getchar_unlocked _getchar_nolock
#endif
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tree<pair<int, int>, null_type, less<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<pair<int, int>, null_type, less<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int N, ans, st, ed, rn, link[1000005], le[1000005], sz[1000005], ign[1000005], lpath[1000005], ldpath[1000005], pref[1000005];
bool oncyc[1000005], vis[1000005];
vector<int> comp, path[1000005], wt[1000005];
vector<iii> adj[1000005];

int find(int x) {
	if (x == link[x]) return x;
	return link[x] = find(link[x]);
}

void unite(int a, int b) {
	a = find(a);
	b = find(b);
	if (a == b) return;
	if (sz[b] > sz[a]) swap(a, b);
	sz[a] += sz[b];
	link[b] = a;
}

void dfs(int n, int e = -1) {
	vis[n] = 1;
	for (auto u : adj[n])
		if (g2(u) != e) {
			if (vis[g0(u)]) {
				st = g0(u);
				ed = n;
				ign[find(n)] = g2(u);
				le[find(n)] = g1(u);
				return;
			}
			dfs(g0(u), n);
		}
}

bool find_path(int n) {
	if (n == ed) {
		path[find(n)].pb(n);
		oncyc[n] = 1;
		return 1;
	}
	for (auto u : adj[n])
		if (g2(u) != ign[n] && find_path(g0(u))) {
			path[find(n)].pb(n);
			wt[find(n)].pb(g1(u));
			oncyc[n] = 1;
			return 1;
		}
	return 0;
}

void find_diam(int n, int e = -1, int d = 0) {
	if (d > ed) {
		ed = d;
		st = n;
	}
	for (auto u : adj[n])
		if ((!oncyc[g0(u)] || g0(u) == rn) && g0(u) != e)
			find_diam(g0(u), n, d + g1(u));
}

main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> N;
	for (int i = 1; i <= N; i++) {
		link[i] = i;
		sz[i] = 1;
	}
	for (int i = 1, U, L; i <= N; i++) {
		cin >> U >> L;
		adj[i].eb(U, L, i);
		adj[U].eb(i, L, i);
		unite(i, U);
	}
	for (int i = 1; i <= N; i++)
		if (find(i) == i) comp.pb(i);
	for (int i : comp) {
		dfs(i);
		find_path(st);
	}
	for (int i = 1; i <= N; i++) {
		if (oncyc[i]) {
			rn = i;
			ed = st = 0;
			find_diam(i);
			ldpath[i] = ed;
			ed = 0;
			find_diam(st);
			lpath[find(i)] = max(lpath[find(i)], ed);
		}
	}
	for (int i : comp) {
		pref[0] = 0;
		int total = le[i];
		for (int j = 0; j < wt[i].size(); j++)
			pref[j + 1] = pref[j] + wt[i][j], total += wt[i][j];
		for (int j = 1, fs = ldpath[path[i][0]] - pref[0], snd = ldpath[path[i][0]] + pref[0]; j < path[i].size(); j++) {
			int curr = path[i][j];
			lpath[i] = max(lpath[i], fs + ldpath[curr] + pref[j]);
			lpath[i] = max(lpath[i], snd + ldpath[curr] - pref[j] + total);
			fs = max(fs, ldpath[curr] - pref[j]);
			snd = max(snd, ldpath[curr] + pref[j]);
		}
		ans += lpath[i];
	}
	cout << ans << '\n';
}

Compilation message

islands.cpp:89:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   89 | main() {
      |      ^
islands.cpp: In function 'int main()':
islands.cpp:123:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |   for (int j = 0; j < wt[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~
islands.cpp:125:92: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |   for (int j = 1, fs = ldpath[path[i][0]] - pref[0], snd = ldpath[path[i][0]] + pref[0]; j < path[i].size(); j++) {
      |                                                                                          ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 54 ms 70892 KB Output isn't correct
2 Runtime error 89 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Incorrect 48 ms 71020 KB Output isn't correct
4 Correct 48 ms 70892 KB Output is correct
5 Incorrect 49 ms 70892 KB Output isn't correct
6 Incorrect 48 ms 70892 KB Output isn't correct
7 Incorrect 48 ms 70892 KB Output isn't correct
8 Runtime error 83 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Incorrect 49 ms 70892 KB Output isn't correct
10 Correct 49 ms 70892 KB Output is correct
11 Correct 55 ms 70892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 86 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 89 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 94 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 117 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 163 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 219 ms 111084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 324 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 309 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -