結果
| 問題 |
No.556 仁義なきサルたち
|
| ユーザー |
horiesiniti
|
| 提出日時 | 2016-07-06 04:02:34 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 2,000 ms |
| コード長 | 671 bytes |
| コンパイル時間 | 129 ms |
| コンパイル使用メモリ | 23,936 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-19 05:22:35 |
| 合計ジャッジ時間 | 1,092 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:18:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
18 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
main.cpp:25:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
25 | scanf("%d %d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#include<stdio.h>
const int LIMIT=10001;
int cs[LIMIT];
int uTree[LIMIT];
int f(int n){
if(n==uTree[n]){
return n;
}else{
int e=f(uTree[n]);
uTree[n]=e;
return e;
}
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
cs[i]=1;
uTree[i]=i;
}
for(int i=0;i<m;i++){
int a,b;
scanf("%d %d",&a,&b);
a=f(a);
b=f(b);
if(a==b){
continue;
}else if(cs[a]!=cs[b]){
if(cs[a]<cs[b]){
uTree[a]=b;
cs[b]+=cs[a];
}else{
uTree[b]=a;
cs[a]+=cs[b];
}
}else{
if(a>b){
uTree[a]=b;
cs[b]+=cs[a];
}else{
uTree[b]=a;
cs[a]+=cs[b];
}
}
}
for(int i=1;i<=n;i++){
printf("%d\n",f(i));
}
}
horiesiniti