結果

問題 No.416 旅行会社
ユーザー IL_mstaIL_msta
提出日時 2017-03-01 18:27:44
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,329 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 132,412 KB
実行使用メモリ 20,000 KB
最終ジャッジ日時 2023-09-03 16:38:40
合計ジャッジ時間 13,378 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

#pragma region GNUC
//以下を参考にした
//https://yukicoder.me/wiki/auto_vectorization
#ifdef __GNUC__
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#endif
#pragma endregion

#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <cmath>

#include <string>
#include <cstring>
#include <vector>
#include <valarray>
//#include <array>//x C++ (G++ 4.6.4)

#include <queue>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <cassert>//assert();
#include <fstream>
#include <random>
/////////
#define REP(i, x, n) for(int i = x; i < n; ++i)
#define rep(i,n) REP(i,0,n)

/////////
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#define PII pair<int,int>
/////////
using namespace::std;

// 最大公約数
template<class T>
inline T gcd(T a, T b){return b ? gcd(b, a % b) : a;}
//inline T gcd(T a, T b){return b == 0 ? a : gcd(b, a % b);}

// 最小公倍数
template<class T>
inline T lcm(T a, T b){return a / gcd(a, b) * b;}
//inline T lcm(T a, T b){return a * b / gcd(a, b);}
////////////////////////////////
class UnionFind{
public:
	int cNum;//要素数
	vector<int> parent;
	vector< list<int> > Glist;
	UnionFind(int n){
		cNum = n;
		parent = vector<int>(n);
		Glist.resize(n);
		for(int i=n-1;i>=0;--i){
			parent[i] = i;
			Glist[i].push_back(i);
		}
	}
	int find(int x){
		if( parent[x] == x ){return x;}
		return parent[x] = find( parent[x] );
	}
	bool same(int x,int y){return find(x) == find(y);}
	void add(int x,int y){//union
		x = find(x);
		y = find(y);
		if( x==y )return;
		parent[x] = y;
		Glist[y].merge( Glist[x] );
	}
};

void solve(){
	int N,M,Q;
	cin >> N >> M >> Q;
	vector<pair<int,int> > GraphEdge(M);
	for(int i=0;i<M;++i){
		int A,B;
		cin >> A >> B;
		--A;--B;//A<B
		GraphEdge[i] = make_pair(A,B);
	}
	sort( GraphEdge.begin(),GraphEdge.end() );
	vector<bool> useEdge(M,true);
	vector<pair<int,int> > AddEdge(Q);
	vector< pair<int,int> >::iterator itr,begin,end;
	begin = GraphEdge.begin();
	end = GraphEdge.end();
	for(int i=0;i<Q;++i){
		int C,D;
		cin >> C >> D;
		--C;--D;//C<D
		AddEdge[i] = make_pair(C,D);
		
		int index = lower_bound( begin,end,AddEdge[i] ) - begin;
		useEdge[index] = false;
	}

	UnionFind uf(N);

	for(int i=0;i<M;++i){
		if( !useEdge[i] ) continue;
		uf.add(GraphEdge[i].first,GraphEdge[i].second);
	}
	vector<int> ans(N,0);
	int root = 0;
	list<int>::iterator Litr,Lbegin,Lend;
	Litr = uf.Glist[uf.find(root)].begin();
	Lend = uf.Glist[uf.find(root)].end();
	for(;Litr != Lend;++Litr){
		ans[*Litr] = -1;
	}

	for(int i=Q-1;i>=0;--i){
		int x = AddEdge[i].first;
		int y = AddEdge[i].second;
		if( uf.same(x,y) ) continue;
		if( uf.same(x,root) ){
			Litr = uf.Glist[uf.find(y)].begin();
			Lend = uf.Glist[uf.find(y)].end();
		}else if( uf.same(y,root) ){
			Litr = uf.Glist[uf.find(x)].begin();
			Lend = uf.Glist[uf.find(x)].end();
		}else{
			uf.add(x,y);
			continue;
		}
		for(;Litr!=Lend;++Litr){
			ans[*Litr] = i+1;
		}
		uf.add(x,y);
	}
	for(int i=1;i<N;++i){
		cout << ans[i] << endl;
	}
}

signed main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	//std::cout << std::fixed;//小数を10進数表示
	//cout << setprecision(16);//小数点以下の桁数を指定
	
	solve();
}
0