結果

問題 No.416 旅行会社
ユーザー bal4ubal4u
提出日時 2019-08-24 10:13:51
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 159 ms / 4,000 ms
コード長 2,634 bytes
コンパイル時間 457 ms
コンパイル使用メモリ 31,240 KB
実行使用メモリ 85,476 KB
最終ジャッジ日時 2023-08-21 10:25:51
合計ジャッジ時間 3,774 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
84,504 KB
testcase_01 AC 2 ms
5,720 KB
testcase_02 AC 2 ms
5,848 KB
testcase_03 AC 1 ms
5,696 KB
testcase_04 AC 2 ms
5,868 KB
testcase_05 AC 2 ms
5,856 KB
testcase_06 AC 2 ms
6,020 KB
testcase_07 AC 3 ms
5,284 KB
testcase_08 AC 11 ms
12,076 KB
testcase_09 AC 40 ms
39,488 KB
testcase_10 AC 116 ms
84,544 KB
testcase_11 AC 115 ms
84,600 KB
testcase_12 AC 116 ms
84,572 KB
testcase_13 AC 112 ms
84,484 KB
testcase_14 AC 153 ms
85,380 KB
testcase_15 AC 151 ms
85,472 KB
testcase_16 AC 159 ms
85,456 KB
testcase_17 AC 156 ms
85,380 KB
testcase_18 AC 156 ms
85,476 KB
testcase_19 AC 129 ms
84,412 KB
testcase_20 AC 128 ms
84,536 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: 備考: in expansion of macro ‘gc’
   17 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:10:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:26:17: 備考: in expansion of macro ‘pc’
   26 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// 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;
}
0