結果
| 問題 |
No.416 旅行会社
|
| ユーザー |
latte0119
|
| 提出日時 | 2016-08-26 22:36:12 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 1,187 ms / 4,000 ms |
| コード長 | 3,156 bytes |
| コンパイル時間 | 1,702 ms |
| コンパイル使用メモリ | 172,704 KB |
| 実行使用メモリ | 123,856 KB |
| 最終ジャッジ日時 | 2024-12-14 19:48:26 |
| 合計ジャッジ時間 | 10,654 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:110:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
110 | scanf("%lld%lld%lld",&N,&M,&Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:114:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
114 | scanf("%lld%lld",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:123:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
123 | scanf("%lld%lld",&C[i],&D[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
class Array {
public:
Array() {}
Array(int n) {
h = 0;
for (int i = 1; i < n; i *= 8) h += 3;
}
int *mutable_get(int k) {
auto p = mutable_get(k, root, 0, h);
root = p.first;
return &p.second->value;
}
int operator[](int k) {
node *res = immutable_get(k, root, 0, h);
return res != nullptr ? res->value : -1;
}
private:
struct node {
node *ch[8] = {};
int value = -1;
};
int h;
node *root = nullptr;
node *immutable_get(int a, node *x, int l, int d) {
if (x == nullptr) return x;
if (d == 0) return x;
int id = (a - l) >> (d - 3);
return immutable_get(a, x->ch[id], l + (id << (d - 3)), d - 3);
}
pair<node *, node *> mutable_get(int a, node *x, int l, int d) {
x = x != nullptr ? new node(*x) : new node();
if (d == 0) return make_pair(x, x);
int id = (a - l) >> (d - 3);
auto p = mutable_get(a, x->ch[id], l + (id << (d - 3)), d - 3);
x->ch[id] = p.first;
return make_pair(x, p.second);
}
};
// root: O(logN loglogN)
// merge: O(logN loglogN)
struct UnionFind {
Array uf;
UnionFind() : uf(0) {}
UnionFind(int n) : uf(n) {}
int root(int x) {
int nd = uf[x];
if (nd < 0) return x;
return root(nd);
}
void merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return;
int *u = uf.mutable_get(x);
int *v = uf.mutable_get(y);
if (-*u < -*v) swap(u, v), swap(x, y);
*u += *v;
*v = x;
}
int size(int x) {
return -uf[root(x)];
}
int query(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return -uf[x];
} else {
return -uf[x] - uf[y];
}
}
};
int N,M,Q;
int C[222222],D[222222];
signed main(){
scanf("%lld%lld%lld",&N,&M,&Q);
vpint es;
rep(i,M){
int a,b;
scanf("%lld%lld",&a,&b);
a--;b--;
if(a>b)swap(a,b);
es.pb(pint(a,b));
}
sort(all(es));
vint used(M,0);
rep(i,Q){
scanf("%lld%lld",&C[i],&D[i]);
C[i]--;D[i]--;
if(C[i]>D[i])swap(C[i],D[i]);
int tmp=lower_bound(all(es),pint(C[i],D[i]))-es.begin();
used[tmp]=true;
}
vector<UnionFind>ufs(Q+1);
UnionFind uf(N);
rep(i,M)if(!used[i])uf.merge(es[i].fi,es[i].se);
ufs[Q]=uf;
for(int i=Q-1;i>=0;i--){
uf.merge(C[i],D[i]);
ufs[i]=uf;
}
reps(i,1,N){
int lb=-1,ub=Q+1;
while(ub-lb>1){
int mid=(ub+lb)/2;
if(ufs[mid].root(0)==ufs[mid].root(i)){
lb=mid;
}
else{
ub=mid;
}
}
if(ub==Q+1)puts("-1");
else printf("%lld\n",ub);
}
return 0;
}
latte0119