Submission #548423

# Submission time Handle Problem Language Result Execution time Memory
548423 2022-04-13T10:24:18 Z Temmie Islands (IOI08_islands) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>

#include <sl/debug.h>

struct Seg {
	
	std::vector <long long> v, w;
	int size;
	
	Seg(int s) {
		size = 1;
		while (size < s) {
			size <<= 1;
		}
		v.resize(size << 1, 0);
		w.resize(size << 1, 0);
	}
	
	void update(int l, int r, long long delta) {
		update(l, r, delta, 0, 0, size);
	}
	
	void update(int tl, int tr, long long delta, int now, int l, int r) {
		if (l >= tr || r <= tl) {
			return;
		}
		if (l >= tl && r <= tr) {
			w[now] += delta;
			return;
		}
		push(now, l, r);
		int mid = (l + r) >> 1;
		update(tl, tr, delta, now * 2 + 1, l, mid);
		update(tl, tr, delta, now * 2 + 2, mid, r);
	}
	
	long long query(int l, int r) {
		return query(l, r, 0, 0, size);
	}
	
	long long query(int tl, int tr, int now, int l, int r) {
		if (l >= tr || r <= tl) {
			return 0;
		}
		push(now, l, r);
		if (l >= tl && r <= tr) {
			return v[now];
		}
		int mid = (l + r) >> 1;
		return std::max(query(tl, tr, now * 2 + 1, l, mid), query(tl, tr, now * 2 + 2, mid, r));
	}
	
	void push(int node, int l, int r) {
		v[node] += w[node] * (r - l);
		if (r - l - 1) {
			w[node * 2 + 1] += w[node];
			w[node * 2 + 2] += w[node];
		}
		w[node] = 0;
	}
	
};

int n;
std::vector <std::vector <std::pair <int, std::pair <int, int>>>> g;
std::vector <std::vector <int>> c;
std::vector <int> cc;
std::vector <long long> cans;

void fc(int node, std::vector <bool>& vis) {
	if (vis[node]) {
		return;
	}
	vis[node] = true;
	c.back().push_back(node);
	for (auto p : g[node]) {
		fc(p.second.first, vis);
	}
}

std::vector <bool> cyc;

bool cy(int node, int par, std::vector <bool>& vis) {
	if (vis[node]) {
		return cyc[node] = true;
	}
	vis[node] = true;
	for (auto p : g[node]) {
		if (p.first != par && cy(p.second.first, p.first, vis)) {
			return cyc[node] ? false : (cyc[node] = true);
		}
	}
	return false;
}

long long td(int node, int par = -1) {
	std::vector <long long> ch;
	for (auto p : g[node]) {
		if (p.second.first == par || cyc[p.second.first]) {
			continue;
		}
		ch.push_back(td(p.second.first, node) + p.second.second);
	}
	std::sort(ch.rbegin(), ch.rend());
	if (ch.size()) {
		cans[cc[node]] =
		std::max(cans[cc[node]], (int) ch.size() > 1 ? ch[0] + ch[1] : ch.empty() ? 0LL : ch[0]);
	}
	return ch.size() ? ch[0] : 0LL;
}

std::vector <long long> sus;

void gse(int node, int par, std::vector <bool>& vis, std::vector <std::pair <long long, long long>>& seq) {
	for (auto p : g[node]) {
		if (p.second.first != par && cyc[p.second.first] && !vis[p.second.first]) {
			vis[p.second.first] = true;
			seq.emplace_back(sus[p.second.first], p.second.second);
			gse(p.second.first, node, vis, seq);
		}
	}
}

int main() {
	std::ios::sync_with_stdio(0); std::cin.tie(0);
	
	std::cin >> n;
	g.resize(n);
	for (int i = 0; i < n; i++) {
		int u, w; std::cin >> u >> w; u--;
		g[u].push_back({ i, { i, w } });
		g[i].push_back({ i, { u, w } });
	}
	std::vector <bool> vis(n, false);
	cc.resize(n, -1);
	for (int i = 0; i < n; i++) {
		if (vis[i]) {
			continue;
		}
		c.resize(c.size() + 1);
		cans.resize(cans.size() + 1, 0);
		fc(i, vis);
		for (int x : c.back()) {
			cc[x] = (int) c.size() - 1;
		}
	}
	cyc.resize(n, false);
	vis = std::vector <bool> (n, false);
	for (auto& v : c) {
		cy(v[0], -1, vis);
	}
	sus.resize(n, 0);
	for (int i = 0; i < n; i++) {
		if (cyc[i]) {
			sus[i] = td(i);
		}
	}
	vis = std::vector <bool> (n, false);
	for (int i = 0; i < n; i++) {
		if (!vis[i] && cyc[i]) {
			std::vector <std::pair <long long, long long>> seq;
			gse(i, -1, vis, seq);
			if ((int) seq.size() < 2) {
				seq.clear();
				for (int x : c[cc[i]]) {
					if (cyc[x]) {
						for (auto p : g[x]) {
							if (cyc[p.second.first]) {
								seq.emplace_back(sus[p.second.first], p.second.second);
							}
						}
					}
				}
				std::sort(seq.begin(), seq.end(), [] (
				std::pair <long long, long long> u, std::pair <long long, long long> v) {
					return u.second < v.second;
				});
				seq = { seq.front(), seq.back() };
			}
			int m = seq.size();
			//for (int k = 0; k < 2; k++) {
				//Seg seg(m);
				//long long sum = 0;
				//for (int j = 0; j < m; j++) {
					//seg.update(j, j + 1, seq[j].first + (sum += seq[j].second));
				//}
				//for (int j = 0; j < m; j++) {
					//seg.update(j + 1, m, -seq[j].second);
					//cans[cc[i]] = std::max(cans[cc[j]], seg.query(0, m));
					//seg.update(0, m, seq[0].second);
				//}
				//long long tmp = seq[0].second;
				//for (int j = 1; j < m; j++) {
					//seq[j - 1].second = seq[j].second;
				//}
				//seq[m - 1].second = tmp;
				//std::reverse(seq.begin(), seq.end());
			//}
			for (int j = 0; j < m; j++) {
				long long sum = seq[j].first;
				for (int k = 1; k < m; k++) {
					int ind = (j + k) % m;
					cans[cc[i]] = std::max(cans[cc[i]], (sum += seq[ind].second) + seq[ind].first);
				}
			}
		}
	}
	long long ans = 0;
	for (long long x : cans) {
		ans += x;
	}
	std::cout << ans << "\n";
	
}

Compilation message

islands.cpp:3:10: fatal error: sl/debug.h: No such file or directory
    3 | #include <sl/debug.h>
      |          ^~~~~~~~~~~~
compilation terminated.