- 枚举二分图
Color Graph
题目让给无向图边刷红色,不允许奇环全红,相当于删除边,使得图中只有偶环,只有偶环即2分图,看题目数据范围容易推得是枚举题。
于是这一题就是枚举二分图左边的点
code:来自某大佬的博客:https://www.cnblogs.com/ucprer/p/11931263.html
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int maxm = 1e4 + 5;
struct edge
{
int u, v;
} E[maxm];
int tot = 0;
void addedge(int u, int v)
{
E[++tot].u = u;
E[tot].v = v;
}
int color[maxn];
int main()
{
int T;
cin >> T;
for(int kase = 1; kase <= T; kase++)
{
int n, m;
scanf("%d%d", &n, &m);
fill(color, color + 1 + n, 0);
tot = 0;
for(int i = 1; i <= m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v);
}
int ans = 0;
for(int meijv = 0; meijv <= (1 << n) - 1; meijv++)
{
int mj = meijv;
for(int i = 1; i <= n; i++)
{
if(mj & 1)
{
color[i] = 1;
}
else color[i] = 0;
mj >>= 1;
}
int res = 0;
for(int i = 1; i <= tot; i++)
{
if(color[E[i].u] != color[E[i].v])
{
res++;
}
}
ans = max(ans, res);
}
printf("Case #%d: %d\n", kase, ans);
}
}
来源:CSDN
作者:妖怪无面男
链接:https://blog.csdn.net/qq_40192591/article/details/104355682