結果

問題 No.416 旅行会社
ユーザー antaanta
提出日時 2016-08-27 00:52:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,767 bytes
コンパイル時間 2,385 ms
コンパイル使用メモリ 191,720 KB
実行使用メモリ 814,336 KB
最終ジャッジ日時 2024-04-26 05:24:41
合計ジャッジ時間 4,912 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
8,832 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 MLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

struct UnionFindTraverse {
	vector<int> data;
	vector<int> head, next, prev;
	void init(int n) {
		data.assign(n, -1);
		head.assign(n, -1);
		next.assign(n, -1);
		prev.assign(n, -1);
	}
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if(x != y) {
			if(data[y] < data[x]) swap(x, y);
			data[x] += data[y];
			data[y] = x;
			if((next[y] = head[x]) != -1)
				prev[head[x]] = y;
			head[x] = y;
		}
		return x != y;
	}
	bool findSet(int x, int y) { return root(x) == root(y); }
	int root(int x) {
		int p = data[x];
		if(p < 0)
			return x;
		int r = root(p);
		if(r != p) {
			data[x] = r;
			int a = prev[x], b = next[x];
			if(a != -1)
				next[a] = b;
			if(b != -1)
				prev[b] = a;
			prev[x] = -1;
			next[x] = head[r];
			prev[head[r]] = x;
			head[r] = x;
		}
		return r;
	}
	int size(int x) { return -data[root(x)]; }

	template<typename Func>
	void traverse(int x, Func f) {
		x = root(x);
		traverseRec(x, f);
	}

	template<typename Func>
	void traverseRec(int x, Func f) const {
		f(x);
		int c = head[x];
		if(c != -1) {
			traverseRec(c, f);
			for(int y = next[c]; y != -1; y = next[y])
				traverseRec(y, f);
			for(int y = prev[c]; y != -1; y = prev[y])
				traverseRec(y, f);
		}
	}
};

int main() {
	int N; int M; int Q;
	while(~scanf("%d%d%d", &N, &M, &Q)) {
		set<pii> edges;
		rep(i, M) {
			int A; int B;
			scanf("%d%d", &A, &B), -- A, -- B;
			if(A > B)swap(A, B);
			edges.insert(make_pair(A, B));
		}
		vpii queries(Q);
		rep(i, Q) {
			int A; int B;
			scanf("%d%d", &A, &B), -- A, -- B;
			if(A > B)swap(A, B);
			edges.erase(make_pair(A, B));
			queries[i] = make_pair(A, B);
		}
		UnionFindTraverse uf;
		uf.init(N);
		for(auto e : edges)
			uf.unionSet(e.first, e.second);
		vector<int> ans(N, 0);
		rep(i, N) if(uf.findSet(i, 0))
			ans[i] = -1;
		for(int i = Q - 1; i >= 0; -- i) {
			auto e = queries[i];
			int x = e.first, y = e.second;
			x = uf.root(x), y = uf.root(y);
			if(x == y) continue;
			if(uf.findSet(0, y))
				swap(x, y);
			if(uf.findSet(0, x)) {
				uf.traverse(y, [&ans, i](int u) {
					ans[u] = i + 1;
				});
			}
			uf.unionSet(x, y);
		}
		for(int i = 1; i < N; ++ i)
			printf("%d\n", ans[i]);
	}
	return 0;
}
0