Let's give you an undirected graph with each vertex degree of k (k is an even number) and ask if we can dye n edges in the graph so that each vertex has two colored edges. That is to say, is there a subgraph with n edges in the source image, so that the degree of each vertex is 2? Think about it. The degree of each vertex is 2, which is actually the minimum ring coverage of the source image. The method for least ring coverage is to first obtain the directed Euler Loop (k is an even number, and the Euler loop must exist) of the source image ), then the question is transformed into whether n edges in the Euler loop can be selected to overwrite all vertices? Isn't this converted into the minimum path overwrite of the DAG!
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<fstream> #include<sstream> #include<bitset> #include<vector> #include<string> #include<cstdio> #include<cmath> #include<stack> #include<queue> #include<stack> #include<map> #include<set> #define FF(i, a, b) for(int i=a; i<b; i++) #define FD(i, a, b) for(int i=a; i>=b; i--) #define REP(i, n) for(int i=0; i<n; i++) #define CLR(a, b) memset(a, b, sizeof(a)) #define debug puts("**debug**") #define LL long long #define PB push_back using namespace std; const int maxn = 1001; int g[maxn][maxn], degree[maxn], match[maxn], id[maxn][maxn]; bool vis[maxn]; int n, k, u, v; void Euler() { FF(i, 1, n+1) if(degree[i]) { int u = i; while(true) { FF(j, 1, n+1) if(g[u][j] && g[j][u]) { g[j][u] = 0; degree[u]--, degree[i]--; u = j; break; } if(u == i) break; } } } bool dfs(int u) { FF(i, 1, n+1) if(!vis[i] && g[u][i]) { vis[i] = true; if(match[i] == 0 || dfs(match[i])) { match[i] = u; return true; } } return false; } bool max_match() { CLR(match, 0); FF(i, 1, n+1) { CLR(vis, 0); if(!dfs(i)) return false; } return true; } int main() { while(~scanf("%d%d", &n, &k)) { CLR(degree, 0);CLR(g, 0); REP(i, n*k/2) { scanf("%d%d", &u, &v); g[u][v] = g[v][u] = 1; id[u][v] = id[v][u] = i+1; degree[u]++, degree[v]++; } Euler(); if(max_match()) { puts("YES"); FF(i, 1, n+1) printf("%d\n", id[match[i]][i]); } else puts("NO"); } return 0; }