結果

問題 No.416 旅行会社
ユーザー tossy
提出日時 2016-08-26 23:12:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 430 ms / 4,000 ms
コード長 2,092 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 104,924 KB
実行使用メモリ 18,972 KB
最終ジャッジ日時 2024-12-14 19:49:40
合計ジャッジ時間 6,278 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

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

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define fi first
#define se second
#define INF 2147483600
class UnionFind {
public:
vector<int> par, rank, sizes; // parent, depth, size(only for root)
UnionFind(int sz) : par(sz), rank(sz, 0), sizes(sz, 1){
rep(i,sz) par[i]=i; // initial tree is independent each other
}
int find(int x){
if(par[x]==x) return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y){
x=find(x);
y=find(y);
if(x==y) return; // already belong to same tree
if(rank[x] < rank[y]){ // y becomes parent node
par[x] = y;
sizes[y] += sizes[x];
} else { // x becomes parent node
par[y]=x;
if(rank[x]==rank[y]) rank[x]++;
sizes[x] += sizes[y];
}
}
bool same(int x, int y){
return find(x) == find(y);
}
int size(int x){
return sizes[find(x)];
}
}; // END class UnionFind
typedef pair<int, int> P;
int main(){
int n,m,q;
cin>>n>>m>>q;
set<P> brg;
rep(i,m){
int a,b;
scanf("%d %d", &a, &b);
a--;b--;
brg.insert(mp(a,b));
}
vector<int> a(q), b(q);
rep(i,q){
scanf("%d %d", &a[i], &b[i]);
a[i]--;b[i]--;
brg.erase(mp(a[i],b[i]));
}
vector<int> res(n);
vector<int> l(n,0), r(n,q+1);
rep(reps, 18){
UnionFind uf(n);
for(P p : brg){ uf.unite(p.fi, p.se); }
vector<vector<int> > s(m+1);
rep(i,n-1) s[(l[i]+r[i])/2].pb(i);
rep(i,q+1){
if(i!=0) uf.unite(a[q-i], b[q-i]);
for(int j : s[i]){
if(uf.same(0,j+1)) r[j]=i;
else l[j]=i;
}
}
}
rep(i,n-1){ if(r[i] == 0) r[i]=q+2;}
rep(i,n-1) printf("%d\n", q-r[i]+1);
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0