結果

問題 No.1813 Magical Stones
ユーザー ygussanyygussany
提出日時 2021-12-29 15:20:07
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,649 bytes
コンパイル時間 444 ms
コンパイル使用メモリ 31,488 KB
実行使用メモリ 14,644 KB
最終ジャッジ日時 2024-10-03 23:25:45
合計ジャッジ時間 4,725 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 4 ms
6,816 KB
testcase_04 AC 1 ms
6,816 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 1 ms
6,816 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
6,820 KB
testcase_13 AC 2 ms
6,820 KB
testcase_14 AC 4 ms
6,816 KB
testcase_15 WA -
testcase_16 AC 69 ms
12,612 KB
testcase_17 AC 69 ms
13,708 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 69 ms
12,160 KB
testcase_21 AC 68 ms
12,664 KB
testcase_22 AC 68 ms
14,260 KB
testcase_23 AC 69 ms
12,556 KB
testcase_24 WA -
testcase_25 AC 7 ms
6,820 KB
testcase_26 AC 4 ms
6,820 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 62 ms
11,676 KB
testcase_32 AC 2 ms
6,816 KB
testcase_33 AC 2 ms
6,820 KB
testcase_34 AC 4 ms
6,816 KB
testcase_35 AC 9 ms
6,820 KB
testcase_36 AC 2 ms
6,816 KB
testcase_37 AC 18 ms
6,820 KB
testcase_38 AC 6 ms
6,816 KB
testcase_39 AC 39 ms
10,384 KB
testcase_40 WA -
testcase_41 AC 4 ms
6,816 KB
testcase_42 AC 10 ms
6,816 KB
testcase_43 AC 6 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define N_MAX 100000
#define M_MAX 200000

typedef struct Edge {
	struct Edge *next;
	int v;
} edge;

void chmin(int* a, int b)
{
	if (*a > b) *a = b;
}

int DFS_SCC(edge* adj[], int label[], int ord[], int low[], int s[], int* head, int u)
{
	s[(*head)++] = u; // Add u to the stack (which maintains the vertices already found but not determined)
	ord[u] = ord[0]++;
	low[u] = ord[u];
	
	int w;
	edge *p;
	for (p = adj[u]; p != NULL; p = p->next) {
		w = p->v;
		if (ord[w] == 0) chmin(&(low[u]), DFS_SCC(adj, label, ord, low, s, head, w)); // w has been found
		else if (ord[w] <= N_MAX) chmin(&(low[u]), ord[w]); // w is already found but not determined
	}
	
	if (low[u] == ord[u]) { // A new SCC containing u has been determined
		while (s[--(*head)] != u) {
			label[s[*head]] = label[0];
			ord[s[*head]] = N_MAX + 1;
		}
		label[u] = label[0]++;
		ord[u] = N_MAX + 1;
	}
	return low[u];
}

int SCC(int N, edge* adj[], int label[], edge*** scc_adj, edge** scc_e)
{
	int u, w, head;
	static int ord[N_MAX + 1], low[N_MAX + 1], s[N_MAX + 1];
	for (u = 1; u <= N; u++) {
		label[u] = 0;
		ord[u] = 0;
	}
	for (u = 1, label[0] = 1, ord[0] = 1; u <= N; u++) {
		if (ord[u] != 0) continue;
		head = 0;
		DFS_SCC(adj, label, ord, low, s, &head, u);
	}
	
	int m = 0, n = label[0] - 1;
	edge *p;
	*scc_e = (edge*)malloc(sizeof(edge) * M_MAX);
	*scc_adj = (edge**)malloc(sizeof(edge*) * (n + 1));
	for (u = 1; u <= n; u++) (*scc_adj)[u] = NULL;
	for (u = 1; u <= N; u++) {
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (label[w] == label[u]) continue;
			(*scc_e)[m].v = label[w];
			(*scc_e)[m].next = (*scc_adj)[label[u]];
			(*scc_adj)[label[u]] = &((*scc_e)[m++]);
		}
	}
	*scc_e = (edge*)realloc(*scc_e, sizeof(edge) * m);
	return n;
}



int main(int argc, char* argv[])
{
	int i, N, M, u, w;
	edge *adj[N_MAX + 1] = {}, e[M_MAX + 1], *p;
	scanf("%d %d", &N, &M);
	for (i = 0; i < M; i++) {
		scanf("%d %d", &u, &w);
		e[i].v = w;
		e[i].next = adj[u];
		adj[u] = &(e[i]);
	}
	
	edge **scc_adj, *scc_e;
	int label[N_MAX + 1], n = SCC(N, adj, label, &scc_adj, &scc_e), ns, nt, deg[N_MAX + 1][2], flag[N_MAX + 1];
	if (n == 1) {
		printf("0\n");
		fflush(stdout);
		return 0;
	}
	for (u = 1; u <= n; u++) {
		deg[u][0] = 0; // Out-degree
		deg[u][1] = 0; // In-degree
	}
	for (u = 1; u <= n; u++) {
		for (p = scc_adj[u]; p != NULL; p = p->next) {
			w = p->v;
			deg[u][0]++;
			deg[w][1]++;
		}
	}
	for (u = 1, ns = 0, nt = 0; u <= n; u++) {
		if (deg[u][0] == 0) nt++; // Sink
		else if (deg[u][1] == 0) ns++; // Source
	}
	printf("%d\n", (ns > nt)? ns: nt);
	fflush(stdout);
	return 0;
}
0