結果

問題 No.92 逃走経路
ユーザー bal4ubal4u
提出日時 2019-07-11 21:57:26
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 2,335 bytes
コンパイル時間 813 ms
コンパイル使用メモリ 31,692 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-08 05:29:42
合計ジャッジ時間 1,636 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 0 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 0 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:8:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:24: 備考: in expansion of macro ‘gc’
   16 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:9:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:26:21: 備考: in expansion of macro ‘pc’
   26 |         while (i--) pc(b[i]);
      |                     ^~

ソースコード

diff #

// yukicoder: No.92 逃走経路
// 2019.7.11 bal4u

#include <stdio.h>
#include <stdlib.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];

	i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
	while (i--) pc(b[i]);
}

#define INF 0x7fffffff
typedef struct { int a, id; } Q;
Q q[500000]; int top;
typedef struct { int a, b, c; } T;
T t[1005]; int M;
int to[105][1005], mo[105][1005], hi[105]; int N;
int d[1005], K;
char ans[105], f[105][105];
int aa[2010][2], sz;

int cmp(const void *a, const void *b) { return ((T *)a)->c - ((T *)b)->c; }

// t[i].c >= x という条件を満たす最小のi
int lower_bound(int x) {
	int m, l = 0, r = M;
	while (l+1 < r) {
		m = (l+r)/2;
		if (t[m].c < x) l = m; else r = m;
	}
	return t[l].c < x? r: l;
}

int dfs(int a, int id) {
	int i;
	
	top = 0, q[top].a = a, q[top++].id = id;
	while (top) {
		a = q[--top].a, id = q[top].id;
		if (id == K) return 1;
		for (i = 0; i < hi[a]; i++) {
			if (mo[a][i] == d[id]) q[top].a = to[a][i], q[top++].id = id+1;
		}
	}
	return 0;
}

int main()
{
	int i, k, a, b, c;

	N = in(), M = in(), K = in();
	for (i = 0; i < M; i++) {
		a = in(), b = in(), c = in();
		t[i].a = a, t[i].b = b, t[i].c = c;
		k = hi[a]++, to[a][k] = b, mo[a][k] = c;
		k = hi[b]++, to[b][k] = a, mo[b][k] = c;
	}
	qsort(t, M, sizeof(T), cmp); t[M].c = INF;
	k = -1, i = K; while (i--) {
		d[i] = in();
		if (k < 0) k = d[i];
		else if (k != d[i]) k = INF;
	}

	if (k < INF && t[M-1].c == t[0].c) {
		k = 0; for (i = 0; i < M; i++) {
			a = t[i].a, b = t[i].b;
			if (!ans[a]) ans[a] = 1, k++;
			if (!ans[b]) ans[b] = 1, k++;
		}
		goto done;
	}
	
	i = lower_bound(d[0]);
	while (t[i].c == d[0]) {
		a = t[i].a, b = t[i].b;
		if (!f[a][b]) f[a][b] = 1, aa[sz][0] = t[i].a, aa[sz++][1] = t[i].b;
		if (!f[b][a]) f[b][a] = 1, aa[sz][0] = t[i].b, aa[sz++][1] = t[i].a;
		i++;
	}

	k = 0; for (i = 0; i < sz; i++) {
		a = aa[i][0], b = aa[i][1];
		if (!ans[a] && dfs(b, 1)) ans[a] = 1, k++;
	}
done:
	out(k), pc('\n');
	for (i = 1; i <= N; i++) if (ans[i]) out(i), pc(' ');
	pc('\n');
	return 0;
}
0