結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
tempura_pp
|
| 提出日時 | 2019-06-29 15:11:34 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,499 ms / 4,000 ms |
| コード長 | 2,000 bytes |
| コンパイル時間 | 1,613 ms |
| コンパイル使用メモリ | 115,776 KB |
| 実行使用メモリ | 19,276 KB |
| 最終ジャッジ日時 | 2024-12-14 20:38:49 |
| 合計ジャッジ時間 | 22,849 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1} ;
struct ppUF{
vector<int> ra,par,ti;
ppUF(int n){
ra.resize(n,0);
ti.resize(n,inf+1);
par.resize(n,-1);
}
int find(int t,int x){
if(ti[x]>t)return x;
return find(t,par[x]);
}
bool unite(int t,int x,int y){
x=find(t,x);y=find(t,y);
if(x==y)return false;
if(ra[x]>ra[y]){
par[y]=x;
ti[y]=t;
}
else{
par[x]=y;
ti[x]=t;
if(ra[x]==ra[y])ra[y]++;
}
return true;
}
bool same(int t,int x,int y){
return find(t,x)==find(t,y);
}
int time(int x,int y){
if(!same(202020,x,y))return -1;
int ok=202020,ng=-1,mid=0;
while(ok-ng>1){
mid=(ok+ng)/2;
if(same(mid,x,y))ok=mid;
else ng=mid;
}
return ok;
}
};
int main(){
int n,m,q;
cin>>n>>m>>q;
map<pint,int> mp;
vector<int> ord;
rep(i,m)ord.push_back(i);
pint v[m];
rep(i,m)cin>>v[i].first>>v[i].second;
rep(i,q){
int x,y;
cin>>x>>y;
mp[{x,y}]=q-i;
}
sort(ord.begin(),ord.end(),[&](int a,int b){return mp[v[a]]<mp[v[b]];});
ppUF uf(n+1);
for(auto idx:ord){
uf.unite(mp[v[idx]], v[idx].first, v[idx].second);
}
rep(i,n-1){
int ret=uf.time(1, i+2);
if(ret==-1)cout<<0<<endl;
else if(ret==0)cout<<-1<<endl;
else cout<<q+1-ret<<endl;
}
return 0;
}
tempura_pp