結果

問題 No.416 旅行会社
ユーザー hirokazu1020hirokazu1020
提出日時 2016-08-26 23:17:21
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,096 bytes
コンパイル時間 953 ms
コンパイル使用メモリ 108,300 KB
実行使用メモリ 18,112 KB
最終ジャッジ日時 2024-04-25 21:46:45
合計ジャッジ時間 5,890 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 194 ms
13,000 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
7,492 KB
testcase_05 AC 2 ms
7,492 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 3 ms
7,492 KB
testcase_08 AC 7 ms
6,940 KB
testcase_09 AC 24 ms
6,980 KB
testcase_10 AC 212 ms
12,740 KB
testcase_11 AC 206 ms
13,128 KB
testcase_12 AC 206 ms
12,996 KB
testcase_13 AC 190 ms
12,872 KB
testcase_14 AC 360 ms
17,988 KB
testcase_15 AC 377 ms
17,860 KB
testcase_16 AC 361 ms
17,988 KB
testcase_17 AC 369 ms
18,112 KB
testcase_18 AC 369 ms
17,988 KB
testcase_19 AC 289 ms
15,684 KB
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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(x==y)return;
	
	if(co[x].size()<co[y].size())concat(co[y],co[x]);
	else concat(co[x],co[y]);
	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])] ){
				vector<int> &v = co[find(y[i])];
				for(int u : v){
					ans[u] = i+1;
				}
			}else if(can[find(y[i])]){
				vector<int> &v = co[find(x[i])];
				for(int u : v){
					ans[u] = i+1;
				}
			}
			unite(x[i],y[i]);
		}
	}
	for(int i=1;i<n;i++){
		cout<<ans[i]<<endl;
	}
	return 0;
}
0