Compute the degree of each vertex in the graph. If all vertices have the same degree, then the graph is regular; otherwise, it is not.
Example
The graphs shown in the examples have the following form:
In the first graph, all vertices have degree , so it is regular. In the second example, the vertices have different degrees; therefore, this graph is not regular.
Algorithm implementation
Store the degrees of the vertices in the array .
#define MAX 110
int deg[MAX];Read the input data.
scanf("%d %d",&n,&m);
memset(deg,0,sizeof(deg));
for(i = 0; i < m; i++)
{For each undirected edge , increase the degrees of vertices and by .
scanf("%d %d",&a,&b);
deg[a]++; deg[b]++;
}Next, check whether all values in the array are equal. If all values are the same (i.e., all vertices have equal degrees), then flag = 0, and the graph is regular.
flag = 0;
for(i = 1; i < n; i++)
if (deg[i] != deg[i+1]) flag = 1;Depending on the value of the variable , print the answer.
if (flag == 0) printf("YES\n");
else printf("NO\n");