vector<bool> visited;;visited = vector<bool> my(numCourses,false);这两句C++代码哪里有错?
时间: 2024-04-09 11:29:06 浏览: 20
这两句代码中有两个错误。首先,分号应该在第一行的末尾而不是第二行。其次,赋值语句应该是 `visited = vector<bool>(numCourses, false);` 而不是 `visited = vector<bool> my(numCourses, false);`。正确的代码应该是这样的:
```cpp
vector<bool> visited;
visited = vector<bool>(numCourses, false);
```
这样就可以创建一个名为 `visited` 的大小为 `numCourses` 的布尔类型向量,并将其所有元素初始化为 `false`。
相关问题
#include <iostream>#include <vector>#include <stack>using namespace std;void dfs(int u, vector<vector<int>>& graph, vector<bool>& visited, vector<int>& farthest) { stack<int> s; s.push(u); visited[u] = true; while (!s.empty()) { int v = s.top(); s.pop(); for (int i = 0; i < graph[v].size(); i++) { int w = graph[v][i]; if (!visited[w]) { visited[w] = true; s.push(w); farthest[w] = farthest[v] + 1; } } }}int main() { int n, m; cin >> n >> m; vector<vector<int>> graph(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; graph[u].push_back(v); } vector<int> farthest(n, 0); for (int i = 0; i < n; i++) { vector<bool> visited(n, false); dfs(i, graph, visited, farthest); } for (int i = 0; i < n; i++) { cout << "Node " << i << " can reach farthest node " << farthest[i] << endl; } return 0;}不用STL实现这个代码
不使用STL实现该代码,可以手动实现栈和vector。
下面是C++实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 10005;
int stack[MAXN], top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() {
return stack[top--];
}
bool empty() {
return top == -1;
}
void dfs(int u, int graph[][MAXN], bool visited[], int farthest[]) {
push(u);
visited[u] = true;
while (!empty()) {
int v = pop();
for (int i = 0; i < MAXN; i++) {
if (graph[v][i] && !visited[i]) {
visited[i] = true;
push(i);
farthest[i] = farthest[v] + 1;
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
int graph[MAXN][MAXN] = {0};
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
graph[u][v] = 1;
}
int farthest[MAXN] = {0};
for (int i = 0; i < n; i++) {
bool visited[MAXN] = {false};
dfs(i, graph, visited, farthest);
}
for (int i = 0; i < n; i++) {
cout << "Node " << i << " can reach farthest node " << farthest[i] << endl;
}
return 0;
}
```
在这个实现中,我们手动实现了栈,并使用数组来代替vector。同时,我们使用二维数组来表示邻接矩阵,而不是vector<vector<int>>来表示邻接表。
其他部分的实现与使用STL的版本相同。
#include <iostream> #include <vector> #include <queue> using namespace std; // 定义图的边结构体 struct Edge { int to; // 边的终点 int weight; // 边的权重 Edge(int t, int w) : to(t), weight(w) {} }; // 定义图的邻接表结构体 struct Graph { int n; // 节点数 vector<vector<Edge>> adj; // 邻接表 Graph(int n) : n(n), adj(n) {} // 添加一条边 void addEdge(int from, int to, int weight) { adj[from].push_back(Edge(to, weight)); } // 深度优先遍历 void dfs(int cur, vector<bool>& visited) { visited[cur] = true; cout << cur << " "; for (auto e : adj[cur]) { if (!visited[e.to]) { dfs(e.to, visited); } } } // 广度优先遍历 void bfs(int cur, vector<bool>& visited) { queue<int> q; q.push(cur); visited[cur] = true; while (!q.empty()) { int u = q.front(); q.pop(); cout << u << " "; for (auto e : adj[u]) { if (!visited[e.to]) { visited[e.to] = true; q.push(e.to); } } } } }; int main() { int n, m; cin >> n >> m; Graph g(n); for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; g.addEdge(u, v, w); g.addEdge(v, u, w); // 无向图需要添加反向边 } // 深度优先遍历 cout << "DFS: "; vector<bool> visited(n, false); g.dfs(0, visited); cout << endl; // 广度优先遍历 cout << "BFS: "; visited.assign(n, false); g.bfs(0, visited); cout << endl; return 0; }给代码添加注释
```
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// 定义图的边结构体
struct Edge {
int to; // 边的终点
int weight; // 边的权重
Edge(int t, int w) : to(t), weight(w) {} // 构造函数
};
// 定义图的邻接表结构体
struct Graph {
int n; // 节点数
vector<vector<Edge>> adj; // 邻接表
Graph(int n) : n(n), adj(n) {} // 构造函数
// 添加一条边
void addEdge(int from, int to, int weight) {
adj[from].push_back(Edge(to, weight));
}
// 深度优先遍历
void dfs(int cur, vector<bool>& visited) {
visited[cur] = true;
cout << cur << " ";
for (auto e : adj[cur]) {
if (!visited[e.to]) {
dfs(e.to, visited);
}
}
}
// 广度优先遍历
void bfs(int cur, vector<bool>& visited) {
queue<int> q;
q.push(cur);
visited[cur] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
cout << u << " ";
for (auto e : adj[u]) {
if (!visited[e.to]) {
visited[e.to] = true;
q.push(e.to);
}
}
}
}
};
int main() {
int n, m;
cin >> n >> m;
Graph g(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
g.addEdge(u, v, w);
g.addEdge(v, u, w); // 无向图需要添加反向边
}
// 深度优先遍历
cout << "DFS: ";
vector<bool> visited(n, false);
g.dfs(0, visited);
cout << endl;
// 广度优先遍历
cout << "BFS: ";
visited.assign(n, false);
g.bfs(0, visited);
cout << endl;
return 0;
}
```
注释已添加在代码中。