結果

問題 No.416 旅行会社
ユーザー kapokapo
提出日時 2016-08-27 12:56:41
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,171 bytes
コンパイル時間 853 ms
コンパイル使用メモリ 99,536 KB
実行使用メモリ 21,764 KB
最終ジャッジ日時 2024-04-26 06:17:21
合計ジャッジ時間 8,437 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 284 ms
14,976 KB
testcase_01 AC 3 ms
8,064 KB
testcase_02 AC 3 ms
8,320 KB
testcase_03 AC 3 ms
8,192 KB
testcase_04 AC 4 ms
8,192 KB
testcase_05 AC 4 ms
8,108 KB
testcase_06 AC 3 ms
8,192 KB
testcase_07 AC 5 ms
8,320 KB
testcase_08 AC 9 ms
8,448 KB
testcase_09 AC 38 ms
9,064 KB
testcase_10 AC 308 ms
14,748 KB
testcase_11 AC 332 ms
19,104 KB
testcase_12 AC 330 ms
19,104 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
#include <stack>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

set<Pii> ab;
vector<Pii> cd;
int par[101010];
int cnt[101010];
int ans[101010];
set<int> vl[101010];

void init(int n)
{
	rep(i,0,n) {
		par[i] = i;
		cnt[i] = 0;
		vl[i].insert(i);
	}
}

int find(int x)
{
	if (par[x] == x) return x;
	return par[x] = find(par[x]);
}

void unite(int x, int y) 
{
	x = find(x);
	y = find(y);
	if (x == y) return;
	if (cnt[x] > cnt[y]) swap(x,y);
	if (x == 0) swap(x,y);    // 0 を基準
	par[x] = y;
	cnt[y] += cnt[x];
	vl[y].insert(all(vl[x])); // setで全頂点をマージ
	vl[x].clear();
}

int main()
{
	int n, m, q;
	cin >> n >> m >> q;
	
	rep(i,0,m) {
		int a,b; cin>>a>>b; a--; b--;
		ab.insert(mp(a,b));
	}
	rep(i,0,q) {
		int a,b; cin>>a>>b; a--; b--;
		cd.pb(mp(a,b));
		ab.erase(mp(a,b));
	}
	reverse(all(cd));

	init(n);
	for (auto e: ab) unite(e.first, e.second);
	for (auto v: vl[0]) ans[v] = -1; // 最初から繋がっている頂点 (橋が全て壊れても連結している島)

	rep(i,0,q) {
		int a = cd[i].first;
		int b = cd[i].second;
		//printf("i=%d a=%d find(a)=%d b=%d find(b)=%d\n",i,a,find(a),b,find(b));
		a = find(a); b = find(b);
		if (a == b) continue;
		if (b==0) swap(a,b);
		if (a==0 && b!=0) {
			for (auto v: vl[b]) if (ans[v] == 0) 
				ans[v] = q-i;
		}
		unite(a,b);
	}

	rep(i,1,n) cout << ans[i] << endl;

	return 0;
}
0