有向图邻接表节点入度代码
时间: 2023-12-07 07:40:04 浏览: 104
邻接表代码
4星 · 用户满意度95%
以下是有向图邻接表节点入度的代码实现:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1005;
struct Edge {
int v, nxt;
} e[MAXN];
int head[MAXN], inDegree[MAXN], cnt;
void addEdge(int u, int v) {
e[++cnt].v = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
int main() {
memset(head, -1, sizeof(head));
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
addEdge(u, v);
inDegree[v]++;
}
for (int i = 1; i <= n; i++) {
cout << "节点" << i << "的入度为:" << inDegree[i] << endl;
}
return 0;
}
```
阅读全文