1156

求出每个联通块的黑白块数 然后再背包 二维的背包 要保证每个块都得取一个

写的有些乱。。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>ed[];
int x,y,flag,g,f[],o[],n,q[];
int co[],a[],b[],dp[][];
int vis[],p[][][];
void dfs(int u,int c)
{
int i;
if(co[u]==)
{
x++;
p[g][x][] = u;
}
else
{
y++;
p[g][y][] = u;
}
for(i = ; i < (int)ed[u].size() ; i++)
{
int v = ed[u][i];
if(co[v])
{
if(co[v]!=-c)
{
flag = ;
return ;
}
}
else
{
co[v] = -c;
dfs(v,-c);
}
}
}
void dfs2(int s,int v)
{
if(flag) return ;
int i;
if(s==)
{
if(v==)
{
flag = ;
return ;
}
else
return ;
}
for(i = ; i <= g ;i++)
{
if(!vis[i]&&s-a[i]>=&&dp[v-][s-a[i]])
{
f[i] = ;
vis[i] = ;
dfs2(s-a[i],v-);
if(flag) return;
f[i] = ;
vis[i] = ;
}
if(!vis[i]&&s-b[i]>=&&dp[v-][s-b[i]])
{
f[i] = ;
vis[i] = ;
dfs2(s-b[i],v-);
if(flag) return ;
vis[i] = ;
f[i] = ;
}
}
}
int main()
{
int m,i,j;
scanf("%d%d",&n,&m);
for(i = ; i <= m ; i++)
{
int u,v;
scanf("%d%d",&u,&v);
ed[u].push_back(v);
ed[v].push_back(u);
}
for(i = ; i <= *n ; i++)
{
if(!co[i])
{
g++;x=;y=;
co[i] = ;
dfs(i,);
a[g] = x;
b[g] = y;
}
if(flag)
break;
}
if(flag)
{
puts("IMPOSSIBLE\n");
return ;
}
else
{
dp[][] = ;
for(i = ; i <= g ; i++)
for(int k = g; k >= ; k--)
{
for(j = n ; j >= ; j--)
{
if(j>=a[i])
dp[k][j] = max(dp[k][j],dp[k-][j-a[i]]);
if(j>=b[i])
dp[k][j] = max(dp[k][j],dp[k-][j-b[i]]);
}
}
if(!dp[g][n])
puts("IMPOSSIBLE");
else
{
dfs2(n,g);
for(i = ; i <= g ; i++)
if(f[i])
{
if(f[i]==)
for(j = ; j <= a[i] ; j++)
{
printf("%d ",p[i][j][]);
q[p[i][j][]] = ;
}
else
for(j = ; j <= b[i] ; j++)
{
printf("%d ",p[i][j][]);
q[p[i][j][]] = ;
}
}
puts("");
for(i = ; i <= *n ;i++)
if(!q[i])
printf("%d ",i);
puts("");
}
}
return ;
}

1156. Two Rounds(dfs+背包)的更多相关文章

  1. ural 1156. Two Rounds

    1156. Two Rounds Time limit: 2.0 secondMemory limit: 64 MB There are two rounds in the Urals Champio ...

  2. 【BZOJ】1673: [Usaco2005 Dec]Scales 天平(dfs背包)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1673 bzoj翻译过来的c<=230不忍吐槽......................... ...

  3. P1021 邮票面值设计(dfs+背包dp)

    P1021 邮票面值设计 题目传送门 题意: 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15N+K≤15)种邮票的情况下 (假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大 ...

  4. 【bzoj4800】: [Ceoi2015]Ice Hockey World Championship dfs

    [bzoj4800]: [Ceoi2015]Ice Hockey World Championship N<=40所以如果直接dfs背包会TLE 考虑Meet-in-the-middle 如果把 ...

  5. Codeforce 221 div1

    A 只要打个表就能发现,1,6,8,9的所有排列就可以产生0~6的余数了... 所以...走不下去的时候一定要打表... #define rep(i,n) for(int i=0 ; i<(n) ...

  6. codevs2894、2837、1669、2503、3231

    6.25动态规划之背包回顾 2894 Txx考试  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Txx是一个 ...

  7. 【NOIP2018】 游记

    All ended? [day 0] 一点感觉没有,不过翘掉了早上的课(当然还有前三周的课),然后刚想睡一会儿,就被通知要上车了/难受 在车上玩了一会儿早上下的Super Mario(主要是早上刷了一 ...

  8. hdu3448 01背包+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound f ...

  9. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

随机推荐

  1. 微信小程序、应用号、订阅号、服务号、企业号小总结

    微信小程序是现在微信推出的一个新的项目,但是很多人都不是很清楚微信小程序是怎么一回事,不明白到底怎样分别微信小程序和别的公众号.订阅号等的区别,那么让小编来给你介绍一下. 微信小程序目前是内侧阶段,是 ...

  2. Careercup - Facebook面试题 - 5188884744896512

    2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...

  3. C#WinForm中显示实时时间:年/月/日 时/分/秒 星期X

    //加载窗体时 string weekstr = ""; private void Form22_Load(object sender, EventArgs e) { this.t ...

  4. BOM-字节序标记

    BOM——Byte Order Mark 字节序标记 首先是什么是字节序? 字节序:与二进制数据在机器存放位置相关的! 可分为两类: 1. 小端字节序: 低地址放低位数据. x86系列的计算机就使用这 ...

  5. maven学习心得整理

    maven的学习心得 已经接触了maven项目有一段时间了,开始时仅仅会使用,在使用中发现了它的强大和方便,于是决心研究一下: 首先,普及一下maven参数: -D:传入属性参数 -P:使用POM中指 ...

  6. Ext学习-前后交互模式介绍

    在前后台交互模式的介绍中,实际上就是Store中Proxy相关的内容,比如Ajax提交. 所以详细的文档请参考: Ext学习-基础概念,核心思想介绍   中关于数据模型和MVC结构部分. 作者:sdj ...

  7. 2012 Asia Hangzhou Regional Contest

    Friend Chains http://acm.hdu.edu.cn/showproblem.php?pid=4460 图的最远两点距离,任意选个点bfs,如果有不能到的点直接-1.然后对于所有距离 ...

  8. SQL Server 之 事务隔离级别

    SET TRANSACTION ISOLATION LEVEL xxx  -- 每次设置只针对当前事务块 xxx 取值: READ UNCOMMITTED READ COMMITTED REPEATA ...

  9. HTML/CSS中常遇到的bug 一些注意事项总结

    1.IE6下横向双倍margin bug (触发条件:块属性标签:float:横向margin设置:IE6下.解决办法:css中加入display:inline.) 2.css中公用属性首先声明:如对 ...

  10. 高性能网络编程1----accept建立连接

    转 http://taohui.org.cn/tcpperf1.html  陶辉 taohui.org.cn 回到应用层,往往只需要调用类似于accept的API就可以建立TCP连接.建立连接的流程大 ...