結果

問題 No.1813 Magical Stones
ユーザー 👑 ygussanyygussany
提出日時 2021-12-29 15:22:46
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 2,618 bytes
コンパイル時間 228 ms
コンパイル使用メモリ 32,852 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2024-04-16 02:02:55
合計ジャッジ時間 3,834 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 70 ms
12,160 KB
testcase_16 AC 67 ms
12,160 KB
testcase_17 AC 66 ms
13,568 KB
testcase_18 AC 65 ms
12,160 KB
testcase_19 AC 67 ms
12,160 KB
testcase_20 AC 66 ms
12,160 KB
testcase_21 AC 66 ms
12,160 KB
testcase_22 AC 66 ms
12,160 KB
testcase_23 AC 66 ms
12,160 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 7 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 43 ms
8,960 KB
testcase_28 AC 58 ms
10,752 KB
testcase_29 AC 52 ms
10,112 KB
testcase_30 AC 50 ms
9,984 KB
testcase_31 AC 58 ms
11,264 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 10 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 18 ms
5,376 KB
testcase_38 AC 6 ms
5,376 KB
testcase_39 AC 37 ms
8,448 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 10 ms
5,376 KB
testcase_43 AC 6 ms
5,376 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 i, N, M, u, w;
	edge *adj[N_MAX + 1] = {}, e[M_MAX], *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
		if (deg[u][1] == 0) ns++; // Source
	}
	printf("%d\n", (ns > nt)? ns: nt);
	fflush(stdout);
	return 0;
}
0