結果
| 問題 | No.679 不思議マーケット |
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-07-29 20:36:41 |
| 言語 | C(gnu17) (gcc 15.2.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,201 bytes |
| 記録 | |
| コンパイル時間 | 183 ms |
| コンパイル使用メモリ | 39,240 KB |
| 最終ジャッジ日時 | 2026-02-22 04:03:16 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 |
ソースコード
// 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;
}
bal4u