結果

問題 No.416 旅行会社
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-08-28 02:24:57
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,672 bytes
コンパイル時間 848 ms
コンパイル使用メモリ 77,796 KB
実行使用メモリ 20,924 KB
最終ジャッジ日時 2024-04-26 07:20:40
合計ジャッジ時間 6,892 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
13,672 KB
testcase_01 AC 4 ms
8,192 KB
testcase_02 AC 3 ms
8,192 KB
testcase_03 WA -
testcase_04 AC 4 ms
8,320 KB
testcase_05 AC 4 ms
8,320 KB
testcase_06 AC 4 ms
8,320 KB
testcase_07 AC 5 ms
8,224 KB
testcase_08 AC 8 ms
8,428 KB
testcase_09 AC 23 ms
9,088 KB
testcase_10 AC 202 ms
15,216 KB
testcase_11 AC 192 ms
15,472 KB
testcase_12 AC 193 ms
15,264 KB
testcase_13 AC 160 ms
13,668 KB
testcase_14 AC 498 ms
20,924 KB
testcase_15 AC 510 ms
20,920 KB
testcase_16 AC 501 ms
20,796 KB
testcase_17 AC 520 ms
20,668 KB
testcase_18 AC 530 ms
20,796 KB
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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++)

///Quick Find Weighted///
const int MAX_N = 200010;
int group[MAX_N];//group[i] := 頂点iが所属するグループ
vector<int> item[MAX_N];//item[g]:= グループgに所属する頂点番号
// アイテムiaの所属するグループとアイテムibの所属するグループを1つにする(異なるグループに属するものに)
void merge(int ia, int ib){
	//iaの所属するグループがibの所属するグループより小さくならないようにする(一般的マージテク)
	if(item[group[ia]].size() < item[group[ib]].size()){
		swap(ia, ib);
	}
	int ga = group[ia], gb = group[ib];//グループgbの方が要素数が少ない
	for(auto u : item[gb]){//uには頂点の番号はいる
		group[u] = ga;//グループの番号を更新
	}
	item[ga].insert(item[ga].end(), item[gb].begin(), item[gb].end());//つなげる
	item[gb].clear();
}
// アイテムiaとアイテムibは同じグループに所属しているか?
bool is_same_group(int ia, int ib){
	return group[ia] == group[ib];
}

void init(int n){
	for (int i = 0; i < n; ++i){
		group[i] = i;
		item[i].push_back(i);
	}
}
/////////////////////////


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.insert(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));
	}

	init(n);//初期化
	for(auto u : s){//最後まで残っている橋をつなげる
		merge(u.first, u.second);
	}

    vector<int> ans(n, 0);//初めから到達できないのは0
    for (int i = 1; i < n; ++i){
    	if(is_same_group(0, i)){
    		ans[i] = -1;//橋がすべて壊れても到達できるのは-1
    	}
    }
    for (int i = q - 1; i >= 0 ; --i){
 		int a = cd[i].first, b = cd[i].second;
 		if(!is_same_group(a, b)){
 			if(!is_same_group(0, a) && !is_same_group(0, b)){//0につながることはない
 				merge(a, b);
 				continue;
 			}
 			
 			if(!is_same_group(0, a) && !is_same_group(0, b)){
                merge(a, b);
            }else{
                if(is_same_group(0, b)){
                	swap(a, b);
                }

                for (int el : item[group[b]])
                    ans[el] = i + 1;
                merge(a, b);
            }
 		}
    }

    for (int i = 1; i < n; ++i){
    	printf("%d\n", ans[i]);
    }
	return 0;
}
0