結果

問題 No.556 仁義なきサルたち
ユーザー takubokudoritakubokudori
提出日時 2017-08-12 00:17:12
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 767 bytes
コンパイル時間 582 ms
コンパイル使用メモリ 65,180 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-21 00:12:26
合計ジャッジ時間 4,378 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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:26:13: warning: control reaches end of non-void function [-Wreturn-type]
   26 |         k[a]=k[b]=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[find(a)]=find(b);
	int t=k[a]+k[b];
	k[a]=k[b]=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[a]==k[b]){
			if(find(a)>find(b)) u(a,b);
			else if(find(a)<find(b)) u(b,a);
		}else if(k[a]<k[b]) u(a,b);
		else u(b,a);
	}
	for(int i=0;i<n;i++){
		cout<<find(i)+1<<endl;
	}
	return 0;
}
0