結果

問題 No.556 仁義なきサルたち
ユーザー idat_50meidat_50me
提出日時 2020-07-27 16:52:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,160 bytes
コンパイル時間 1,607 ms
コンパイル使用メモリ 169,992 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-11 05:55:08
合計ジャッジ時間 2,751 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T, typename U>
using pv=vector<pair<T,U>>;
template<typename T>
using matrix=vector<vector<T>>;
template<typename T>
using pque=priority_queue<T>;
template<typename T>
using lpque=priority_queue<T,vector<T>,greater<T>>;

using ll=long long;
using intpair=pair<int,int>;
using llpair=pair<ll,ll>;
using ilpair=pair<int,ll>;
using lipair=pair<ll,int>;
using intvec=vector<int>;
using llvec=vector<ll>;
using intq=queue<int>;
using llq=queue<ll>;
using intmat=vector<intvec>;
using llmat=vector<llvec>;

#define PI 3.141592653589793
#define INTINF ((1<<30)-1)
#define LLINF 1LL<<60
#define MPRIME 1000000007
#define MPRIME9 998244353
#define MMPRIME (1ll<<61)-1

#define len length()
#define pushb push_back
#define fi first
#define se second

#define all(name) name.begin(),name.end()
#define rall(name) name.rbegin(),name.rend()
#define gsort(vbeg,vend) sort(vbeg,vend,greater<>())

template<class T>
inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class T>
inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

template<class T>
inline void init(T& v) {
	for(auto &a: v) cin>>a;
}
template<class T, class U>
inline void init(vector<pair<T,U>>& v) {
	for(auto &a: v) cin>>a.first>>a.second;
}
template<class T, class N>
inline void init(T& v, N n) {
	v.resize(n);
	for(auto &a: v) cin>>a;
}
template<class T, class U, class N>
inline void init(vector<pair<T,U>>& v, N n) {
	v.resize(n);
	for(auto &a: v) cin>>a.first>>a.second;
}

template<class T>
inline void out(T a) {
	cout<<a<<endl;
}
template<class T, class... U>
inline void out(T a, U... alist) {
	cout<<a<<" ";
	out(forward<U>(alist)...);
}

template<class N>
void resiz(N n) {
	//empty
}
template<class N, class T, class... U>
void resiz(N n, T&& hd, U&&... tl) {
	hd.resize(n);
	resiz(n,forward<U>(tl)...);
}

ll binpow(ll a, ll ex, ll p) {
	ll result=1;
	while(ex>0) {
		if(ex&1) result=result*a%p;
		ex>>=1;
		a=a*a%p;
	}
	return result;
}




struct Union_Find {
private:
	vector<int> parent;
	vector<int> num;
	int treenum;

public:
	Union_Find(int n) : parent(n),num(n,1) {
		treenum=n;
		for(int i=0; i<n; i++) {
			parent[i]=i;
		}
	}

	int root(int x) {
		assert(x < parent.size());
		if(parent[x]==x) return x;
		return parent[x]=root(parent[x]);
	}

	int size(int x) {
		return num[root(x)];
	}

	void merge(int x, int y) {
		int xrt=root(x);
		int yrt=root(y);
		if(xrt==yrt) return;
		parent[yrt]=xrt;
		num[xrt]+=num[yrt];
		treenum--;
	}

	bool same(int x, int y) {
		return root(x)==root(y);
	}

	int tnum() {
		return treenum;
	}
};



int N,M;

void input() {
	cin>>N>>M;
}

void solve() {
	Union_Find uf(N+1);
	for(int i=0; i<M; i++) {
		int A,B; cin>>A>>B;
		int Asize=uf.size(A), Bsize=uf.size(B);
		if(Asize>Bsize || (Asize==Bsize && uf.root(A)<uf.root(B))) uf.merge(A,B);
		else uf.merge(B,A);
	}

	for(int i=1; i<=N; i++) {
		cout<<uf.root(i)<<'\n';
	}
	cout<<flush;
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout<<fixed<<setprecision(15);
	int t=1;
	while(t) {
		input();
		solve();
		t--;
	}
}
0