結果

問題 No.416 旅行会社
ユーザー waidottowaidotto
提出日時 2016-08-27 00:03:16
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,525 bytes
コンパイル時間 1,629 ms
コンパイル使用メモリ 173,304 KB
実行使用メモリ 15,852 KB
最終ジャッジ日時 2024-04-26 04:12:11
合計ジャッジ時間 12,541 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
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>

class UnionFindTree {
	public:
		UnionFindTree(int n) {
			rank = std::vector<int>(n, 0);
			for(int i = 0; i < n; ++i) {
				parent.push_back(i);
			}
		}
		int find(int x) {
			if(parent[x] == x) {
				return x;
			} else {
				return parent[x] = find(parent[x]);
			}
		}
		void unite(int x, int y) {
			x = find(x);
			y = find(y);
			if(x == y) return;
			if(rank[x] < rank[y]) {
				parent[x] = y;
			} else {
				parent[y] = x;
				if(rank[x] == rank[y]) ++rank[x];
			}
		}
		bool same(int x, int y) {
			return find(x) == find(y);
		}
	private:
		std::vector<int> parent;
		std::vector<int> rank;
};

int main(void) {
	int N, M, Q;
	std::cin >> N >> M >> Q;
	UnionFindTree tree(N + 1);
	std::set<std::pair<int, int>> AB;
	std::vector<std::pair<int, int>> CD;
	std::vector<int> answer(N + 1, 0);
	for(int i = 0; i < M; ++i) {
		int A, B;
		std::cin >> A >> B;
		AB.insert(std::make_pair(A, B));
	}
	for(int i = 0; i < Q; ++i) {
		int C, D;
		std::cin >> C >> D;
		CD.push_back(std::make_pair(C, D));
		AB.erase(std::make_pair(C, D));
	}
	for(auto it = AB.begin(); it != AB.end(); ++it) {
		tree.unite(it->first, it->second);
	}
	for(int j = 2; j <= N; ++j) {
		if(tree.same(1, j)) {
			answer[j] = -1;
		}
	}
	for(int i = Q; 1 <= i; --i) {
		tree.unite(CD[i - 1].first, CD[i - 1].second);
		for(int j = 2; j <= N; ++j) {
			if(tree.same(1, j)) {
				if(answer[j] == 0) {
					answer[j] = i;
				}
			}
		}
	}
	for(int i = 2; i <= N; ++i) {
		std::cout << answer[i] << '\n';
	}
	return 0;
}

0