結果

問題 No.679 不思議マーケット
コンテスト
ユーザー bal4u
提出日時 2019-07-29 20:36:41
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,201 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 183 ms
コンパイル使用メモリ 39,240 KB
最終ジャッジ日時 2026-02-22 04:03:16
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// yukicoder: No.679 不思議マーケット
// 2019.7.29 bal4u

#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

// トポロジーソート
#define TMAX 505
int t_hi[TMAX], t_to[TMAX][TMAX];   // トポロジーソートのための有向グラフ構造
int t_ans[TMAX];                    // トポロジーソートの結果。後ろのほうが指されたもの。

int topoSort(int V) {
	int i, j, k, sz;
	static int q[TMAX], top, end;
	static int count[TMAX];

	for (i = 0; i < V; i++) for (j = 0; j < t_hi[i]; j++) count[t_to[i][j]]++;
	top = end = 0;
	for (i = 0; i < V; i++) if (!count[i]) q[end++] = i;
	sz = 0;  while (top < end) {
		t_ans[sz++] = i = q[top++];
		for (j = 0; j < t_hi[i]; j++) {
			k = t_to[i][j];
			if (--count[k] == 0) q[end++] = k;
		}
	}
	return sz;
}

int main()
{
	int i, k, h, n, m;

	n = in(), m = in();
	while (m--) {
		int g = in()-1, r = in();
		while (r--) h = in()-1, t_to[h][t_hi[h]++] = g;
	}
	printf("%d\n", topoSort(n));
	return 0;
}
0