跳到正文

剪贴板

剪贴板 d5qq79ha

原文
剪贴板信息

剪贴板信息

收录
更新

station

C++
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<typename A>
string to_string(A v) {
	string s = "{";
	for (auto x : v) {
		if ((int)s.size() > 1) s += ", ";
		s += to_string(x);
	}
	return s += "}";
}

void debug_out() { cerr << '\n'; }

template<typename T, typename... U>
void debug_out(T x, const U&... args) {
	cerr << ' ' << to_string(x);
	debug_out(args...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)  // Debugger

struct Line {
	int l, r;
};

int main() {
	freopen("station.in", "r", stdin);
	freopen("station.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m, x;
	cin >> n >> m >> x;
	vector<Line> a(m);
	for (int i = 0; i < m; ++i) cin >> a[i].l >> a[i].r;
	// cerr << "here\n";
	vector<int> s;
	{
		sort(a.begin(), a.end(), [&](Line u, Line v) {
			return u.r < v.r;
		});
		// cerr << "sorted\n";
		int mx = x, pos = 0;
		while (pos < m && a[pos].r <= x) ++pos;
		for (int i = pos; i < m; ++i) if (a[i].l <= mx) mx = max(mx, a[i].r);
		for (int i = pos; i < m; ++i) if (a[i].l <= mx) s.push_back(a[i].r);
	}
	// cerr << "here\n";
	{
		sort(a.begin(), a.end(), [&](Line u, Line v) {
			return u.l < v.l;
		});
		int mn = x, pos = m;
		while (pos > 0 && a[pos - 1].l >= x) --pos;
		for (int i = pos - 1; i >= 0; --i) if (a[i].r >= mn) mn = min(mn, a[i].l);
		for (int i = pos - 1; i >= 0; --i) if (a[i].r >= mn) s.push_back(a[i].l);
	}
	// cerr << "hey there\n";
	sort(s.begin(), s.end());
	s.resize(unique(s.begin(), s.end()) - s.begin());
	for (int x : s) cout << x << ' ';
	cout << '\n';
}

cities

C++
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<typename A>
string to_string(A v) {
	string s = "{";
	for (auto x : v) {
		if ((int)s.size() > 1) s += ", ";
		s += to_string(x);
	}
	return s += "}";
}

void debug_out() { cerr << '\n'; }

template<typename T, typename... U>
void debug_out(T x, const U&... args) {
	cerr << ' ' << to_string(x);
	debug_out(args...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)  // Debugger

int main() {
	freopen("cities.in", "r", stdin);
	freopen("cities.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m, k;
	cin >> n >> m >> k;
	vector<int> U(m), V(m);
	for (int i = 0; i < m; ++i) cin >> U[i] >> V[i], --U[i], --V[i];
	if (n <= 20) {
		int res = 0;
		for (int s = 0; s < (1 << n); ++s) {
			int t = __builtin_popcount(s);
			if (t < 2) continue;
			vector<int> te(n);
			iota(te.begin(), te.end(), 0);
			auto Find = [&](int u) {
				while (u != te[u]) u = te[u] = te[te[u]];
				return u;
			};
			for (int i = 0; i < m; ++i) if ((~s >> U[i] & 1) || (~s >> V[i] & 1)) te[Find(U[i])] = Find(V[i]);
			int cnt = 0;
			vector<int> sz(n, 0), rsz;
			for (int u = 0; u < n; ++u) sz[Find(u)] += 1;
			for (int u = 0; u < n; ++u) if (u == Find(u)) {
				cnt += 1;
				rsz.push_back(sz[u]);
			}
			if (cnt != t) continue;
			sort(rsz.begin(), rsz.end());
			if (rsz.back() - rsz[0] <= k) res += 1;
		}
		cout << res << '\n';
		return 0;
	}
	vector<vector<int>> g(n);
	for (int i = 0; i < m; ++i) {
		g[U[i]].push_back(V[i]);
		g[V[i]].push_back(U[i]);
	}
	vector<int> fa(n, -1);
	vector<int> sz(n, 1);
	function<void(int)> Dfs = [&](int u) {
		for (int v : g[u]) if (v != fa[u] && !~fa[v]) {
			fa[v] = u;
			Dfs(v);
			sz[u] += sz[v];
		}
	};
	fa[0] = 0;
	Dfs(0);
	int res = 0;
	for (int x = 1; x < n; ++x) if (n % x == 0) {
		int t = 0, root = -1;
		int ok = true;
		vector<int> use(n, false);
		for (int u = 1; u < n; ++u) if (sz[u] % x == 0) {
			t += 1;
			use[u] = true;
			if (fa[u] == 0 || sz[fa[u]] % x != 0) {
				if (!~root) root = fa[u];
				else if (fa[u] != root) {
					ok = false;
					break;
				}
			}
		}
		if (!ok) continue;
		if (!~root) root = 0;
		t += 1;
		use[root] = true;
		if (t != n / x) continue;
		// debug(x, use);
		vector<int> te(n);
		iota(te.begin(), te.end(), 0);
		auto Find = [&](int u) {
			while (u != te[u]) u = te[u] = te[te[u]];
			return u;
		};
		for (int i = 0; i < m; ++i) if (!use[U[i]] || !use[V[i]]) te[Find(U[i])] = Find(V[i]);
		int cnt = 0;
		vector<int> sz(n, 0), rsz;
		for (int u = 0; u < n; ++u) sz[Find(u)] += 1;
		for (int u = 0; u < n; ++u) if (u == Find(u)) {
			cnt += 1;
			rsz.push_back(sz[u]);
		}
		if (cnt != t) continue;
		sort(rsz.begin(), rsz.end());
		if (rsz.back() - rsz[0] <= k) res += 1;
	}
	cout << res << '\n';
}

transfer

C++
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<typename A>
string to_string(A v) {
	string s = "{";
	for (auto x : v) {
		if ((int)s.size() > 1) s += ", ";
		s += to_string(x);
	}
	return s += "}";
}

void debug_out() { cerr << '\n'; }

template<typename T, typename... U>
void debug_out(T x, const U&... args) {
	cerr << ' ' << to_string(x);
	debug_out(args...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)  // Debugger

struct Worker {
	int pos, w;
};

int main() {
	freopen("transfer.in", "r", stdin);
	freopen("transfer.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	int sid, n, k, tt;
	cin >> sid >> n >> k >> tt;
	vector<int> fa(n, -1);
	vector<vector<int>> g(n);
	for (int v = 1; v < n; ++v) {
		cin >> fa[v], --fa[v];
		g[fa[v]].push_back(v);
	}
	vector<int> sz(n, 1);
	for (int u = n - 1; u >= 0; --u) for (int v : g[u]) sz[u] += sz[v];
	vector<Worker> a(k);
	for (int i = 0; i < k; ++i) cin >> a[i].pos >> a[i].w, --a[i].pos;
	auto Calc = [&]() {
		vector<multiset<int>> s(n);
		for (auto p : a) if (~p.pos) s[p.pos].insert(p.w);
		for (int u = n - 1; u >= 0; --u) {
			for (int v : g[u]) {
				if (s[u].size() < s[v].size()) swap(s[u], s[v]);
				for (int x : s[v]) s[u].insert(x);
				multiset<int>().swap(s[v]);
			}
			while ((int)s[u].size() > sz[u]) s[u].erase(s[u].begin());
		}
		ll sum = 0;
		for (int x : s[0]) sum += x;
		return sum;
	};
	cout << Calc() << ' ';
	while (tt--) {
		int op;
		cin >> op;
		if (op == 1) {
			Worker u;
			cin >> u.pos >> u.w, --u.pos;
			a.push_back(u);
		}
		else {
			int u;
			cin >> u, --u;
			a[u].pos = -1;
		}
		cout << Calc() << ' ';
	}
	cout << '\n';
}