hdu5727(全排列+二分图)

orz多校题真的是rbq。。

这个题也想到二分图了。。不过窝的建法没法保持项链是联通的。。所以gg。。

然后正解是枚举出阴珠子的全排列,然后让空位和阳珠子进行匹配就完了。。

仔细想想其实这题真的不难。。只是练得太少了qaq



/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 30
#define nm 100
#define pi 3.1415926535897931
using namespace std;
const ll inf=998244353;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}


struct edge{int t;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
int n,m,v[NM],match[NM],b[NM],a[NM][NM],ans,_x,_y;
bool _v[NM];

bool dfs(int x){
	link(x)if(v[j->t]!=_x){
		v[j->t]=_x;
		if(!match[j->t]||dfs(match[j->t])){match[j->t]=x;return true;}
	}
	return false;
}

void check(){
	mem(e);mem(h);o=e;mem(match);mem(v);int cnt=0;
	inc(i,1,n)inc(j,1,n)if(!a[i][b[j]]&&!a[i][b[j+1]])add(i,j+n);
	inc(i,1,n)if(dfs(_x=i))cnt++;
	ans=max(ans,cnt);
}

void _dfs(int x){
	if(x==n+1){check();return;}
	inc(j,2,n)if(!_v[j]){
		_v[j]++;b[x]=j;
		_dfs(x+1);
		_v[j]=false;
	}
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		ans=0;mem(_v);mem(b);mem(a);
		inc(i,1,m){_x=read();_y=read();a[_x][_y]++;}
		b[n+1]=b[1]=1;_v[1]++;
		_dfs(2);
		printf("%d\n",n-ans);
	}
	return 0;
}






Necklace

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3559    Accepted Submission(s): 1090


Problem Description
SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang energy. SJX wants to make a necklace with these magic gems for his beloved BHB. To avoid making the necklace too Yin or too Yang, he must place these magic gems Yin after Yang and Yang after Yin, which means two adjacent gems must have different kind of energy. But he finds that some gems with Yang energy will become somber adjacent with some of the Yin gems and impact the value of the neckless. After trying multiple times, he finds out M rules of the gems. He wants to have a most valuable neckless which means the somber gems must be as less as possible. So he wonders how many gems with Yang energy will become somber if he make the necklace in the best way.
 

Input
  Multiple test cases.

  For each test case, the first line contains two integers N(0N9),M(0MNN), descripted as above.

  Then M lines followed, every line contains two integers X,Y, indicates that magic gem X with Yang energy will become somber adjacent with the magic gem Y with Yin energy.
 

Output
One line per case, an integer indicates that how many gem will become somber at least.
 

Sample Input
 
  
2 1 1 1 3 4 1 1 1 2 1 3 2 1
 

Sample Output
 
  
1 1
 

Author
HIT
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   6275  6274  6273  6272  6271 
 

Statistic |  Submit |  Discuss | Note

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/79981823
今日推荐