結果

問題 No.416 旅行会社
ユーザー koprickykopricky
提出日時 2017-07-17 05:16:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 627 ms / 4,000 ms
コード長 2,463 bytes
コンパイル時間 1,446 ms
コンパイル使用メモリ 163,568 KB
実行使用メモリ 31,008 KB
最終ジャッジ日時 2023-08-21 10:12:41
合計ジャッジ時間 8,870 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 270 ms
19,784 KB
testcase_01 AC 5 ms
10,344 KB
testcase_02 AC 5 ms
10,324 KB
testcase_03 AC 5 ms
10,348 KB
testcase_04 AC 5 ms
10,352 KB
testcase_05 AC 5 ms
10,352 KB
testcase_06 AC 5 ms
10,484 KB
testcase_07 AC 6 ms
10,396 KB
testcase_08 AC 11 ms
10,992 KB
testcase_09 AC 37 ms
12,028 KB
testcase_10 AC 321 ms
20,376 KB
testcase_11 AC 319 ms
20,600 KB
testcase_12 AC 316 ms
20,436 KB
testcase_13 AC 272 ms
19,692 KB
testcase_14 AC 614 ms
30,836 KB
testcase_15 AC 598 ms
31,008 KB
testcase_16 AC 610 ms
30,932 KB
testcase_17 AC 615 ms
30,840 KB
testcase_18 AC 627 ms
30,948 KB
testcase_19 AC 478 ms
28,956 KB
testcase_20 AC 472 ms
29,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

vector<int> G[MAX_N];
set<int> hoge[MAX_N];
vector<P> rem;
vector<int> kind;
vector<vector<int> > inc;

void initial(int n)
{
    kind.resize(n);
    inc.resize(n);
    rep(i,n){
        kind[i] = i;
        inc[i].assign(1,i);
    }
}

void unite(int a,int b)
{
    if(inc[kind[a]].size() < inc[kind[b]].size()){
        swap(a,b);
    }
    int ga = kind[a],gb = kind[b];
    rep(i,inc[gb].size()){
        kind[inc[gb][i]] = ga;
    }
    inc[ga].insert(inc[ga].end(),inc[gb].begin(),inc[gb].end());
    inc[gb].clear();
}

bool same(int a,int b){
    return kind[a] == kind[b];
}

int main()
{
    int n,m,q;
    cin >> n >> m >> q;
    rep(i,m){
        int a,b;
        cin >> a >> b;
        hoge[a-1].insert(b-1);
        hoge[b-1].insert(a-1);
    }
    rep(i,q){
        int c,d;
        cin >> c >> d;
        rem.pb(P(c-1,d-1));
        hoge[c-1].erase(d-1);
        hoge[d-1].erase(c-1);
    }
    rep(i,n){
        each(it,hoge[i]){
            G[i].pb(it);
        }
    }
    initial(n);
    rep(i,n){
        rep(j,G[i].size()){
            if(!same(i,G[i][j])){
                unite(i,G[i][j]);
            }
        }
    }
    vector<int> ans(n-1,0);
    rep(i,n-1){
        if(same(0,i+1)){
            ans[i] = -1;
        }
    }
    for(int i=q-1;i>=0;i--){
        P p = rem[i];
        if(!same(p.fi,p.se)){
            if(same(0,p.fi)){
                rep(j,inc[kind[p.se]].size()){
                    ans[inc[kind[p.se]][j]-1] = i+1;
                }
                unite(0,p.se);
            }else if(same(0,p.se)){
                rep(j,inc[kind[p.fi]].size()){
                    ans[inc[kind[p.fi]][j]-1] = i+1;
                }
                unite(0,p.fi);
            }else{
                unite(p.fi,p.se);
            }
        }
    }
    rep(i,n-1){
        cout << ans[i] << endl;
    }
    return 0;
}
0