結果
| 問題 |
No.416 旅行会社
|
| ユーザー |
kapo
|
| 提出日時 | 2016-08-27 13:13:04 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 733 ms / 4,000 ms |
| コード長 | 2,171 bytes |
| コンパイル時間 | 1,094 ms |
| コンパイル使用メモリ | 97,896 KB |
| 実行使用メモリ | 20,292 KB |
| 最終ジャッジ日時 | 2024-12-14 20:03:48 |
| 合計ジャッジ時間 | 9,024 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
#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] = 1;
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;
}
kapo