結果

問題 No.416 旅行会社
ユーザー peroonperoon
提出日時 2019-04-18 11:37:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 489 ms / 4,000 ms
コード長 3,513 bytes
コンパイル時間 2,618 ms
コンパイル使用メモリ 183,932 KB
実行使用メモリ 33,600 KB
最終ジャッジ日時 2023-08-21 10:24:40
合計ジャッジ時間 8,806 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 230 ms
19,460 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 27 ms
4,876 KB
testcase_10 AC 261 ms
19,388 KB
testcase_11 AC 267 ms
24,160 KB
testcase_12 AC 268 ms
24,144 KB
testcase_13 AC 231 ms
24,240 KB
testcase_14 AC 470 ms
32,084 KB
testcase_15 AC 454 ms
33,016 KB
testcase_16 AC 457 ms
33,600 KB
testcase_17 AC 455 ms
32,532 KB
testcase_18 AC 489 ms
32,392 KB
testcase_19 AC 330 ms
22,032 KB
testcase_20 AC 327 ms
21,316 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: メンバ関数 ‘std::vector<long long int> UnionFind::GetList(ll)’ 内:
main.cpp:80:5: 警告: 非 void を戻す関数内に return 文がありません [-Wreturn-type]
   80 |     }
      |     ^

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define LOCAL
#ifdef LOCAL
#define show(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif

template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}

struct UnionFind{
    vector<ll> parent;
    UnionFind(ll sz){
        parent.resize(sz);
        reset();
    }

    void reset(){
        FOR(i, 0, parent.size()){
            parent[i] = i;
        }
    }

    void unite(ll a, ll b){
        if(a>b){
            swap(a, b);
        }
        ll rootA = findRoot(a);
        ll rootB = findRoot(b);
        if(rootA>rootB){
            swap(rootA, rootB);
        }
        if(rootA==rootB){
            return;
        }else{
            // 小さい方を親にする
            parent[rootB] = rootA;
        }
    }

    ll findRoot(ll a){
        if(parent[a]==a){
            return a;
        }else{
            return parent[a] = findRoot(parent[a]);
        }
    }

    void print(){
        cout << "---" << endl;
        for(ll p : parent){
            cout << p << endl;
        }
        cout << "---" << endl;
    }

    vector<ll> GetList(ll i){

    }
};

vector<vector<ll> > G;
vector<ll> ans;

// 塗る
void dfs(ll i, ll v){
    // cout << "dfs " << i << " " << v << endl;
    ans[i] = v;
    for(ll to : G[i]){
        if(ans[to]==0) dfs(to, v);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N, M, Q;
    cin >> N >> M >> Q;
    
    G.resize(N);
    ans.resize(N);

    vector<ll> A(M);
    vector<ll> B(M);
    FOR(i, 0, M){
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        A[i] = a;
        B[i] = b;
    }

    // 壊される辺
    vector<ll> C(Q);
    vector<ll> D(Q);
    map<pair<ll, ll>, ll> mp_break;
    FOR(i, 0, Q){
        ll c, d;
        cin >> c >> d;
        c--;
        d--;
        C[i] = c;
        D[i] = d;
        mp_break[make_pair(c, d)] = 1;
    }


    auto uf = UnionFind(N);

    // 辺をはる
    FOR(i, 0, M){
        ll a = A[i];
        ll b = B[i];
        auto p = make_pair(a, b);
        if(mp_break.count(p)>0){
            // 辺をはらない
        }else{
            uf.unite(a, b);

            G[a].push_back(b);
            G[b].push_back(a);
        }
    }

    // 壊した後もつながっている点
    FOR(i, 0, N){
        if(uf.findRoot(i)==0){
            ans[i] = -1;
        }
    }

    // vprint(ans);

    // 破壊を逆順に
    // uf.print();
    for(int i=Q-1; i>=0; i--){
        // pn(i);
        ll c = C[i];
        ll d = D[i];
        ll rootC = uf.findRoot(c);
        ll rootD = uf.findRoot(d);
        // cout << "connect " << c << " " << d << " , " << rootC << " " << rootD << endl;

        if(rootC==0 && rootD!=0){
            // 今回でつながる
            dfs(rootD, i+1);
        }
        if(rootD==0 && rootC!=0){
            dfs(rootC, i+1);
        }
        uf.unite(c, d);
        
        G[c].push_back(d);
        G[d].push_back(c);        
    }

    FOR(i, 1, N){
        p(ans[i]);
    }
    // vprint(ans);
    
    return 0;
}
0