Time Limit: 0.25 s
Space limit: 4 m
Question:
M different edges exist in the undirected completely graph of N (1 <= n <= 200) points. select no more than (m + 1) or two edges, so that any two points can reach each other through no more than three edges.
Solution:
Divide the edges of a undirected full graph into two parts. Therefore, the shortest distance of some vertices must not exceed 3.
No good proof... -_-!
Code
1 ~ M/2 is a group, M/2 + 1 ~ M is a group, and Floyd determines whether the first group meets the requirements. If the first group is output, the second group is not output.
#include <cstdio>const int INF = 300 + 9;int g[INF][INF], f[INF][INF];int n, m, i, j, k, t;int main() {scanf ("%d%d", &n, &m);t = (m + 1) >> 1;for (i = 1; i <= n; ++i)for (j = 1; j <= n; ++j) {scanf ("%d", g[i] + j);f[i][j] = g[i][j] <= t ? 1 : INF;}for (k = 1; k <= n; ++k)for (i = 1; i < n; ++i)for (j = i + 1; j <= n; ++j)if (f[i][k] + f[j][k] < f[i][j])f[i][j] = f[j][i] = f[i][k] + f[j][k];for (i = 1; i < n; ++i)for (j = i + 1; j <= n; ++j)if (f[i][j] > 3) {printf ("%d\n", m - t);for (++t; t <= m; ++t) printf ("%d ", t);putchar (10);return 0;}printf ("%d\n", t);for (i = 1; i <= t; ++i) printf ("%d ", i);putchar (10);return 0;}