結果

問題 No.416 旅行会社
ユーザー 👑 kmjpkmjp
提出日時 2016-08-26 22:45:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 495 ms / 4,000 ms
コード長 1,909 bytes
コンパイル時間 1,832 ms
コンパイル使用メモリ 177,788 KB
実行使用メモリ 32,500 KB
最終ジャッジ日時 2024-05-08 14:56:48
合計ジャッジ時間 7,830 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 228 ms
25,232 KB
testcase_01 AC 9 ms
14,164 KB
testcase_02 AC 10 ms
14,520 KB
testcase_03 AC 9 ms
13,700 KB
testcase_04 AC 9 ms
13,728 KB
testcase_05 AC 9 ms
14,012 KB
testcase_06 AC 9 ms
13,420 KB
testcase_07 AC 11 ms
13,792 KB
testcase_08 AC 14 ms
14,736 KB
testcase_09 AC 36 ms
13,940 KB
testcase_10 AC 264 ms
24,560 KB
testcase_11 AC 254 ms
24,564 KB
testcase_12 AC 252 ms
24,560 KB
testcase_13 AC 229 ms
25,200 KB
testcase_14 AC 486 ms
32,368 KB
testcase_15 AC 478 ms
32,252 KB
testcase_16 AC 495 ms
32,480 KB
testcase_17 AC 479 ms
32,500 KB
testcase_18 AC 457 ms
32,336 KB
testcase_19 AC 323 ms
25,968 KB
testcase_20 AC 337 ms
25,992 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