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. When to use Class.isInstance() & when to use instanceof operator?

    I think the official documentation gives you the answer to this one (albeit in a fairly nonspecific ...

  2. js eval()执行传参函数的写法

    .cs public class Message<T> { // 数据总数 public int? Total { get; set; } // 关键数据 public List<T ...

  3. Cookie 获取

    二级域名可以获取一级域名的Cookie值 二级域名下删除顶级域名下的Cookie,需要添加顶级域名的Cookie作用域 /// <summary> /// 根据cookie名称删除 /// ...

  4. BAT CMD 批处理文件脚本 -2

    http://checheng1988.blog.51cto.com/4725808/1090733 在很多windows程序中会见到很多用扩展名为.bat和.cmd结尾的文件,那么这些文件能干什么呢 ...

  5. c++ 常用数据接口 set

    #include <set> #include <iostream> #include <string> int main(void) { std::set< ...

  6. SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式

    前言: spring容器以xml的形式注入bean,然后可以在类中获取,获取的形式主要有二种:第一种最简单--采用@Resource 或@Autowired关键字在加载spring文件时将bean注入 ...

  7. 剑指offer--面试题14--收获

    按照作者的说法,作为应届毕业生的我来说,如果能写出初级程序员的参考代码来解决面试题14就可认为过关了... 参考代码如下: void ReorderOddEven_1(int *pData, unsi ...

  8. java递归查询方法

    一.需求 项目里要让用户能够设置所选择教材的章课节,以针对章课节提供相应的题目供用户做题. 设计:用户设置了教材后,首次登录,进行章节设置时.默认为用户选择第一章.第一课.第一节. 思路:用户访问页面 ...

  9. CentOS(RHEL) 操作备忘

    1.安装中文语言包及切换 yum groupinstall chinese-support vi /etc/sysconfig/i18n change en_US to zh_CN 2.用户自动登录 ...

  10. Oracle RAC 常用维护工具和命令

    Oracle RAC 常用维护工具和命令 分类: Oracle Basic Knowledge Oracle RAC2010-03-09 01:02 13987人阅读 评论(6) 收藏 举报 orac ...