結果

問題 No.416 旅行会社
ユーザー furafura
提出日時 2020-07-25 14:41:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,695 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 222,404 KB
実行使用メモリ 19,940 KB
最終ジャッジ日時 2023-09-09 12:45:41
合計ジャッジ時間 14,778 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class union_find{
	int n;
	vector<int> p;
public:
	union_find(int n):n(n),p(n,-1){}
	int find(int u){ return p[u]<0?u:p[u]=find(p[u]); }
	void unite(int u,int v){
		u=find(u); v=find(v);
		if(u!=v){
			if(p[v]<p[u]) swap(u,v);
			p[u]+=p[v]; p[v]=u; n--;
		}
	}
	bool is_same(int u,int v){ return find(u)==find(v); }
	int size()const{ return n; }
	int size(int u){ return -p[find(u)]; }
};

int main(){
	int n,m,q; scanf("%d%d%d",&n,&m,&q);
	set<pair<int,int>> E;
	rep(i,m){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		E.emplace(u,v);
	}
	vector<pair<int,int>> F(q);
	rep(i,q){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		F[i]={u,v};
		E.erase({u,v});
	}

	vector<vector<int>> C(n);
	rep(u,n) C[u].emplace_back(u);
	union_find U(n);
	for(auto [u,v]:E) if(!U.is_same(u,v)) {
		u=U.find(u);
		v=U.find(v);
		U.unite(u,v);
		vector<int> tmp;
		merge(C[u].begin(),C[u].end(),C[v].begin(),C[v].end(),back_inserter(tmp));
		if(u==U.find(u)) C[u]=tmp; // u が親, v が子
		else             C[v]=tmp; // v が親, u が子
	}

	vector<int> ans(n,-1);
	rep(u,n) if(U.is_same(0,u)) ans[u]=q;
	for(int i=q-1;i>=0;i--){
		int u=U.find(F[i].first);
		int v=U.find(F[i].second);
		if(!U.is_same(u,v)){
			if(U.is_same(0,u) && !U.is_same(0,v)){
				for(int w:C[v]) ans[w]=i;
			}
			else if(U.is_same(0,v) && !U.is_same(0,u)){
				for(int w:C[u]) ans[w]=i;
			}
			U.unite(u,v);
			vector<int> tmp;
			merge(C[u].begin(),C[u].end(),C[v].begin(),C[v].end(),back_inserter(tmp));
			if(u==U.find(u)) C[u]=tmp;
			else             C[v]=tmp;
		}
	}
	for(int u=1;u<n;u++) printf("%d\n",ans[u]==q?-1:ans[u]+1);

	return 0;
}
0