結果

問題 No.416 旅行会社
ユーザー naimonon77
提出日時 2016-10-30 13:27:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 2,051 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 188,620 KB
実行使用メモリ 16,256 KB
最終ジャッジ日時 2024-11-24 23:34:52
合計ジャッジ時間 8,631 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16 RE * 5
権限があれば一括ダウンロードができます

ソースコード

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)1e5 + 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