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. VB脚本错误,系统找不到制定的文件 。代码:80070002

    希望得到网友的答案:非常感谢!

  2. ssh整合not found class 异常总结

    (1)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqls ...

  3. C# Control.Invoke匿名委托

    if (txbValue.InvokeRequired) txbValue.Invoke(new MethodInvoker(delegate() {                          ...

  4. sql server 中DateName()函数及DatePart()函数

    Datepart():返回代表指定日期的指定日期部分的整数 语法:Datepart(datepart,date)  返回类型:int DateName():返回代表指定日期的指定日期部分的字符串 语法 ...

  5. windows8安装msi或exe软件提示2503错误的解决办法

    windows8以后的版本安装msi软件(比如nodejs.msi.Git.msi.python.msi.T ortoiseSVN.msi)的时候老师出现2503.2502的错误,究其原因还是系统权限 ...

  6. django 1.9.7 css控制模板样式

    问题:css样式不能控制html样式(针对开发环境,不针对生产环境) 现象: django.template.exceptions.TemplateSyntaxError: Invalid block ...

  7. JavaScript(四):运算符&数据类型转换

    +:算符的加法:连接字符串 加法会将其它类型的值,自动转为字符串,然后再进行连接运算! var a=1+2; console.log('first: '+a); var a=1+2+'3';//先计算 ...

  8. python 试题归纳及答疑 更新中.....

    一.Python基础篇(80题) 1.你为什么学习Python? 一.答题思路 1.阐述 python 优缺点 2.Python应用领域说明 3.根据自身工作情况阐述为什么会使用python 1)py ...

  9. javascript_函数式_链式编程

  10. 文件缓冲区在fork后复制

    场景:父进程trace进程A,当A进程fork子进程B时,让父进程也fork子进程去trace子进程B,用于trace的进程将被trace的进程发生的系统调用号通过fprintf存入各自文件中 问题: ...