2016 Multi-University Training Contest 1 Necklace 环排+二分匹配
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5727
题意:由2*N颗宝石构成的环(阴阳宝石均为N颗且标号均从1~N) 之后给定M组 a,b;表示阳宝石a若和阴宝石b相邻会使得阳宝石变暗,问所构成的环中阳宝石变暗的最少数量?
其中(1<=N<=9, 1<= M <= N*N)
思路:确定一个阴宝石(我代码中让1不动),其余的环排。
将空隙变成一个点,先将所夹空隙的两点与阳宝石每点建图之后跑匈牙利即可,这跑出来的是最大匹配数(即最多不变暗的阳宝石数);
稍加剪枝还是容易过的;8! = 40320
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define A first
#define B second
#define MK make_pair
#define esp 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define bitnum(a) __builtin_popcount(a)
#define clear0 (0xFFFFFFFE)
#define mod 1000000007
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
template<typename T>
void read1(T &m)
{
T x = ,f = ;char ch = getchar();
while(ch <'' || ch >''){ if(ch == '-') f = -;ch=getchar(); }
while(ch >= '' && ch <= ''){ x = x* + ch - '';ch = getchar(); }
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
inline ll gcd(ll a,ll b){ return b == ? a: gcd(b,a%b); }
template<class T1, class T2> inline void gmin(T1& a,T2 b) { if(a > b) a = b; }
template<class T1, class T2> inline void gmax(T1& a,T2 b) { if(a < b) a = b; }
int S[][], f[], n;
int vis[], girl[], line[][];
int dfs(int p)
{
rep1(i,,n){
if(line[p][i] && !vis[i]){
vis[i] = ;
if(!girl[i] || dfs(girl[i])){
girl[i] = p;
return ;
}
}
}
return ;
} void init()
{
MS0(girl);MS0(line);
rep1(i,,n) rep1(j,,n) if(!S[i][f[j]] && !S[i][f[j+]]) line[i][j] = ;
} int solve()
{
init(); //建边
int ans = ;
rep1(i,,n){
MS0(vis);
if(dfs(i)) ans++;
}
return n-ans;
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int m, a, b;
while(scanf("%d%d",&n, &m) == ){
MS0(S);
rep1(i,,m){
read2(a,b);
S[a][b] = ;
}
rep1(i,,n) f[i] = i; f[n+] = ;
int ans = inf;
do{
gmin(ans, solve());
}while(next_permutation(f+,f+n+) && ans);
printf("%d\n",ans);
}
return ;
}
2016 Multi-University Training Contest 1 Necklace 环排+二分匹配的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分
题目链接:http://codeforces.com/problemset/gymProblem/101028/I I. March Rain time limit per test 2 second ...
- HDU 5727 Necklace 环排+二分图匹配
这是从山东大学巨巨那里学来的做法 枚举下黑色球的排列总数是8!,然后八个白球可选的位置与左右两个黑球存不存在关系建图就行 这是原话,具体一点,每次生成环排,只有互不影响的才连边 最后:注重一点,n个数 ...
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 2016 Al-Baath University Training Camp Contest-1 E
Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 2016 Multi-University Training Contest 1 GCD【RMQ+二分】
因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,23333,估计看题人真的是觉得博主就是个撒缺.废话不多说了,补题... update////2 ...
- 2016 Al-Baath University Training Camp Contest-1 J
Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- 2016 Al-Baath University Training Camp Contest-1 H
Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...
随机推荐
- css笔记01:CSS例子
body { margin:0; padding:0; background:#000 url('images/backgrounds/star.png') no-repeat fixed; font ...
- 关于 ES6箭头函数
转自 http://simplyy.space/article/577c5b0dcbe0a3e656c87c24 多个连续的箭头函数与柯里化 高阶函数 高阶函数定义:将函数作为参数或者返回值是函 ...
- 创建FILE GEODATABASE 和栅格目录及向栅格目录中添加影像
using System;using System.IO;using ESRI.ArcGIS.DataSourcesGDB;using ESRI.ArcGIS.DataSourcesRaster;us ...
- Uva120 Stacks of Flapjacks 翻煎饼
水水题.给出煎饼数列, 一次只能让第一个到第i个数列全部反转,要求把数列排序为升序. 算法点破后不值几钱... 只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了.递归或循环即 ...
- win10 IIS10 HTTP 错误 404.2 - Not Found
环境win10系统IIS10里边发布web应用程序的时候,出现 HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页 ...
- [改善Java代码]使用静态内部类提高封装性
建议38: 使用静态内部类提高封装性 Java中的嵌套类(Nested Class)分为两种:静态内部类(也叫静态嵌套类,Static Nested Class)和内部类(Inner Class).内 ...
- [改善Java代码]别让null值和空值威胁到变长方法
建议5:别让null值和空值威胁到变长方法 public class Client { public void methodA(String str,Integer... is){ } public ...
- .NET 认识
- MyBatis(3.2.3) - Cache
Caching data that is loaded from the database is a common requirement for many applications to impro ...
- WM_NCCALCSIZE消息处理详解
[前言]指定应用程序的标题高度和边框的宽度的方法有很多种.其中最普遍的方法有下面的两种:第一种:创建没有标题栏应用程序,在客户区让出一部分空间用一幅图片画一个标题栏,让人“误认为”是标题栏.第二种:处 ...