結果
| 問題 |
No.556 仁義なきサルたち
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2017-08-14 00:57:51 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 5 ms / 2,000 ms |
| コード長 | 1,615 bytes |
| コンパイル時間 | 746 ms |
| コンパイル使用メモリ | 83,640 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-13 09:24:05 |
| 合計ジャッジ時間 | 1,528 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:84:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
84 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:90:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
90 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-
*
* 556.cc: No.556 仁義なきサルたち - yukicoder
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 10000;
const int INF = 1 << 30;
/* typedef */
typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;
struct UFT {
int links[MAX_N], sizes[MAX_N];
UFT() {}
void clear(int n) {
for (int i = 0; i < n; i++) links[i] = i, sizes[i] = 1;
}
int root(int i) {
int i0 = i;
while (links[i0] != i0) i0 = links[i0];
return (links[i] = i0);
}
int size(int i) { return sizes[root(i)]; }
bool same(int i, int j) { return root(i) == root(j); }
int merge(int i0, int i1) {
int r0 = root(i0), r1 = root(i1), mr;
if (r0 == r1) return r0;
if (r0 > r1) swap(r0, r1);
if (sizes[r0] >= sizes[r1]) {
links[r1] = r0;
sizes[r0] += sizes[r1];
mr = r0;
}
else {
links[r0] = r1;
sizes[r1] += sizes[r0];
mr = r1;
}
return mr;
}
};
/* global variables */
UFT uft;
/* subroutines */
/* main */
int main() {
int n, m;
scanf("%d%d", &n, &m);
uft.clear(n);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
uft.merge(a, b);
}
for (int i = 0; i < n; i++) printf("%d\n", uft.root(i) + 1);
return 0;
}
tnakao0123