結果

問題 No.416 旅行会社
ユーザー naimonon77naimonon77
提出日時 2016-10-30 13:28:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 580 ms / 4,000 ms
コード長 2,051 bytes
コンパイル時間 2,648 ms
コンパイル使用メモリ 185,964 KB
実行使用メモリ 20,012 KB
最終ジャッジ日時 2023-08-21 10:08:10
合計ジャッジ時間 9,662 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 281 ms
14,744 KB
testcase_01 AC 7 ms
8,760 KB
testcase_02 AC 6 ms
8,644 KB
testcase_03 AC 6 ms
8,764 KB
testcase_04 AC 6 ms
8,732 KB
testcase_05 AC 7 ms
8,796 KB
testcase_06 AC 6 ms
8,596 KB
testcase_07 AC 9 ms
8,792 KB
testcase_08 AC 14 ms
8,964 KB
testcase_09 AC 39 ms
9,400 KB
testcase_10 AC 304 ms
14,608 KB
testcase_11 AC 297 ms
14,744 KB
testcase_12 AC 296 ms
14,636 KB
testcase_13 AC 275 ms
14,724 KB
testcase_14 AC 573 ms
20,012 KB
testcase_15 AC 572 ms
19,940 KB
testcase_16 AC 575 ms
19,832 KB
testcase_17 AC 580 ms
19,984 KB
testcase_18 AC 567 ms
19,952 KB
testcase_19 AC 413 ms
17,644 KB
testcase_20 AC 418 ms
17,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

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

// #define int ll
#ifdef _MSC_VER
const bool test = true;
#else 
const bool test = false;
#endif

int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
const int 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>> bridge;
pair<int, int> broken[MAX];
int ans[MAX];
UnionFind uf(MAX);
vector<vector<int>> g2i(MAX);

int main() {
	int i, j, k;
	cin >> N >> M >> Q;
	rep(i, M) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		bridge.insert({ a,b });
	}
	rep(i, Q) {
		int c, d;
		cin >> c >> d;
		c--; d--;
		broken[i] = { c,d };
	}

	for (auto c : broken) {
		bridge.erase(c);
	}

	auto alive = bridge;

	for (auto c : alive) {
		uf.unite(c.first, c.second);
	}

	rep(i, N) {
		g2i[uf.root(i)].push_back(i);
	}

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

	for (i = Q - 1; i > -1; i--) {
		int x = uf.root(broken[i].first);
		int y = uf.root(broken[i].second);
		if (uf.same(x, y)) continue;
		if (uf.same(0, y)) swap(x, y);
		if (uf.same(0, x)) {
			for (auto v : g2i[y]) {
				ans[v] = i + 1; // ?
			}
		}

		if (uf.size(x) < uf.size(y)) swap(x, y);
		g2i[x].insert(g2i[x].end(), ALL(g2i[y]));
		uf.unite(x, y);
	}

	REP(i, 1, N) {
		cout << ans[i] << endl;
	}
	return 0;
}
0