- 问答
求助实现方式为什么错了
- 2023-3-25 7:07:57 @
改成 vector 就 a 了
#include "bits/stdc++.h"
using namespace std;
int n, s;
struct edge {
int to, nxt;
}e[1919810];
int hd[1919810];
int vis[1919810];
int cnt;
void add_edge (int u, int v) {
++cnt;
e[cnt].to = v;
e[cnt].nxt = hd[u];
hd[u] = cnt;
}
void dfs(int now) {
for (int i = hd[now]; i; i = e[i].nxt) {
int to = e[i].to;
if (!vis[to]) {
vis[to] = 1;
printf ("%d ", to);
dfs(to);
}
}
}
int main() {
scanf ("%d%d", &n, &s);
for (int i = 1; i <= n; ++i) {
int u, v;
scanf ("%d%d", &u, &v);
add_edge (u, v);
add_edge (v, u);
}
vis[s] = 1;
printf ("%d ", s);
dfs(s);
}
1 条评论
-
滨小王彧烐 LV 9 @ 2023-3-25 17:30:10
邻接表存储是与输入反向的,输入时
2 3 2 4
存入时为
2 4 2 3
。
- 1