結果

問題 No.529 帰省ラッシュ
ユーザー kazumakazuma
提出日時 2017-06-25 15:02:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 780 ms / 4,500 ms
コード長 5,530 bytes
コンパイル時間 2,431 ms
コンパイル使用メモリ 198,468 KB
実行使用メモリ 37,556 KB
最終ジャッジ日時 2024-05-09 16:43:53
合計ジャッジ時間 10,649 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 9 ms
6,944 KB
testcase_05 AC 8 ms
6,940 KB
testcase_06 AC 8 ms
6,940 KB
testcase_07 AC 9 ms
6,944 KB
testcase_08 AC 510 ms
21,248 KB
testcase_09 AC 503 ms
19,912 KB
testcase_10 AC 534 ms
23,940 KB
testcase_11 AC 543 ms
24,232 KB
testcase_12 AC 438 ms
19,512 KB
testcase_13 AC 387 ms
37,556 KB
testcase_14 AC 497 ms
26,880 KB
testcase_15 AC 774 ms
26,324 KB
testcase_16 AC 780 ms
26,256 KB
testcase_17 AC 553 ms
34,968 KB
testcase_18 AC 537 ms
35,180 KB
testcase_19 AC 537 ms
32,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

struct Edge {
	int from, to;
	Edge(int f, int t) {
		from = f;
		to = t;
	}
};

using Edges = vector<Edge>;
using Graph = vector<Edges>;
pair<vector<int>, Edges> bridge(const Graph& g) {
	const int n = g.size();
	int idx = 0, s = 0, t = 0, k = 0;
	vector<int>ord(n, -1), onS(n), stk(n), roots(n), cmp(n);
	Edges brdg;
	function<void(int, int)> dfs = [&](int v, int u) {
		ord[v] = idx++;
		stk[s++] = v;
		onS[v] = true;
		roots[t++] = v;
		for (auto& e : g[v]) {
			int w = e.to;
			if (ord[w] == -1)dfs(w, v);
			else if (u != w && onS[w])
				while (ord[roots[t - 1]]>ord[w])--t;
		}
		if (v == roots[t - 1]) {
			brdg.emplace_back(u, v);
			while (true) {
				int w = stk[--s];
				onS[w] = false;
				cmp[w] = k;
				if (v == w)break;
			}
			--t;
			++k;
		}
	};
	for (int u = 0; u<n; ++u) {
		if (ord[u] == -1) {
			dfs(u, n);
			brdg.pop_back();
		}
	}
	return make_pair(cmp, brdg);
}

class HLDecomposition {
	vector<vector<int>> g;
	vector<int> vid, head, heavy, parent, depth, inv;
	int dfs(int curr, int prev) {
		parent[curr] = prev;
		int sub = 1, max_sub = 0;
		for (int next : g[curr]) if (next != prev) {
			depth[next] = depth[curr] + 1;
			int sub_next = dfs(next, curr);
			sub += sub_next;
			if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next;
		}
		return sub;
	}
	void bfs() {
		int k = 0;
		queue<int> q({ 0 });
		while (!q.empty()) {
			int h = q.front(); q.pop();
			for (int i = h; i != -1; i = heavy[i]) {
				vid[i] = k++;
				inv[vid[i]] = i;
				head[i] = h;
				for (int j : g[i]) if (j != parent[i] && j != heavy[i]) q.push(j);
			}
		}
	}
public:
	HLDecomposition(int n)
		: g(n), vid(n, -1), head(n), heavy(n, -1), parent(n), depth(n), inv(n) {}

