Question:
It is necessary to build roads between N cities so that any two cities can reach and there are no more than two roads. In addition, some cities cannot build roads.
Ideas:
To connect all N cities, we thought it was the smallest spanning tree problem at the beginning, which is actually a simple question. It is required that there be no more than two roads between two cities, so all cities should be connected to one point. As for this point, it is very easy to find, you only need to find a path that is not restricted by other points.
//cf 192 B#include <stdio.h>#include <string.h>char map[1005][1005];int main(){ int n, m; while (scanf("%d %d", &n, &m) != EOF) { int s, e; memset(map, 0, sizeof(map)); for (int i = 1; i <= m; i++) { scanf("%d %d", &s, &e); map[s][e] = 1; map[e][s] = 1; } int x; for (int i = 1; i <= n; i++) { int flag = 1; for (int j = 1; j <= n; j++) { if (map[i][j]) { flag = 0; break; } } if (flag) { x = i; break; } } printf("%d\n", n-1); for (int i = 1; i <= n; i++) { if (x == i) continue; else printf("%d %d\n", x, i); } } return 0;}