以下代码实现了一个无向图的邻接表存储结构,关于该代码的说法正确的是:
#include <vector>
#include <stdexcept>
using namespace std;
class UndirectedGraph {
private:
int vertexCount;
vector<vector<int>> adjList;
public:
UndirectedGraph(int n) : vertexCount(n), adjList(n) {}
void addEdge(int u, int v) {
if (u < 0 || u >= vertexCount || v <0 || v >= vertexCount) {
throw out_of_range("顶点编号越界");
}
adjList[u].push_back(v);
adjList[v].push_back(u);
}
int getDegree(int u) {
return adjList[u].size();
}
};