結果

問題 No.416 旅行会社
ユーザー naimonon77naimonon77
提出日時 2016-08-30 15:33:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 595 ms / 4,000 ms
コード長 2,272 bytes
コンパイル時間 1,905 ms
コンパイル使用メモリ 185,248 KB
実行使用メモリ 20,104 KB
最終ジャッジ日時 2023-08-21 10:01:13
合計ジャッジ時間 9,275 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 272 ms
14,736 KB
testcase_01 AC 4 ms
8,560 KB
testcase_02 AC 4 ms
8,244 KB
testcase_03 AC 5 ms
8,316 KB
testcase_04 AC 4 ms
8,348 KB
testcase_05 AC 5 ms
8,300 KB
testcase_06 AC 4 ms
8,548 KB
testcase_07 AC 6 ms
8,308 KB
testcase_08 AC 11 ms
8,988 KB
testcase_09 AC 36 ms
9,328 KB
testcase_10 AC 303 ms
14,816 KB
testcase_11 AC 310 ms
14,744 KB
testcase_12 AC 307 ms
14,588 KB
testcase_13 AC 276 ms
14,540 KB
testcase_14 AC 594 ms
19,880 KB
testcase_15 AC 592 ms
20,104 KB
testcase_16 AC 582 ms
19,760 KB
testcase_17 AC 594 ms
19,868 KB
testcase_18 AC 595 ms
19,856 KB
testcase_19 AC 411 ms
17,304 KB
testcase_20 AC 413 ms
17,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(),(a).end()
#define ifnot(a) if(not a)
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
bool test = 0;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX (int)2e5 + 5

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unite(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 same(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 N, M, Q;
set<pair<int, int>> alive;
pair<int, int> broken[MAX];
int ans[MAX];
UnionFind uf(MAX);
vector<vector<int>> group_num2item(MAX);

int main() {
	int i, j, k;
	cin >> N >> M >> Q;



	rep(i, M) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		alive.insert({ a,b });
	}

	rep(i, Q) {
		cin >> broken[i].first >> broken[i].second;
		broken[i].first--; broken[i].second--;
		alive.erase({ broken[i] });
	}

	{
		auto itr = alive.begin();
		for (; itr != alive.end(); ++itr) {
			uf.unite(itr->first, itr->second);
		}
	}
	rep(i, N) {
		group_num2item[uf.root(i)].push_back(i);
	}

	rep(i, N) {
		if (uf.same(0, i)) {
			ans[i] = -1;
		}
	}

	// O(NQ) -> 
	for (i = Q - 1; i > -1; --i) {
		int x = broken[i].first;
		x = uf.root(x);
		int y = broken[i].second;
		y = uf.root(y);

		if (uf.same(x, y))
			continue;
		// x,yは別グループ
		if (uf.same(0, y)) {
			swap(x, y);
		}
		// yは0と同じグループじゃない	
		if (uf.same(0, x)) {
			for (auto j : group_num2item[y]) {
				ans[j] = i + 1;
			}
		}

		if (uf.size(x) < uf.size(y)) {
			swap(x, y);
		}
		// uf.size(x) >= uf.size(y)
		group_num2item[x].insert(group_num2item[x].end(),
			ALL(group_num2item[y]));
		group_num2item[y].clear();

		uf.unite(x, y);
	}

	for (i = 1; i < N; ++i) {
		cout << ans[i] << endl;
	}
	return 0;
}
0