結果

問題 No.416 旅行会社
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-29 16:35:34
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,418 bytes
コンパイル時間 1,171 ms
コンパイル使用メモリ 81,744 KB
実行使用メモリ 21,824 KB
最終ジャッジ日時 2024-05-01 07:47:14
合計ジャッジ時間 12,067 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)

class DisjointSet{
    public:
        vector<int> rank, p;//rank:木の高さ p:親の頂点番号
        DisjointSet(){}
        DisjointSet(int size){//頂点の数
            rank.resize(size, 0);
            p.resize(size, 0);
            rep(i, size) makeSet(i);
        }
        void makeSet(int x){
            p[x] = x;
            rank[x] = 0;
        }
        //同じ木にあるか判定(今回使わない)
        bool same(int x, int y){
            return findSet(x) == findSet(y);
        }
        // 木どうしをくっつける
        void unite(int x, int y){
            link(findSet(x), findSet(y));
        }
        //木の高さを考慮して木どうしをくっつける
        void link(int x, int y){
            if(rank[x] > rank[y]){
                p[y] = x;
            }else{
                p[x] = y;
                if(rank[x] == rank[y]) rank[y]++;
            }
        }
        //親を探す(ルートまで)
        int findSet(int x){
            if(x != p[x]){
                p[x] = findSet(p[x]);
            }
            return p[x];
        }
};

int main(void){
	int n, m, q; cin >> n >> m >> q;
	vector<pair<int, int> > cd;
	set<pair<int, int> > s;//最後まで残っている橋を入れる

	for (int i = 0; i < m; ++i){
		int a, b; cin >> a >> b;
		a--; b--;
		s.emplace(make_pair(a, b));
	}
	for (int i = 0; i < q; ++i){
		int c, d; cin >> c >> d;
		c--; d--;
		cd.push_back(make_pair(c, d));
		s.erase(make_pair(c, d));
	}

	DisjointSet ds = DisjointSet(n);
	for(auto u : s){
		ds.unite(u.first, u.second);
	}

    vector<int> ans(n, 0);
    for (int i = 1; i < n; ++i){
    	if(ds.same(0, i)){
    		ans[i] = -1;
    	}
    }

    for (int i = q - 1; i >= 0; --i){
        int a = cd[i].first, b = cd[i].second;
        if(!ds.same(a, b)){
            if(!ds.same(0, a) && !ds.same(0, b)){//0につながることはない
                ds.unite(a, b);
                continue;
            }

    	    ds.unite(cd[i].first, cd[i].second);
    	    for (int j = 1; j < n; ++j){
    		    if(ans[j] == 0 && ds.same(0, j)){
    		        ans[j] = i + 1;
                }
            }
        }
    }
    for (int i = 1; i < n; ++i){
    	printf("%d\n", ans[i]);
    }
	return 0;
}
0