Uvalive 4256 (dp), uvalive00006dp
A: A number ranging from 1 to n forms an undirected connected graph, giving the connectivity, and then giving a sequence of numbers, this sequence requires that neighboring points be equal or directly connected in the graph. You can modify at least several points in the sequence to meet the sequence requirements.
Question: f [I] [j] indicates the minimum modification point of the sequence composed of the first I arrays ending with the number j, then f [I] [j] = min {f [I] [j], f [I-1] [k] + (d [I]! = J)}. In this case, j = k or g [j] [k] = 1. Finally, f [len] [k] selects the maximum value after all the numbers.
#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", &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));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 = min(res, f[m][i]);printf("%d\n", res);}return 0;}