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. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  2. 在MAC上安装虚拟机搭建Ubuntu开发环境

    由于工作需要,需要在LINUX环境搭建服务器,但是工作中使用的是MAC系统,只好用虚拟机来搭建LINUX服务器环境.下面记录介绍一下搭建步骤以供需要的人参考使用. 下载准备 虚拟机使用VMWare   ...

  3. 一个有趣的 SQL 查询(查询7天连续登陆)

    一个有趣的 SQL 查询 一个朋友有这样一个SQL查询需求: 有一个登录表(tmp_test),包含用户ID(uid)和登录时间(login_time).表结构如下: . row ********** ...

  4. shell脚本积累

    统计当前目录下文件夹的大小 for d in $(ls) do du -sh ./$d done 获取之前日期date  +"%Y%m%d" -d  "-n days&q ...

  5. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  6. NYOJ-171 聪明的kk AC 分类: NYOJ 2014-01-02 09:01 165人阅读 评论(0) 收藏

    #include<stdio.h> #define max(x,y) x>y?x:y int main(){ int num[22][22]={0}; int n,m; int x, ...

  7. C#枚举注释实例

    public enum 枚举名称    {        /// <summary>        /// 注释描述1        /// </summary>        ...

  8. The service ‘xxx’ configured for WCF is not registered with the Autofac container

    最近在使用autofac.wcf时,报如下异常: Exception Details: System.InvalidOperationException: The service 'xxx' conf ...

  9. Ignore files which are already versioned

    If you accidentally added some files which should have been ignored, how do you get them out of vers ...

  10. URAL 1244. Gentlemen (DP)

    题目链接 题意 : 给出一幅不完全的纸牌.算出哪些牌丢失了. 思路 : 算是背包一个吧.if f[j]>0  f[j+a[i]] += f[j];然后在记录一下路径. #include < ...