无向图的度
图中每一个顶点的度定义为以该项点为一个端点的边的数目
#include <cstdio>const int MAXN = 100;int degree[MAXN] = { 0 };int main() {int n, m, u, v;scanf("%d%d", &n, &m);//在输出边度的时候就已经表示度的数目了,所以用一个degree(hash思想进行直接 ) for (int j = 0; j < m; j++) { scanf("%d%d", &u, &v); degree[u]++; degree[v]++; }for (int i = 0; i < n; i++) { printf("%d", degree[i]); if (i < n - 1) { printf(" "); }}return 0;
}
有向图的度
//有向图的入度数和出度数
#include <cstdio>const int MAXN = 100;
int inDegree[MAXN] = { 0 };//入度的条数
int outDegree[MAXN] = { 0 };//出度的条数int main() {int n, m, u, v;scanf("%d%d", &n, &m);//n个顶点,m条边for (int j = 0; j < m; j++) {scanf("%d%d", &u, &v);outDegree[u]++;inDegree[v]++;}for (int i = 0; i < n; i++) {printf("%d %d\n", inDegree[i], outDegree[i]);}return 0;
}