結果

問題 No.416 旅行会社
ユーザー koprickykopricky
提出日時 2018-09-07 10:12:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 399 ms / 4,000 ms
コード長 2,750 bytes
コンパイル時間 2,893 ms
コンパイル使用メモリ 181,716 KB
実行使用メモリ 31,108 KB
最終ジャッジ日時 2023-08-21 10:19:48
合計ジャッジ時間 7,713 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
19,952 KB
testcase_01 AC 5 ms
10,660 KB
testcase_02 AC 5 ms
10,596 KB
testcase_03 AC 6 ms
10,812 KB
testcase_04 AC 6 ms
10,560 KB
testcase_05 AC 6 ms
10,620 KB
testcase_06 AC 6 ms
10,704 KB
testcase_07 AC 7 ms
10,664 KB
testcase_08 AC 8 ms
10,840 KB
testcase_09 AC 18 ms
11,996 KB
testcase_10 AC 137 ms
20,516 KB
testcase_11 AC 126 ms
20,616 KB
testcase_12 AC 127 ms
20,532 KB
testcase_13 AC 81 ms
19,948 KB
testcase_14 AC 389 ms
31,108 KB
testcase_15 AC 387 ms
31,092 KB
testcase_16 AC 383 ms
31,108 KB
testcase_17 AC 389 ms
31,004 KB
testcase_18 AC 399 ms
30,948 KB
testcase_19 AC 303 ms
28,996 KB
testcase_20 AC 303 ms
29,244 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;

class MergeTech{
private:
    int sz;
public:
    vector<int> kind;   //所属しているグループ番号
    vector<vector<int> > inc;   //グループのメンバー
    MergeTech(){}
    MergeTech(int node_size) : sz(node_size), kind(sz), inc(sz){
        for(int i = 0; i < sz; i++){
            kind[i] = i, inc[i].push_back(i);
        }
    }
    bool same(int a,int b){ return kind[a] == kind[b]; }
    void unite(int a,int b){
        if(same(a,b)) return;
        if(inc[kind[a]].size() < inc[kind[b]].size()) swap(a,b);
        int ga = kind[a], gb = kind[b];
        for(int u : inc[gb]){
            kind[u] = ga;
        }
        inc[ga].insert(inc[ga].end(),inc[gb].begin(),inc[gb].end());
        inc[gb].clear();
    }
};


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);
        }
    }
    MergeTech mt(n);
    rep(i,n){
        rep(j,G[i].size()){
            if(!mt.same(i,G[i][j])){
                mt.unite(i,G[i][j]);
            }
        }
    }
    vector<int> ans(n-1,0);
    rep(i,n-1){
        if(mt.same(0,i+1)){
            ans[i] = -1;
        }
    }
    for(int i=q-1;i>=0;i--){
        P p = rem[i];
        if(!mt.same(p.fi,p.se)){
            if(mt.same(0,p.fi)){
                rep(j,mt.inc[mt.kind[p.se]].size()){
                    ans[mt.inc[mt.kind[p.se]][j]-1] = i+1;
                }
                mt.unite(0,p.se);
            }else if(mt.same(0,p.se)){
                rep(j,mt.inc[mt.kind[p.fi]].size()){
                    ans[mt.inc[mt.kind[p.fi]][j]-1] = i+1;
                }
                mt.unite(0,p.fi);
            }else{
                mt.unite(p.fi,p.se);
            }
        }
    }
    rep(i,n-1){
        printf("%d\n",ans[i]);
    }
    return 0;
}
0