結果

問題 No.416 旅行会社
ユーザー HAHAHAHAHAHA
提出日時 2016-09-01 11:59:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 337 ms / 4,000 ms
コード長 1,727 bytes
コンパイル時間 3,869 ms
コンパイル使用メモリ 164,964 KB
実行使用メモリ 14,656 KB
最終ジャッジ日時 2023-08-21 10:02:52
合計ジャッジ時間 6,268 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
10,832 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 13 ms
4,384 KB
testcase_10 AC 108 ms
11,008 KB
testcase_11 AC 107 ms
10,980 KB
testcase_12 AC 111 ms
10,888 KB
testcase_13 AC 88 ms
10,828 KB
testcase_14 AC 329 ms
13,812 KB
testcase_15 AC 323 ms
13,944 KB
testcase_16 AC 325 ms
14,108 KB
testcase_17 AC 331 ms
13,804 KB
testcase_18 AC 337 ms
13,908 KB
testcase_19 AC 208 ms
14,656 KB
testcase_20 AC 213 ms
14,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;



// not...
struct UnionFind {
	vector<int> data;
	void init(int n) { data.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;
		}
		return x != y;
	}
	bool findSet(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
};

int main() {
	int N; int M; int Q;
	while(~scanf("%d%d%d", &N, &M, &Q)) {
		set<pair<int ,int> > edges;
		for(int i=0;i<M;i++){
			int A; int B;
			scanf("%d%d", &A, &B), -- A, -- B;
			if(A > B)swap(A, B);
			edges.insert(make_pair(A, B));
		}
		vector<pair<int, int> > queries(Q);
		for(int i=0;i<Q;i++){
			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);
		}
		UnionFind uf;
		uf.init(N);
		for(auto e : edges)
			uf.unionSet(e.first, e.second);
		vector<int> ans(N, 0);
		for(int i=0;i<N;i++) if(uf.findSet(i, 0))
			ans[i] = -1;
		vector<vector<int>> as(N);
		for(int i=0;i<N;i++)
			as[uf.root(i)].push_back(i);
		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);
			vector<int> &v = as[x], &w = as[y];
			if(uf.findSet(0, x)) {
				for(int j : w)
					ans[j] = i + 1;
			}
			if(v.size() < w.size())
				v.swap(w);
			v.insert(v.end(), w.begin(), w.end());
			w.clear();
			uf.unionSet(x, y);
			as[uf.root(x)].swap(v);
		}
		for(int i = 1; i < N; ++ i)
			printf("%d\n", ans[i]);
	}
	return 0;
}
0