結果

問題 No.416 旅行会社
ユーザー hirokazu1020hirokazu1020
提出日時 2016-08-26 23:11:09
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,031 bytes
コンパイル時間 1,098 ms
コンパイル使用メモリ 107,516 KB
実行使用メモリ 813,888 KB
最終ジャッジ日時 2024-04-25 21:44:35
合計ジャッジ時間 5,024 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
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 #

#define _CRT_SECURE_NO_WARNINGS
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<random>
using namespace std;
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define uniq(v) v.erase(unique(all(v)),v.end())


#define MAX_N 100000
int par[MAX_N];
int _rank[MAX_N];
vector<int> co[MAX_N];
bool can[MAX_N];


void concat(vector<int> &a,vector<int> &b){
	for(auto x:b){
		a.push_back(x);
	}
	b.clear();
}

void init(int n){
	for(int i=0;i<n;i++){
		par[i]=i;
		_rank[i]=0;
		
		co[i].push_back(i);
	}
	can[0]=true;
}
int find(int x){
	if(par[x]==x)return x;
	return par[x]=find(par[x]);
}
void unite(int x,int y){
	x=find(x);
	y=find(y);
	
	if(co[x].size()<co[y].size())concat(co[y],co[x]);
	else concat(co[y],co[x]);
	if(_rank[x]<_rank[y]){
		par[x]=y;
		if(co[y].empty())co[x].swap(co[y]);
	}else{
		par[y]=x;
		if(_rank[x]==_rank[y])_rank[x]++;
		if(co[x].empty())co[x].swap(co[y]);
	}

	
	if(can[x])can[y]=true;
	else if(can[y])can[x]=true;
}
bool same(int x,int y){
	return find(x)==find(y);
}


int x[200000],y[200000];
int ans[100000];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m,q;
	cin>>n>>m>>q;
	set<pair<int,int>> e;
	rep(i,m){
		int a,b;
		cin>>a>>b;
		a--;b--;
		e.emplace(a,b);
	}
	rep(i,q){
		cin>>x[i]>>y[i];
		x[i]--;y[i]--;
		e.erase(make_pair(x[i],y[i]));
	}
	init(n);
	for(auto p: e){
		unite(p.first,p.second);
	}
	rep(i,n){
		if(find(i)==0)ans[i]=-1;
	}
	for(int i=q-1;i>=0;i--){
		if(!same(x[i],y[i])){
			if(can[find(x[i])] ){
				for(int u : co[find(y[i])]){
					ans[u] = i+1;
				}
			}else if(can[find(y[i])]){
				for(int u : co[find(x[i])]){
					ans[u] = i+1;
				}
			}
			unite(x[i],y[i]);
		}
	}
	for(int i=1;i<n;i++){
		cout<<ans[i]<<endl;
	}
	return 0;
}
0