From 414e2b676c6cb8e9ae04c00ca733c17308b36aef Mon Sep 17 00:00:00 2001 From: unlockable Date: Mon, 20 Nov 2023 19:34:26 +0800 Subject: [PATCH] AC! --- 2023205/main.cpp | 49 +++++++++++++++++---------------------------- 2023205/testdat.txt | 1 + 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/2023205/main.cpp b/2023205/main.cpp index 83c4adc..6169785 100644 --- a/2023205/main.cpp +++ b/2023205/main.cpp @@ -2,21 +2,13 @@ // 并查集。并查集只关注双向连通性。 struct Edge { - int next; unsigned short end; bool used_in_dfs; }; Edge edges[1050000]; char nodes_visited[65540]; -int head[65540], len; - -int add_edge(int start, int terminal) { - edges[++len].end = terminal; - edges[len].next = head[start]; - head[start] = len; - return 0; -} +int pos[65540]; int dfs(int start) { if (nodes_visited[start] >= 1) { @@ -24,7 +16,7 @@ int dfs(int start) { return 0; } nodes_visited[start] = 1; - for (int j = head[start]; j != 0; j = edges[j].next) { + for (int j = pos[start]; j < pos[start + 1]; j++) { edges[j].used_in_dfs = dfs(edges[j].end); } return 1; @@ -36,17 +28,17 @@ int dfs_with_dead_edge(int start, int dead_edge_start, int dead_edge_end) { } nodes_visited[start] = 1; if (start == dead_edge_start) { - for (int j = head[start]; j != 0; j = edges[j].next) { + for (int j = pos[start]; j < pos[start + 1]; j++) { if (edges[j].end == dead_edge_end) { continue; } - dfs(edges[j].end); + dfs_with_dead_edge(edges[j].end, dead_edge_start, dead_edge_end); } return 0; } - for (int j = head[start]; j != 0; j = edges[j].next) { - dfs(edges[j].end); + for (int j = pos[start]; j < pos[start + 1]; j++) { + dfs_with_dead_edge(edges[j].end, dead_edge_start, dead_edge_end); } return 0; @@ -55,15 +47,19 @@ int dfs_with_dead_edge(int start, int dead_edge_start, int dead_edge_end) { int main() { int N, M; scanf("%d %d", &N, &M); + int total_edge = 0; for (int start = 0; start < N; start++) { + pos[start] = total_edge; int edge_count; scanf("%d", &edge_count); for (int j = 0; j < edge_count; j++) { - int terminal; - scanf("%d", &terminal); - add_edge(start, terminal); + unsigned short terminal; + scanf("%hu", &terminal); + edges[pos[start] + j] = Edge{terminal, false}; } + total_edge += edge_count; } + pos[N] = total_edge; dfs(0); @@ -79,21 +75,10 @@ int main() { printf("1\n"); - // for (int i = 0; i < M; i++) { - // int start, end; - // scanf("%d %d", &start, &end); - // if (nodes_visited[end] == 2) { - // printf("1\n"); - // } - // else { - // printf("0\n"); - // } - // } - for (int i = 0; i < M; i++) { int start, end; scanf("%d %d", &start, &end); - for (int j = head[start]; j != 0; j = edges[j].next) { + for (int j = pos[start]; j < pos[start + 1]; j++) { if (edges[j].end == end) { if (edges[j].used_in_dfs) { for (int k = 0; k < N; k++) { @@ -104,14 +89,16 @@ int main() { bool connected = true; for (int k = 0; k < N; k++) { if (nodes_visited[k] < 1) { - printf("0\n"); connected = false; break; } } if (connected) { printf("1\n"); - } + } + else { + printf("0\n"); + } } else { printf("1\n"); diff --git a/2023205/testdat.txt b/2023205/testdat.txt index b256dc0..6052b31 100644 --- a/2023205/testdat.txt +++ b/2023205/testdat.txt @@ -2,3 +2,4 @@ 4 1 1 1 2 2 3 2 1 3 2 1 2 0 1 +4 1 1 1 2 2 3 2 1 3 2 1 2 1 2 \ No newline at end of file