結果

問題 No.416 旅行会社
ユーザー bal4ubal4u
提出日時 2019-08-24 11:14:01
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 280 ms / 4,000 ms
コード長 2,684 bytes
コンパイル時間 368 ms
コンパイル使用メモリ 31,268 KB
実行使用メモリ 163,008 KB
最終ジャッジ日時 2023-08-21 10:26:03
合計ジャッジ時間 4,776 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 219 ms
162,744 KB
testcase_01 AC 1 ms
5,748 KB
testcase_02 AC 1 ms
5,832 KB
testcase_03 AC 1 ms
5,720 KB
testcase_04 AC 2 ms
5,760 KB
testcase_05 AC 2 ms
5,856 KB
testcase_06 AC 3 ms
6,112 KB
testcase_07 AC 4 ms
7,496 KB
testcase_08 AC 11 ms
12,492 KB
testcase_09 AC 47 ms
44,888 KB
testcase_10 AC 203 ms
162,736 KB
testcase_11 AC 201 ms
162,652 KB
testcase_12 AC 202 ms
162,752 KB
testcase_13 AC 216 ms
162,732 KB
testcase_14 AC 280 ms
162,896 KB
testcase_15 AC 270 ms
163,008 KB
testcase_16 AC 237 ms
162,884 KB
testcase_17 AC 238 ms
163,004 KB
testcase_18 AC 239 ms
163,008 KB
testcase_19 AC 212 ms
159,136 KB
testcase_20 AC 215 ms
159,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: No.416 旅行会社
// 2019.8.24 bal4u

#include <stdio.h>

typedef long long ll;

#if 0
#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 9999991
typedef struct { ll r; int id; } HASH;
HASH hash[HASHSIZ+5], *hashend = hash+HASHSIZ;

int lookup(int a, int b) {
	ll r = (ll)b << 17 | a;
	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)b << 17 | a;
	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;
	size[1] = n;  // 本問題のための特例
}
inline static int root(int i) { while (i != id[i]) id[i] = id[id[i]], i = id[i]; return i; }
inline static void unite(int p, int q, int tm) {
    int t, i = root(p), j = root(q); if (i == j) return;
	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];
	if (i == 1) { 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