結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
19,964 KB
testcase_01 AC 5 ms
10,452 KB
testcase_02 AC 5 ms
10,548 KB
testcase_03 AC 5 ms
10,456 KB
testcase_04 AC 5 ms
10,496 KB
testcase_05 AC 5 ms
10,612 KB
testcase_06 AC 5 ms
10,464 KB
testcase_07 AC 6 ms
10,632 KB
testcase_08 AC 8 ms
10,960 KB
testcase_09 AC 17 ms
11,992 KB
testcase_10 AC 125 ms
20,764 KB
testcase_11 AC 117 ms
20,788 KB
testcase_12 AC 117 ms
20,544 KB
testcase_13 AC 79 ms
19,968 KB
testcase_14 AC 334 ms
31,000 KB
testcase_15 AC 348 ms
31,064 KB
testcase_16 AC 355 ms
31,184 KB
testcase_17 AC 364 ms
30,900 KB
testcase_18 AC 380 ms
31,216 KB
testcase_19 AC 277 ms
28,968 KB
testcase_20 AC 266 ms
29,216 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){
        printf("%d\n",ans[i]);
    }
    return 0;
}
0