結果

問題 No.556 仁義なきサルたち
ユーザー takubokudoritakubokudori
提出日時 2017-08-12 08:02:05
言語 C++11
(gcc 11.4.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 876 bytes
コンパイル時間 476 ms
コンパイル使用メモリ 65,156 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 05:50:36
合計ジャッジ時間 3,960 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int u(int, int)’:
main.cpp:27:15: warning: control reaches end of non-void function [-Wreturn-type]
   27 |         k[fa] = k[fb] = t;
      |         ~~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <string>
#define MAX 10001
using namespace std;
typedef long long int lli;

int f[MAX];
int k[MAX];


int find(int x) {
	if (f[x] == x)return x;
	else return f[x] = find(f[x]);
}

int u(int a, int b) {
	int fa = find(a);
	int fb = find(b);
	if (fa == fb)return 0;
	// cout<<"u:"<<a<<":"<<find(a)<<" "<<b<<":"<<find(b)<<endl;
	f[fa] = fb;
	int t = k[fa] + k[fb];
	fa = find(a);
	k[fa] = k[fb] = t;
}

int main(void) {
	int n, m, a, b;
	cin >> n >> m;
	for (int i = 0; i<n; i++) {
		f[i] = i;
		k[i] = 1;
	}
	while (m--) {
		cin >> a >> b;
		a--; b--;
		if (k[find(a)] == k[find(b)]) {
			if (find(a)>find(b)) u(a, b);
			else if (find(a)<find(b)) u(b, a);
		}
		else if (k[find(a)]<k[find(b)]) u(a, b);
		else u(b, a);
	}
	for (int i = 0; i<n; i++) {
		cout << find(i) + 1 << endl;
	}
	return 0;
}
0