結果

問題 No.556 仁義なきサルたち
ユーザー EtoNagisaEtoNagisa
提出日時 2017-08-12 00:19:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,832 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 164,184 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 00:14:04
合計ジャッジ時間 1,979 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[aa]; if (aa != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)///
struct UnionFind {
	vector<int> par;
	vector<int> s;

	UnionFind(int n) {
		par.resize(n);
		s.resize(n);
		REP(i, n) {
			par[i] = i;
			s[i] = 1;
		}
	}

	int root(int a) {
		return par[a] == a ? a : par[a] = root(par[a]);
	}

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

	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y)return;
		if (s[x] < s[y]) {
			par[x] = y;
			s[y] += s[x];
		}
		else if(s[x]>s[y]) {
			par[y] = x;
			s[x] += s[y];
		}
		else {
			if (x < y) {
				par[y] = x;
				s[x] += s[y];
			}
			else {
				par[x] = y;
				s[y] += s[x];
			}
		}
	}
};
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	UnionFind uf(n);
	REP(i, m) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		uf.unite(a, b);
	}
	REP(i, n) {
		cout << uf.root(i)+1 << endl;
	}
}
0