結果

問題 No.416 旅行会社
ユーザー latte0119latte0119
提出日時 2016-08-26 22:36:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,190 ms / 4,000 ms
コード長 3,156 bytes
コンパイル時間 1,926 ms
コンパイル使用メモリ 173,240 KB
実行使用メモリ 123,816 KB
最終ジャッジ日時 2024-05-08 14:56:31
合計ジャッジ時間 10,883 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 269 ms
119,412 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 7 ms
6,940 KB
testcase_09 AC 40 ms
12,520 KB
testcase_10 AC 276 ms
118,224 KB
testcase_11 AC 255 ms
119,288 KB
testcase_12 AC 270 ms
119,160 KB
testcase_13 AC 255 ms
118,352 KB
testcase_14 AC 1,190 ms
123,680 KB
testcase_15 AC 776 ms
123,512 KB
testcase_16 AC 654 ms
123,816 KB
testcase_17 AC 814 ms
123,780 KB
testcase_18 AC 891 ms
123,552 KB
testcase_19 AC 613 ms
114,324 KB
testcase_20 AC 920 ms
113,780 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#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;
}
0