Codeforces 839E Mother of Dragons(最大团)
2017-08-31 19:52
344 查看
E. Mother of Dragons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are n castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more than one wall,
no wall connects a castle to itself.
Sir Jaime Lannister has discovered that Daenerys Targaryen is going to attack his kingdom soon. Therefore he wants to defend his kingdom. He has k liters
of a strange liquid. He wants to distribute that liquid among the castles, so each castle may contain some liquid (possibly zero or non-integer number of liters). After that the stability of a wall is defined as follows: if the wall connects two castles a and b,
and they contain x and y liters
of that liquid, respectively, then the strength of that wall is x·y.
Your task is to print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 40, 1 ≤ k ≤ 1000).
Then n lines follows. The i-th
of these lines contains n integers ai, 1, ai, 2, ..., ai, n (
).
If castles i and j are
connected by a wall, then ai, j = 1.
Otherwise it is equal to 0.
It is guaranteed that ai, j = aj, i and ai, i = 0 for
all 1 ≤ i, j ≤ n.
Output
Print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.
Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b.
The checker program will consider your answer correct, if
.
Examples
input
output
input
output
Note
In the first sample, we can assign 0.5, 0.5, 0 liters of liquid to castles 1, 2, 3,
respectively, to get the maximum sum (0.25).
In the second sample, we can assign 1.0, 1.0, 1.0, 1.0 liters of liquid to castles 1, 2, 3, 4,
respectively, to get the maximum sum (4.0)
题目大意:
有一个无向图,和一个数字K,要求把K把K分配到一些结点上(可以分配小数),使得每个边两边的点的值乘积之和最大。
解题思路:
可以发现,把K均等分给最大团上的点时答案最优。对于最大团的求解,一个比较快的方法是Bron_Kerbosch算法。
关于Bron_Kerbosch算法的介绍就直接引用别人的了。
AC代码:
#include <algorithm>
#include <cstdio>
using namespace std;
#define ULL unsigned long long
const int MAXN=45;//最大结点数,根据题意修改
int V;//顶点数
int max_clique;//最大团的大小
ULL G[MAXN];//邻接矩阵的状压表示
inline int ctz(ULL s)//返回末尾0的个数,由于__bilitin_ctzll输入0时未定义,这里手动定义
{
return s?__builtin_ctzll(s):64;
}
void dfs(ULL R,ULL P,ULL X)//已经在团中的点状压,可能在团中的点状压,不考虑的点状压
{
if(!P && !X)//当前团是极大团
{
max_clique=max(max_clique,__builtin_popcountll(R));//R中1的就是极大团中点,如果要输出构成最大团的点,这里更新时保存R中的点即可
return ;
}
if(!P)//当前团是以求得的极大团的子图
return;
int pivot = ctz(P|X);//选择一个轴点
ULL PP = P&~G[pivot];//代选择点中不与轴点相邻的点
for(int u=ctz(PP);u<V;u+=ctz(PP>>(u+1))+1)//枚举PP中的点
{
dfs(R|((ULL)1<<u),P&G[u],X&G[u]);
P ^= (ULL)1<<u;//把u移到不考虑的点中
X |= (ULL)1<<u;
}
}
int BronKerbosch()
{
max_clique=0;
dfs(0,((ULL)1<<V)-1,0);//初始团中没有点,所有点都可能在团中,没有点不考虑
return max_clique;
}
int main()
{
int K;
scanf("%d%d", &V, &K);
for(int i=0;i<V;++i)
for(int j=0;j<V;++j)
{
int tmp;
scanf("%d", &tmp);
if(tmp)
G[i]|=(ULL)1<<j;
}
int i = BronKerbosch();
printf("%.12lf\n", 1.0*K*K*(i-1)/i/2.0);
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are n castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more than one wall,
no wall connects a castle to itself.
Sir Jaime Lannister has discovered that Daenerys Targaryen is going to attack his kingdom soon. Therefore he wants to defend his kingdom. He has k liters
of a strange liquid. He wants to distribute that liquid among the castles, so each castle may contain some liquid (possibly zero or non-integer number of liters). After that the stability of a wall is defined as follows: if the wall connects two castles a and b,
and they contain x and y liters
of that liquid, respectively, then the strength of that wall is x·y.
Your task is to print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 40, 1 ≤ k ≤ 1000).
Then n lines follows. The i-th
of these lines contains n integers ai, 1, ai, 2, ..., ai, n (
).
If castles i and j are
connected by a wall, then ai, j = 1.
Otherwise it is equal to 0.
It is guaranteed that ai, j = aj, i and ai, i = 0 for
all 1 ≤ i, j ≤ n.
Output
Print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.
Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b.
The checker program will consider your answer correct, if
.
Examples
input
3 1 0 1 0 1 0 0 0 0 0
output
0.250000000000
input
4 4 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0
output
4.000000000000
Note
In the first sample, we can assign 0.5, 0.5, 0 liters of liquid to castles 1, 2, 3,
respectively, to get the maximum sum (0.25).
In the second sample, we can assign 1.0, 1.0, 1.0, 1.0 liters of liquid to castles 1, 2, 3, 4,
respectively, to get the maximum sum (4.0)
题目大意:
有一个无向图,和一个数字K,要求把K把K分配到一些结点上(可以分配小数),使得每个边两边的点的值乘积之和最大。
解题思路:
可以发现,把K均等分给最大团上的点时答案最优。对于最大团的求解,一个比较快的方法是Bron_Kerbosch算法。
关于Bron_Kerbosch算法的介绍就直接引用别人的了。
AC代码:
#include <algorithm>
#include <cstdio>
using namespace std;
#define ULL unsigned long long
const int MAXN=45;//最大结点数,根据题意修改
int V;//顶点数
int max_clique;//最大团的大小
ULL G[MAXN];//邻接矩阵的状压表示
inline int ctz(ULL s)//返回末尾0的个数,由于__bilitin_ctzll输入0时未定义,这里手动定义
{
return s?__builtin_ctzll(s):64;
}
void dfs(ULL R,ULL P,ULL X)//已经在团中的点状压,可能在团中的点状压,不考虑的点状压
{
if(!P && !X)//当前团是极大团
{
max_clique=max(max_clique,__builtin_popcountll(R));//R中1的就是极大团中点,如果要输出构成最大团的点,这里更新时保存R中的点即可
return ;
}
if(!P)//当前团是以求得的极大团的子图
return;
int pivot = ctz(P|X);//选择一个轴点
ULL PP = P&~G[pivot];//代选择点中不与轴点相邻的点
for(int u=ctz(PP);u<V;u+=ctz(PP>>(u+1))+1)//枚举PP中的点
{
dfs(R|((ULL)1<<u),P&G[u],X&G[u]);
P ^= (ULL)1<<u;//把u移到不考虑的点中
X |= (ULL)1<<u;
}
}
int BronKerbosch()
{
max_clique=0;
dfs(0,((ULL)1<<V)-1,0);//初始团中没有点,所有点都可能在团中,没有点不考虑
return max_clique;
}
int main()
{
int K;
scanf("%d%d", &V, &K);
for(int i=0;i<V;++i)
for(int j=0;j<V;++j)
{
int tmp;
scanf("%d", &tmp);
if(tmp)
G[i]|=(ULL)1<<j;
}
int i = BronKerbosch();
printf("%.12lf\n", 1.0*K*K*(i-1)/i/2.0);
return 0;
}
相关文章推荐
- CodeForces - 839E Mother of Dragons 最大团(Bron–Kerbosch算法)
- CodeForces - 839E Mother of Dragons (最大团)
- CodeForces - 839E Mother of Dragons(无向图最大团)
- codeforces 839 E. Mother of Dragons(最大团)
- Codeforces 839E Mother of Dragons
- Codeforces 839E Mother of Dragons【__builtin_popcount()的使用】
- CodeForces 332 B. Maximum Absurdity 动规 求和最大的两段连续子列
- CodeForces 474E Pillars(线断树区间最大)
- CodeForces 722C Destroying Array(数组删除区间最大值)
- 【codeforces 733D】【贪心 乱搞】D. Kostya the Sculptor【给你n个长方形,让你找出2个或1个长方体,使得他们拼接成的长方体的内接圆半径最大】
- Codeforces 542A. Place Your Ad Here (扫描线进阶 带权值的线段交求最大值) (线段树)
- codeforces 498C C. Array and Operations(最大流+数论)
- 网络流(最大流)CodeForces 512C:Fox And Dinner
- 多对多LCP和最大 Trie DFS CodeForces - 566A Matching Names
- Codeforces 734E. Anton and Tree By Assassin 缩点+树的最大直径
- codeforces 78E - Evacuation 最大流
- Codeforces 546 E:士兵的旅行 最大网络流
- CodeForces 501A Contest(最大得分)
- Codeforces 706D Vasiliy's Multiset (字典树求异或最大值)
- Codeforces 546E Soldier and Traveling(最大流)