結果

問題 No.416 旅行会社
ユーザー bal4ubal4u
提出日時 2019-08-24 11:16:06
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 103 ms / 4,000 ms
コード長 2,695 bytes
コンパイル時間 1,298 ms
コンパイル使用メモリ 30,980 KB
実行使用メモリ 38,536 KB
最終ジャッジ日時 2023-08-21 10:25:36
合計ジャッジ時間 3,753 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
37,636 KB
testcase_01 AC 1 ms
5,712 KB
testcase_02 AC 2 ms
5,784 KB
testcase_03 AC 1 ms
5,796 KB
testcase_04 AC 2 ms
5,772 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
5,344 KB
testcase_08 AC 10 ms
12,168 KB
testcase_09 AC 28 ms
28,696 KB
testcase_10 AC 64 ms
37,632 KB
testcase_11 AC 63 ms
37,704 KB
testcase_12 AC 63 ms
37,596 KB
testcase_13 AC 62 ms
37,688 KB
testcase_14 AC 103 ms
38,500 KB
testcase_15 AC 98 ms
38,528 KB
testcase_16 AC 100 ms
38,536 KB
testcase_17 AC 98 ms
38,508 KB
testcase_18 AC 98 ms
38,468 KB
testcase_19 AC 74 ms
37,692 KB
testcase_20 AC 74 ms
37,624 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 2000003 // 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