結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-08-24 10:13:51 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 155 ms / 4,000 ms |
| コード長 | 2,634 bytes |
| コンパイル時間 | 455 ms |
| コンパイル使用メモリ | 32,128 KB |
| 実行使用メモリ | 85,376 KB |
| 最終ジャッジ日時 | 2024-12-14 20:38:01 |
| 合計ジャッジ時間 | 3,415 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
コンパイルメッセージ
main.c: In function 'in':
main.c:9:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
9 | #define gc() getchar_unlocked()
| ^~~~~~~~~~~~~~~~
main.c:17:24: note: in expansion of macro 'gc'
17 | int n = 0, c = gc();
| ^~
main.c: In function 'out':
main.c:10:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
10 | #define pc(c) putchar_unlocked(c)
| ^~~~~~~~~~~~~~~~
main.c:26:17: note: in expansion of macro 'pc'
26 | if (!n) pc('0');
| ^~
ソースコード
// yukicoder: No.416 旅行会社
// 2019.8.24 bal4u
#include <stdio.h>
typedef long long ll;
#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), c = gc(); while (c >= '0');
return n;
}
void out(int n) { // 整数の表示(出力)
int i;
char b[30];
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');
}
//// ハッシュ関数
#define HASHSIZ 5000011 // 2000081
typedef struct { ll r; int id; } HASH;
HASH hash[HASHSIZ+5], *hashend = hash+HASHSIZ;
int lookup(int a, int b) {
ll r = (ll)a << 17 | b;
HASH *p = hash + (int)(r % HASHSIZ);
while (p->r) {
if (p->r == r) return p->id;
if (++p == hashend) p = hash;
}
return -1;
}
void insert(int a, int b, int id) {
ll r = (ll)a << 17 | b;
HASH *p = hash + (int)(r % HASHSIZ);
while (p->r) {
if (p->r == r) return;
if (++p == hashend) p = hash;
}
p->r = r, p->id = id;
}
#define MAX 100005
int top[MAX], end[MAX], next[MAX];
int tim[MAX];
//// UNION-FIND library
int id[MAX], size[MAX];
void init(int n) { int i; for (i = 1; i <= n; i++) id[i] = top[i] = end[i] = i, size[i] = 1; }
int root(int i) { while (i != id[i]) id[i] = id[id[i]], i = id[i]; return i; }
void unite(int p, int q, int tm) {
int t, i = root(p), j = root(q); if (i == j) return;
if (i != 1 && j != 1) {
if (size[i] < size[j]) t = i, i = j, j = t;
id[j] = i, size[i] += size[j];
next[end[i]] = top[j], end[i] = end[j];
} else {
if (i > j) t = i, i = j, j = t;
id[j] = i, size[i] += size[j];
next[end[i]] = top[j], end[i] = end[j];
j = top[j]; do tim[j] = tm, j = next[j]; while (j);
}
}
int N;
typedef struct { int a, b; } EDGE;
EDGE edge[MAX*2]; int M;
EDGE down[MAX*2]; int Q;
char ng[MAX*2];
int main()
{
int i, Q;
N = in(), M = in(), Q = in();
// 橋の入力。id番号を登録
for (i = 0; i < M; i++) {
edge[i].a = in(), edge[i].b = in();
insert(edge[i].a, edge[i].b, i);
}
// 破壊する橋リストの入力。上記のid番号と一致する橋をngとマーク
for (i = 1; i <= Q; i++) {
down[i].a = in(), down[i].b = in();
ng[lookup(down[i].a, down[i].b)] = 1;
}
init(N); // Union-find 初期化
// 最後まで残った橋たちをつなぐ
for (i = 0; i < M; i++) if (!ng[i]) unite(edge[i].a, edge[i].b, -1);
// 逆順で橋を復旧していく
for (i = Q; i; i--) unite(down[i].a, down[i].b, i);
for (i = 2; i <= N; i++) out(tim[i]);
return 0;
}
bal4u