Test instructions: There are numbers from 1 to N to form an undirected connected graph, give the connectivity, and then give a sequence of numbers, asking the sequence that the adjacent points are either equal or directly connected in the graph, and ask at least several points in the sequence to make the sequence meet the requirements.
The f[i][j] represents a sequence of the first I arrays with a minimum modification point ending with a number J, then f[i][j] = Min{f[i][j],f[i-1][k] + (d[i]! = j)}, at which time j==k or g[j][k] = = 1. Last f[len][k] All the numbers go over and choose the maximum value.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;const int N = 205;const int INF = 0x3f3f3f3f;int N, m, G[n][n], D[n], F[n][n];int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &am P;n, &m); memset (g, 0, sizeof (g)), int A, b;for (int i = 0; i < m; i++) {scanf ("%d%d", &a, &b); G[a][b] = G[b] [A] = 1;} scanf ("%d", &m), for (int i = 1; I <= m; i++) scanf ("%d", &d[i]), memset (f, INF, sizeof (f)), and for (int i = 1; i < = N; i++) F[1][i] = (i = d[1]), for (int i = 2, I <= m; i++) for (int j = 1; J <= N; j + +) for (int k = 1; k <= N; k++) if (j = k | | g[j][k]) f[i][j] = min (F[i][j], F[i-1][k] + (J! = D[i]); int res = inf;for (int i = 1; I <= n; i++) res = mi N (Res, f[m][i]);p rintf ("%d\n", res);} return 0;}
Uvalive 4256 (DP)