結果

問題 No.416 旅行会社
ユーザー naimonon77naimonon77
提出日時 2016-08-30 15:30:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 645 ms / 4,000 ms
コード長 2,406 bytes
コンパイル時間 3,503 ms
コンパイル使用メモリ 184,208 KB
実行使用メモリ 45,916 KB
最終ジャッジ日時 2023-08-21 10:02:43
合計ジャッジ時間 11,017 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 285 ms
40,576 KB
testcase_01 AC 16 ms
36,420 KB
testcase_02 AC 16 ms
34,192 KB
testcase_03 AC 15 ms
36,604 KB
testcase_04 AC 15 ms
36,184 KB
testcase_05 AC 15 ms
33,976 KB
testcase_06 AC 16 ms
36,508 KB
testcase_07 AC 16 ms
36,504 KB
testcase_08 AC 21 ms
36,620 KB
testcase_09 AC 47 ms
35,100 KB
testcase_10 AC 318 ms
43,488 KB
testcase_11 AC 315 ms
40,400 KB
testcase_12 AC 313 ms
43,468 KB
testcase_13 AC 284 ms
40,524 KB
testcase_14 AC 631 ms
45,716 KB
testcase_15 AC 645 ms
45,560 KB
testcase_16 AC 630 ms
45,916 KB
testcase_17 AC 619 ms
45,492 KB
testcase_18 AC 608 ms
45,784 KB
testcase_19 AC 429 ms
43,232 KB
testcase_20 AC 434 ms
43,372 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)1e6 + 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<int> item2group_num(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) {
		item2group_num[i] = uf.root(i);
		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)
		for (auto j : group_num2item[y]) {
			item2group_num[j] = x;
		}
		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