This commit is contained in:
unlockable
2023-11-20 19:34:26 +08:00
parent 615133f12a
commit 414e2b676c
2 changed files with 19 additions and 31 deletions

View File

@@ -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,7 +89,6 @@ int main() {
bool connected = true;
for (int k = 0; k < N; k++) {
if (nodes_visited[k] < 1) {
printf("0\n");
connected = false;
break;
}
@@ -112,6 +96,9 @@ int main() {
if (connected) {
printf("1\n");
}
else {
printf("0\n");
}
}
else {
printf("1\n");

View File

@@ -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