結果

問題 No.416 旅行会社
ユーザー naimonon77naimonon77
提出日時 2016-08-30 13:23:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,831 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 174,216 KB
実行使用メモリ 23,296 KB
最終ジャッジ日時 2024-04-26 17:59:20
合計ジャッジ時間 13,284 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 #

#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);

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);
			dump(itr->first);
			dump(itr->second);
		}
	}

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

	for (i = Q - 1; i > -1; --i) {
		uf.unite(broken[i].first, broken[i].second);
		rep(j, N) {
			if (ans[j] == 0 && uf.same(0,j)) {
				ans[j] = i;
			}
		}
	}

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