結果

問題 No.416 旅行会社
ユーザー 👑 kmjpkmjp
提出日時 2016-08-26 22:45:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 568 ms / 4,000 ms
コード長 1,909 bytes
コンパイル時間 2,106 ms
コンパイル使用メモリ 162,360 KB
実行使用メモリ 32,236 KB
最終ジャッジ日時 2023-08-21 09:40:06
合計ジャッジ時間 8,329 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 225 ms
25,648 KB
testcase_01 AC 10 ms
15,112 KB
testcase_02 AC 10 ms
15,112 KB
testcase_03 AC 10 ms
15,192 KB
testcase_04 AC 10 ms
15,068 KB
testcase_05 AC 10 ms
15,072 KB
testcase_06 AC 10 ms
15,072 KB
testcase_07 AC 11 ms
15,340 KB
testcase_08 AC 14 ms
15,764 KB
testcase_09 AC 35 ms
16,408 KB
testcase_10 AC 253 ms
25,592 KB
testcase_11 AC 258 ms
25,532 KB
testcase_12 AC 260 ms
25,576 KB
testcase_13 AC 222 ms
26,316 KB
testcase_14 AC 548 ms
32,112 KB
testcase_15 AC 533 ms
32,132 KB
testcase_16 AC 550 ms
32,172 KB
testcase_17 AC 568 ms
32,232 KB
testcase_18 AC 557 ms
32,236 KB
testcase_19 AC 400 ms
26,840 KB
testcase_20 AC 373 ms
26,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

template<int um> class UF {
	public:
	vector<int> par,rank;
	UF() {rank=vector<int>(um,0); for(int i=0;i<um;i++) par.push_back(i);}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};
UF<500000> uf;

int N,M,Q;
int R[101010];
int X[201010],Y[202020];
map<pair<int,int>,int> MM;
int T[202020];
int D[202020];

vector<int> V[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>Q;
	FOR(i,M) {
		cin>>X[i]>>Y[i];
		X[i]--;
		Y[i]--;
		MM[{X[i],Y[i]}]=i;
	}
	MINUS(D);
	FOR(i,Q) {
		cin>>x>>y;
		T[i] = MM[{x-1,y-1}];
		D[T[i]]=i;
	}
	
	FOR(i,M) if(D[i]<0) uf(X[i],Y[i]);
	FOR(i,N) V[uf[i]].push_back(i);
	FOR(i,N) if(uf[0]==uf[i]) R[i]=-1;
	//FOR(r,N) _P("%d",uf[i]);
	
	for(i=Q-1;i>=0;i--) if(uf[X[T[i]]] != uf[Y[T[i]]]) {
		x=uf[X[T[i]]];
		y=uf[Y[T[i]]];
		if(x==uf[0]) {
			FORR(r,V[y]) R[r]=i+1;
		}
		if(y==uf[0]) {
			FORR(r,V[x]) R[r]=i+1;
		}
		if(V[x].size()<V[y].size()) swap(V[x],V[y]);
		FORR(r,V[y]) V[x].push_back(r);
		V[y].clear();
		uf(x,y);
		if(uf[x]==y) swap(V[x],V[y]);
		
	}
	FOR(i,N-1) cout<<R[i+1]<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0