	void add(int u, int v) {
		g[u].push_back(v);
		g[v].push_back(u);
	}
	void build() {
		dfs(0, -1);
		bfs();
	}
	void for_each(int u, int v, function<void(int, int)> f) {
		if (vid[u] > vid[v]) swap(u, v);
		f(max(vid[head[v]], vid[u]), vid[v]);
		if (head[u] != head[v]) for_each(u, parent[head[v]], f);
	}
	void for_each_directed(int u, int v, function<void(int, int, int)> f) {
		if (vid[u] > vid[v]) {
			f(max(vid[head[u]], vid[v]), vid[u], 1);
			if (head[u] != head[v]) for_each_directed(parent[head[u]], v, f);
		}
		else {
			f(max(vid[head[v]], vid[u]), vid[v], 0);
			if (head[u] != head[v]) for_each_directed(u, parent[head[v]], f);
		}
	}
	void for_each_edge(int u, int v, function<void(int, int)> f) {
		if (vid[u] > vid[v]) swap(u, v);
		if (head[u] != head[v]) {
			f(vid[head[v]], vid[v]);
			for_each_edge(u, parent[head[v]], f);
		}
		else {
			if (u != v) f(vid[u] + 1, vid[v]);
		}
	}
	int ancestor(int u, int d) {
		while (true) {
			if (depth[head[u]] > depth[u] - d) {
				d -= depth[u] - depth[head[u]] + 1;
				if (head[u] == 0) return 0;
				u = parent[head[u]];
			}
			else {
				return inv[vid[u] - d];
			}
		}
	}
	int lca(int u, int v) {
		if (vid[u] > vid[v]) swap(u, v);
		if (head[u] == head[v]) return u;
		return lca(u, parent[head[v]]);
	}
	int distance(int u, int v) {
		return depth[u] + depth[v] - 2 * depth[lca(u, v)];
	}
};

template <typename T>
class SegmentTree {
	using func_t = function<T(T, T)>;
	const int n;
	const T id;
	func_t merge;
	vector<T> data;
	int size(int n) {
		int res;
		for (res = 1; res < n; res <<= 1);
		return res;
	}
	T sub(int l, int r, int node, int lb, int ub) {
		if (ub <= l || r <= lb) return id;
		if (l <= lb && ub <= r) return data[node];
		return merge(sub(l, r, node * 2, lb, (lb + ub) / 2), sub(l, r, node * 2 + 1, (lb + ub) / 2, ub));
	}
public:
	SegmentTree(int n_, T id_, func_t merge_) :
		n(size(n_)), id(id_), merge(merge_), data(size(n_) * 2, id_) {}
	void Init(const vector<T>& data_) {
		for (int i = 0; i < (int)data_.size(); i++)
			data[i + n] = data_[i];
		for (int i = n - 1; i >= 0; i--)
			data[i] = merge(data[i * 2], data[i * 2 + 1]);
	}
	void Update(int p, T val) {
		p += n;
		data[p] = val;
		while (p >>= 1) data[p] = merge(data[p * 2], data[p * 2 + 1]);
	}
	void Add(int p, T val) {
		p += n;
		data[p] += val;
		while (p >>= 1) data[p] = merge(data[p * 2], data[p * 2 + 1]);
	}
	T Find(int i) {
		return data[i + n];
	}
	T Find(int l, int r) {
		return sub(l, r + 1, 1, 0, n);
	}
};

const function<P(P, P)> merg = [](P l, P r) { return l.first >= r.first ? l : r; };

int main()
{
	int N, M, Q, com, X, Y;
	cin >> N >> M >> Q;
	Graph G(N);
	vector<int> A(M), B(M);
	for (int i = 0; i < M; i++) {
		cin >> A[i] >> B[i]; A[i]--; B[i]--;
		G[A[i]].push_back(Edge(A[i], B[i]));
		G[B[i]].push_back(Edge(B[i], A[i]));
	}
	auto bri = bridge(G);
	int V = 0;
	for (int i = 0; i < N; i++) {
		V = max(V, bri.first[i]);
	}
	V++;
	HLDecomposition hl(V);
	for (auto e : bri.second) {
		hl.add(bri.first[e.from], bri.first[e.to]);
	}
	hl.build();
	SegmentTree<P> st(V, P(), merg);
	vector<priority_queue<int>> W(V);
	while (Q--) {
		cin >> com >> X >> Y;
		if (com == 1) {
			X--;
			X = bri.first[X];
			W[X].push(Y);
			hl.for_each(X, X, [&](int l, int r) {
				st.Update(l, P(W[X].top(), X));
			});
		}
		else {
			X--; Y--;
			X = bri.first[X]; Y = bri.first[Y];
			P res;
			hl.for_each(X, Y, [&](int l, int r) {
				res = merg(res, st.Find(l, r));
			});
			if (res == P()) {
				printf("%d\n", -1);
				continue;
			}
			printf("%d\n", res.first);
			W[res.second].pop();
			P val = W[res.second].empty() ? P() : P(W[res.second].top(), res.second);
			hl.for_each(res.second, res.second, [&](int l, int r) {
				st.Update(l, val);
			});
		}
	}
	return 0;
}
0