結果

問題 No.416 旅行会社
ユーザー OUDON
提出日時 2016-12-13 02:52:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 545 ms / 4,000 ms
コード長 2,485 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 189,128 KB
実行使用メモリ 34,360 KB
最終ジャッジ日時 2024-12-14 20:21:26
合計ジャッジ時間 8,903 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};
class UnionFind {
public:
vector<int> par, union_size;
vector<set<int>> group; // group[i] := i
UnionFind(int n) : par(n), union_size(n, 1), group(n)
{
for (int i=0; i<n; i++) {
par[i] = i;
group[i].insert(i);
}
}
// xy
void merge(int x, int y)
{
x = find(x), y = find(y);
if (x == y) return;
if (union_size[x] < union_size[y]) swap(x, y);
union_size[x] += union_size[y];
union_size[y] = union_size[x];
par[y] = x;
group[x].insert(group[y].begin(), group[y].end());
group[y] = set<int>();
}
set<int>& get_same_group(int x) { return group[find(x)]; }
int find(int x) { return (par[x] == x ? x : par[x] = find(par[x])); }
bool is_same(int x, int y) { return find(x) == find(y); }
};
int main()
{
ios::sync_with_stdio(false);
int N, M, Q;
cin >> N >> M >> Q;
UnionFind UF(N);
set<PII> AS, BS;
vector<PII> B;
rep(i, M + Q) {
int U, V;
cin >> U >> V;
U--, V--;
(i < M ? AS : BS).emplace(U, V);
if (i >= M) B.emplace_back(U, V);
}
for (auto e : AS) {
if (!BS.count(e)) UF.merge(e.first, e.second);
}
vector<int> ans(N);
REP(i, 1, N) {
ans[i] = (UF.is_same(0, i) ? -1 : 0);
}
UF.get_same_group(0) = set<int>();
reverse(B.begin(), B.end());
rep(i, Q) {
int s = B[i].first, t = B[i].second;
if (UF.is_same(s, t)) continue;
set<int> g;
if (UF.is_same(0, s)) g.swap(UF.get_same_group(t));
if (UF.is_same(0, t)) g.swap(UF.get_same_group(s));
UF.merge(s, t);
for (auto u : g) {
assert(ans[u] == 0);
ans[u] = Q - i;
}
}
REP(i, 1, N) cout << ans[i] << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0