結果

問題 No.416 旅行会社
ユーザー 👑 kmjpkmjp
提出日時 2016-08-27 00:29:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 462 ms / 4,000 ms
コード長 1,785 bytes
コンパイル時間 2,085 ms
コンパイル使用メモリ 176,672 KB
実行使用メモリ 23,280 KB
最終ジャッジ日時 2024-05-08 15:01:52
合計ジャッジ時間 7,878 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 219 ms
17,904 KB
testcase_01 AC 9 ms
11,892 KB
testcase_02 AC 9 ms
12,020 KB
testcase_03 AC 9 ms
12,016 KB
testcase_04 AC 8 ms
11,888 KB
testcase_05 AC 10 ms
12,012 KB
testcase_06 AC 9 ms
11,888 KB
testcase_07 AC 10 ms
12,016 KB
testcase_08 AC 14 ms
12,140 KB
testcase_09 AC 33 ms
12,912 KB
testcase_10 AC 233 ms
17,776 KB
testcase_11 AC 236 ms
17,772 KB
testcase_12 AC 239 ms
17,772 KB
testcase_13 AC 217 ms
18,364 KB
testcase_14 AC 446 ms
23,148 KB
testcase_15 AC 462 ms
23,280 KB
testcase_16 AC 460 ms
23,152 KB
testcase_17 AC 456 ms
23,152 KB
testcase_18 AC 457 ms
23,152 KB
testcase_19 AC 330 ms
20,980 KB
testcase_20 AC 333 ms
20,848 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];
set<pair<int,int>> E;

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>>y;
		E.insert({x-1,y-1});
	}
	FOR(i,Q) {
		cin>>X[i]>>Y[i];
		X[i]--,Y[i]--;
		E.erase({X[i],Y[i]});
	}
	
	FORR(r,E) uf(r.first,r.second);
	FOR(i,N) V[uf[i]].push_back(i);
	FOR(i,N) if(uf[0]==uf[i]) R[i]=-1;
	
	for(i=Q-1;i>=0;i--) if(uf[X[i]] != uf[Y[i]]) {
		x=uf[X[i]];
		y=uf[Y[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(x,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