結果
| 問題 |
No.416 旅行会社
|
| ユーザー |
👑 |
| 提出日時 | 2024-01-25 10:12:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 639 ms / 4,000 ms |
| コード長 | 3,390 bytes |
| コンパイル時間 | 1,458 ms |
| コンパイル使用メモリ | 135,536 KB |
| 最終ジャッジ日時 | 2025-02-18 22:38:26 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
//yukicoder@cpp17
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <random>
#include <bitset>
#include <complex>
#include <utility>
#include <numeric>
#include <functional>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);
P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる
計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/
/*
function corner below
*/
/*
Function corner above
*/
/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
cin.tie(0);
ios::sync_with_stdio(false);
}
*/
struct UnionFind {
int n;
vector<int> par;
vector<int> size_;
UnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1){
for(int i = 0; n > i; i++)par[i] = i;
}
int root(int x){
if(par[x] == x)return x;
return par[x] = root(par[x]);
}
void unite(int a,int b){
int ra = root(a);
int rb = root(b);
if(ra==rb)return;
if(size(ra) > size(rb)) swap(ra,rb);
par[ra] = rb;
size_[rb] += size_[ra];
}
bool same(int a, int b){
return root(a) == root(b);
}
int size(int a){
return size_[root(a)];
}
void debug(){
for(int i = 0; n > i; i++){
cout << size_[root(i)] << " ";
}
cout << endl;
return;
}
};
vector<int> ans(100000, 0);
vector<int> Z[100000];
void dfs(int v, int val){
ans[v] = val;
for(int i = 0; Z[v].size() > i; i++){
if(ans[Z[v][i]] != val){
dfs(Z[v][i], val);
}
}
}
int main(){
int n,m,q;cin>>n>>m>>q;
vector<pair<int,int>> A(m);
for(int i = 0; m > i; i++){
cin>>A[i].first>>A[i].second;
A[i].first--;A[i].second--;
}
vector<pair<int,int>> Q(q);
set<pair<int,int>> bw;
for(int i = 0; q > i; i++){
cin>>Q[i].first>>Q[i].second;
Q[i].first--;Q[i].second--;
bw.insert(Q[i]);
}
UnionFind uf(n);
for(int i = 0; m > i; i++){
if(!bw.count(A[i])){
uf.unite(A[i].first, A[i].second);
Z[A[i].first].push_back(A[i].second);
Z[A[i].second].push_back(A[i].first);
}
}
dfs(0, -1);
for(int i = q-1; 0 <= i; i--){
if(uf.root(Q[i].first) != uf.root(Q[i].second)){
if(uf.root(Q[i].first) == uf.root(0)){
dfs(Q[i].second, i+1);
}else if(uf.root(Q[i].second) == uf.root(0)){
dfs(Q[i].first, i+1);
}
uf.unite(Q[i].first, Q[i].second);
Z[Q[i].first].push_back(Q[i].second);
Z[Q[i].second].push_back(Q[i].first);
}
}
for(int i = 1; n > i; i++)cout << ans[i] << endl;
return 0;
}