1、给定一个50个节点的无向图,有$m$条边。现在以任意一种序列对每个节点染色。染当前节点的代价为染色完当前节点后满足两个端点都被染色的边的数量。求最小的染色代价。$m \leq 20$

思路:一个直观的思路是应该先染色度数小的节点。由于$m\leq 20$,所以如果先把那些孤立的点以及那些度数为1但是其相连的另一端还未染色的节点先染色,那么剩下还未染色的节点数不会超过20。而且已经染色的节点代价都为0.令$c_{s}$表示将状态为$s$的节点全部染色时最后一步的代价。$f_{i}$表示将状态$i$全部染色的代价,那么$f_{i}=min(f_{t}+c_{i})$,其中$t=i$^$2^{j}$且$i$&$2^{j}\neq 0$,即最后染色节点$j$

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <set>
#include <vector>
#include <time.h>
#include <queue>
#include <stack>
#include <map>
#include <assert.h>
using namespace std; const int N=50; int h[N];
int a[N],aNum;
int d[N];
int cost[1<<23];
int dp[1<<23]; int GetCost(const int Mask,const vector<int>& Ex,const vector<int>& Ey)
{
int p[N];
memset(p,0,sizeof(p));
for(int i=0;i<50;++i)
{
if(h[i]) p[i]=1;
else if(Mask&(1<<a[i])) p[i]=1;
}
int nNum=0;
const int m=(int)Ex.size();
for(int i=0;i<m;++i)
{
if(p[Ex[i]]&&p[Ey[i]]) ++nNum;
}
return nNum;
} class Gperm
{
public:
int countfee(vector<int> x,vector<int> y)
{
const int m=(int)x.size();
const int n=50;
for(int i=0;i<m;++i) ++d[x[i]],++d[y[i]];
for(int i=0;i<n;++i)
{
if(0==d[i]) h[i]=1;
else if(1==d[i])
{
int j;
for(int k=0;k<m;++k)
{
if(x[k]==i) j=y[k];
else if(y[k]==i) j=x[k];
}
if(!h[j]) h[i]=1;
}
}
for(int i=0;i<n;++i) if(!h[i]) a[i]=aNum++;
for(int i=0;i<(1<<aNum);++i) cost[i]=GetCost(i,x,y); memset(dp,-1,sizeof(dp));
dp[0]=0;
for(int i=1;i<(1<<aNum);++i)
{
for(int j=0;j<aNum;++j) if(i&(1<<j))
{
int tmp=dp[i^(1<<j)]+cost[i];
if(dp[i]==-1||tmp<dp[i]) dp[i]=tmp;
}
}
return dp[(1<<aNum)-1]; }
};

2、给定一个$n$个节点的无向图,其中有些边是不确定的,可能有也可能没有。这样的边有$k$条。所以不同的图有 $2^{k}$种。第$i$种图的的最大团的大小为$f_{i}$,求所有$f_{i}$的和。

思路:枚举每一种图计算其最大团。首先枚举最大团中包含的最小顶点$x$,那么剩下的点的选择范围一定是$[x+1,n-1]$中跟$x$有边的点,设这些组成的集合为$S_{1}$。那么从小到大枚举$S_{1}$中的点作为最大团中的点,然后会得到新的集合$S_{2}$,依次类推。

#include <stdio.h>
#include <string>
#include <stack>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std; class Clicounting
{
int nBestAns;
int n;
long long a[50]; int Get(long long x)
{
int cnt=0;
while(x)
{
++cnt;
x=x&(x-1);
}
return cnt;
} int dfs(long long Mask,int nCurSize)
{
if(Mask==0)
{
if(nCurSize>nBestAns)
{
nBestAns=nCurSize;
return 1;
}
return 0;
}
int Pre=0;
while(Mask!=0)
{
if(nCurSize+Get(Mask)<=nBestAns) return 0;
while(Pre<n&&!(Mask&(1ll<<Pre))) ++Pre;
Mask^=1ll<<Pre;
if(dfs(Mask&a[Pre],nCurSize+1)) return 1;
}
return 0;
} public:
int count(vector<string> g)
{
n=(int)g.size();
vector<pair<int,int> > V;
int mp[50][50];
for(int i=0;i<n;++i) for(int j=i+1;j<n;++j)
{
if(g[i][j]=='?')
{
V.push_back(make_pair(i,j));
mp[i][j]=mp[j][i]=(int)V.size()-1;
}
}
int nAns=0;
const int M=(int)V.size();
for(int i=0;i<(1<<M);++i)
{
memset(a,0,sizeof(a));
for(int x=0;x<n;++x) for(int y=x+1;y<n;++y)
{
if(g[x][y]=='1') a[x]|=1ll<<y,a[y]|=1ll<<x;
else if(g[x][y]=='?')
{
if(i&(1<<mp[x][y])) a[x]|=1ll<<y,a[y]|=1ll<<x;
}
}
nBestAns=0;
for(int x=n-1;x>=0;--x)
{
long long b=0;
for(int y=x+1;y<n;++y) b|=1ll<<y;
dfs(b&a[x],1);
}
nAns+=nBestAns;
}
return nAns;
}
};

  

topcoder srm 696 div1 -3的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  3. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  4. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  5. Topcoder SRM 602 div1题解

    打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...

  6. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  7. Topcoder SRM 584 DIV1 600

    思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...

  8. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

  9. topcoder srm 575 div1

    problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...

随机推荐

  1. 读写App.config配置文件的方法

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  2. 【AngularJS】解决ng-if中的ng-model值无效的问题(转)

    from:http://blog.csdn.net/u013451157/article/details/60866210 与其他指令一样,ng-if指令也会创建一个子级作用域,因此,如果在ng-if ...

  3. mysql字符集问题,及排序规则

    字符集问题: 基本概念 • 字符(Character)是指人类语言中最小的表义符号.例如’A'.’B'等:• 给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符,这一数值就是字符的编码(E ...

  4. django 网站的搭建(1)

    使用 python django 模块来搭建自己的博客网站. 本人环境:阿里云centos7+django1.10+python3.5 使用工具:putty + winscp 1.首先安装python ...

  5. C#基本知识零散总结

    C#基本知识零散总结 类的属性:(property) C#中定义属性使用的语法 public string SomeProperty { get { return "属性的值"; ...

  6. 【Hadoop学习之八】MapReduce开发

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 伪分布式:HDFS和YARN 伪分 ...

  7. python实现堆栈和队列

    利用python列表实现堆栈和队列 堆栈: 堆栈是一个后进先出的数据结构,其工作方式就像生活中常见到的直梯,先进去的人肯定是最后出. 我们可以设置一个类,用列表来存放栈中的元素的信息,利用列表的app ...

  8. 使用函数式编程消除重复无聊的foreach代码(Scala示例)

    摘要:使用Scala语言为例,展示函数式编程消除重复无聊的foreach代码. 难度:中级 概述 大多数开发者在开发生涯里,会面对大量业务代码.而这些业务代码中,会发现有大量重复无聊的 foreach ...

  9. 设计模式之Adapter(适配器)(转)

    定义: 将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份. 为何使用? 我们经常碰到要将两个没有关系的类组合在一起使用,第一解决方案是 ...

  10. 环绕声5.1ch

    简单说5.1ch就是数字影院中的音频输出术语,环绕立体声输出,让人有置身电影院的感觉,由五个音箱(两个主音箱.两个环绕箱.一个中置箱)+一个低音炮组成 5.1环绕声包括了5个全频带声道和 1个低频效果 ...