結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 204 ms
19,976 KB
testcase_01 AC 5 ms
10,388 KB
testcase_02 AC 5 ms
10,508 KB
testcase_03 AC 5 ms
10,480 KB
testcase_04 AC 5 ms
10,596 KB
testcase_05 AC 5 ms
10,540 KB
testcase_06 AC 5 ms
10,544 KB
testcase_07 AC 6 ms
10,460 KB
testcase_08 AC 10 ms
10,764 KB
testcase_09 AC 30 ms
11,908 KB
testcase_10 AC 250 ms
20,636 KB
testcase_11 AC 243 ms
20,436 KB
testcase_12 AC 244 ms
20,632 KB
testcase_13 AC 204 ms
19,756 KB
testcase_14 AC 475 ms
30,928 KB
testcase_15 AC 464 ms
31,048 KB
testcase_16 AC 468 ms
30,928 KB
testcase_17 AC 468 ms
30,904 KB
testcase_18 AC 450 ms
31,108 KB
testcase_19 AC 382 ms
28,960 KB
testcase_20 AC 399 ms
29,220 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;
        scanf("%d%d",&a,&b);
        hoge[a-1].insert(b-1);
        hoge[b-1].insert(a-1);
    }
    rep(i,q){
        int c,d;
        scanf("%d%d",&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