結果
| 問題 |
No.556 仁義なきサルたち
|
| ユーザー |
bal4u
|
| 提出日時 | 2019-07-26 21:37:09 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,673 bytes |
| コンパイル時間 | 192 ms |
| コンパイル使用メモリ | 32,512 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-02 06:43:57 |
| 合計ジャッジ時間 | 1,191 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
コンパイルメッセージ
main.c: In function 'in':
main.c:7:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
7 | #define gc() getchar_unlocked()
| ^~~~~~~~~~~~~~~~
main.c:15:24: note: in expansion of macro 'gc'
15 | int n = 0, c = gc();
| ^~
main.c: In function 'out':
main.c:8:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
8 | #define pc(c) putchar_unlocked(c)
| ^~~~~~~~~~~~~~~~
main.c:24:17: note: in expansion of macro 'pc'
24 | if (!n) pc('0');
| ^~
ソースコード
// yukicoder: No.556 仁義なきサルたち
// 2019.7.26 bal4u
#include <stdio.h>
#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in() { // 非負整数の入力
int n = 0, c = gc();
do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
return n;
}
void out(int n) { // 非負整数の表示(出力)
int i;
char b[20];
if (!n) pc('0');
else {
// if (n < 0) pc('-'), n = -n;
i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
while (i--) pc(b[i]);
}
pc('\n');
}
/* UNION-FIND library */
#define MAX 10003
int id[MAX], size[MAX];
void init(int n) { int i; for (i = 0; i < n; i++) id[i] = i, size[i] = 1; }
int root(int i) { while (i != id[i]) id[i] = id[id[i]], i = id[i]; return i; }
int connected(int p, int q) { return root(p) == root(q); }
void unite(int p, int q) {
int i = root(p), j = root(q); if (i == j) return;
if (size[i] < size[j]) id[i] = j, size[j] += size[i]; else id[j] = i, size[i] += size[j];
}
int top[MAX], sz[MAX];
int main()
{
int i, N, M, a, b, aa, bb;
N = in(), M = in();
init(N+1); for (i = 1; i <= N; i++) top[i] = i, sz[i] = 1;
while (M--) {
a = in(), b = in();
aa = top[root(a)], bb = top[root(b)];
if (aa == bb) continue;
unite(a, b);
if (sz[aa] == sz[bb]) {
if (aa < bb) top[root(a)] = top[root(b)] = aa, sz[aa] <<= 1;
else top[root(a)] = top[root(b)] = bb, sz[bb] <<= 1;
} else if (sz[aa] > sz[bb]) {
top[root(a)] = top[root(b)] = aa, sz[aa] += sz[bb];
} else {
top[root(a)] = top[root(b)] = bb, sz[bb] += sz[aa];
}
}
for (i = 1; i <= N; i++) out(top[root(i)]);
return 0;
}
bal4u