2019ICPC上海K-Color Graph(二分图 状压枚举)

简介: 2019ICPC上海K-Color Graph(二分图 状压枚举)

linkkk

题意:

给出n点m边的简单无向图,每条边最开始是白色的。每一轮都可以将一条白边染成红色,要求最后不能有红色的奇环,求最多能够染的边数。

思路:

核心在于二分图的性质:二分图不存在长度为奇数的环,因为每一条边都是从一个集合走到另一个集合,必须要走奇数次。

这样的话问题就转化成了求最多可以选择多少条边使得选择出来的边为二分图。

可以看到n只有16,可以状压枚举所有情况,对于具体的每一种情况,如果该位为1的话,假设在左边集合里,否则在右边集合里。然后再枚举给出的m条边,如果端点的所在的集合不同,说明该边为二分图的一条边。最后取最大值即可。

总结:最重要的就是能够联想到二分图&看到数据范围想到可以状压枚举所有情况。

代码:

// Problem: Color Graph
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/4370/K
// Memory Limit: 524288 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
#define read read()
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int maxn=4e5+7,maxm=1e6+7,mod=1e9+7;
int n,m,vis[55];
PII e[55];
int main(){
  int _=read,Case=1;
  while(_--){
    int n=read,m=read;
    rep(i,1,m){
      int u=read-1,v=read-1;
      e[i]={u,v};
    }
    int ans=0;
    for(int i=0;i<(1<<n);i++){
      for(int j=0;j<n;j++){
        if(i&(1<<j)) vis[j]=1;
        else vis[j]=0;
      }
      int now=0;
      rep(i,1,m){
        int u=e[i].first,v=e[i].second;
        if(vis[u]^vis[v]) now++;
      }
      ans=max(ans,now);
    }
    printf("Case #%d: %d\n",Case++,ans);
  }
  return 0;
}
目录
相关文章
|
10月前
2021山东省赛 M . Matrix Problem(构造)
2021山东省赛 M . Matrix Problem(构造)
40 0
|
11月前
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1053 Path of Equal Weight
【PAT甲级 - C++题解】1053 Path of Equal Weight
64 0
(数学)(必要解题步骤)2021icpc上海D. Strange Fractions
(数学)(必要解题步骤)2021icpc上海D. Strange Fractions
47 0
LeetCode 75 Sort Colors 颜色分类(荷兰国旗)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
70 0
|
安全
【CCCC】L3-009 长城 (30分),计算几何+凸包,极角排序
【CCCC】L3-009 长城 (30分),计算几何+凸包,极角排序
117 0
2021杭电多校第八场 HDU7063-Square Card(求两圆相交面积)
2021杭电多校第八场 HDU7063-Square Card(求两圆相交面积)
57 0
2021杭电多校第八场 HDU7063-Square Card(求两圆相交面积)
|
BI
2022年牛客多校第2场 J . Link with Arithmetic Progression (三分+枚举)
2022年牛客多校第2场 J . Link with Arithmetic Progression (三分+枚举)
95 0
[USACO | UPC] Liars and Truth Tellers | 拓展域并查集
题目描述 After spending so much time around his cows, Farmer John has started to understand their language. Moreover, he notices that among his N cows (2 ≤ N ≤ 1000 ), some always tell the truth while others always lie.
109 0
|
人工智能
[CCPC] 2017秦皇岛H Prime Set | 二分图最大匹配 [据说是个金牌题]
题意: 给出n个数,如果两数之和a i + a j a_i+a_ja i ​ +a j ​ (i ≠ j i \neq ji  ​ =j)为素数,那么这两个数的下标组成的集合{ i , j } {\{i,j}\}{i,j} 问最多挑选k个这样的集合,集合最大的大小是多少 思路: 将给出的n个数进行暴力两两求和,看两数之和是否为素数,将能够构成素数的两个数的下标连一条边 开始将match数组标记为− 1 -1−1,如果能够构成素数,那么将这个数标记为0 00 然后进行二分图最大匹配,如果说匹配的个数 ≥ k \ge k≥k 那么答案就是k ∗ 2 k*2k∗2
111 0