結果

問題 No.416 旅行会社
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-08-27 20:56:34
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,545 bytes
コンパイル時間 852 ms
コンパイル使用メモリ 78,920 KB
実行使用メモリ 21,040 KB
最終ジャッジ日時 2024-04-26 07:01:24
合計ジャッジ時間 6,344 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
13,668 KB
testcase_01 AC 4 ms
8,392 KB
testcase_02 AC 3 ms
8,420 KB
testcase_03 WA -
testcase_04 AC 4 ms
8,304 KB
testcase_05 AC 3 ms
8,240 KB
testcase_06 AC 4 ms
8,256 KB
testcase_07 AC 4 ms
8,376 KB
testcase_08 AC 7 ms
8,448 KB
testcase_09 AC 23 ms
9,092 KB
testcase_10 AC 192 ms
15,472 KB
testcase_11 AC 195 ms
15,340 KB
testcase_12 AC 190 ms
15,504 KB
testcase_13 AC 160 ms
13,668 KB
testcase_14 AC 437 ms
20,872 KB
testcase_15 AC 447 ms
20,924 KB
testcase_16 AC 438 ms
20,752 KB
testcase_17 AC 443 ms
21,040 KB
testcase_18 AC 442 ms
20,800 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)){
 				for(auto u : item[group[b]]){
 					ans[u] = i + 1;
 				}
 			}else{
 				for(auto u : item[group[a]]){
 					ans[u] = i + 1;
 				}
 			}
 			merge(a, b);
 		}
    }
    for (int i = 1; i < n; ++i){
    	printf("%d\n", ans[i]);
    }
	return 0;
}
0