Codeforces 839E Mother of Dragons【__builtin_popcount()的使用】
E. Mother of Dragons
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.
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.
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 .
3 1
0 1 0
1 0 0
0 0 0
0.250000000000
4 4
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
4.000000000000
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)
题目链接:http://codeforces.com/contest/839/problem/E
分析__builtin_popcount()的使用及原理参看这里
下面给出AC代码:
#include "bits/stdc++.h"
using namespace std;
const int N = ;
const double eps = 1e-;
int n , k;
long long graph[N];
double ans;
int clique;
int bitcnt[ << ];
void solve(long long mask , int cur){
clique = max(clique , cur);
int idx = -;
int mx = ;
for(int i = ; i <= n ; ++i){
if(!((mask >> i) & )){
continue;
}
int tmp = __builtin_popcountll(graph[i] & mask);
if(idx == - || tmp > mx){
mx = tmp;
idx = i;
}
}
if(idx == -){
return;
}
clique = max(clique , cur + );
if(mx + + cur <= clique){
return;
}
solve((mask ^ (1LL << idx)) & graph[idx] , cur + );
solve(mask ^ (1LL << idx) , cur);
}
int main(){
scanf("%d %d" , &n , &k);
for(int i = ; i <= n ; ++i){
int tmp;
graph[i] = ;
for(int j = ; j <= n ; ++j){
scanf("%d" , &tmp);
graph[i] |= (1LL << j) * tmp;
}
}
long long mask = ;
for(int i = ; i <= n ; ++i){
mask |= 1LL << i;
}
clique = ;
solve(mask , );
ans = (1.0 * k * k) / (1.0 * clique * clique);
ans *= clique * (clique - );
ans /= ;
printf("%.6lf\n" , ans);
}
Codeforces 839E Mother of Dragons【__builtin_popcount()的使用】的更多相关文章
- Codeforces 839E Mother of Dragons(极大团)
[题目链接] http://codeforces.com/contest/839/problem/E [题目大意] 现在有一些点,现在你有k的液体,随意分配给这些点, 当两个点有边相连的时候,他们能产 ...
- Codeforces 839E Mother of Dragons
题 OvO http://codeforces.com/contest/839/problem/E (Codeforces Round #428 (Div. 2) - E) 解 首先,k肯定是要平均分 ...
- 【CF839E】Mother of Dragons 折半状压
[CF839E]Mother of Dragons 题意:给你一张n个点,m条边的无向图.你有k点能量,你可以把能量分配到任意一些点上,每个点分到的能量可以是一个非负实数.定义总能量为:对于所有边&l ...
- Codeforces Round #428 (Div. 2)E. Mother of Dragons
http://codeforces.com/contest/839/problem/E 最大团裸题= =,用Bron–Kerbosch算法,复杂度大多博客上没有,维基上查了查大约是O(3n/3) 最大 ...
- CF839E Mother of Dragons 最大团 Bron-Kerbosch算法
题意简述 给你一个\(n\)个节点的无向图\(G=\{V,E\}\)的邻接矩阵\(g\)和每个点的点权为\(s_i\),且\(\sum_{i=1}^n s_i = K\),要你求出\(\mathrm{ ...
- Codeforces Round #428 (Div. 2) 题解
题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...
- TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E
传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...
- 22. CTF综合靶机渗透(十五)
靶机说明: Game of Thrones Hacking CTF This is a challenge-game to measure your hacking skills. Set in Ga ...
- 长春理工大学第十四届程序设计竞赛(重现赛)M.Orx Zone
链接:https://ac.nowcoder.com/acm/contest/912/M 题意: Daenerys Stormborn, 风暴中出生的丹尼莉丝,the Unburnt, 烧不死的,Qu ...
随机推荐
- 日期函数ADD_MONTHS,MONTHS_BETWEEN,LAST_DAY,NEXT_DAY
- 使用requests爬取猫眼电影TOP100榜单
Requests是一个很方便的python网络编程库,用官方的话是"非转基因,可以安全食用".里面封装了很多的方法,避免了urllib/urllib2的繁琐. 这一节使用reque ...
- 如何严格设置php中session过期时间
如何严格限制session在30分钟后过期! 1.设置客户端cookie的lifetime为30分钟: 2.设置session的最大存活周期也为30分钟: 3.为每个session值加入时间戳,然后在 ...
- js功能代码大全
1.日期格式化 //化为2017-08-14 function formatDate (date) { var y = date.getFullYear(); var m = date.getMont ...
- CPP--正码,反码,补码~附整数溢出的探讨
之前说到了long的争议(http://www.cnblogs.com/dotnetcrazy/p/8059210.html),这边就不用long来举例了,用int吧 可以看一下这篇文章(http:/ ...
- 【转】java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
最近在配置最新的ssh(struts2.3.16.3+hibernate4.3.7+spring4.1.2)的时候遇到的这个错误提示,后来在网上找了半天都不能解决,虽然有个说法是model对象用这样@ ...
- 阿里云 redis 通过rinetd 进行端口透传
https://help.aliyun.com/document_detail/43850.html?spm=5176.7738718.2.3.yW2eyQ 目前云数据库 Redis 版需要通过 EC ...
- 原生JavaScript如何解决父元素查找指定类名的子元素的问题
问题:已知一个元素的类名是right并且这个元素的祖先元素的类名为parent,现在想通过原生JavaScript获得这个类名为right的元素. 我的思路:利用递归.先判断已知的祖先元素(题中的pa ...
- 我的Python学习笔记(二):浅拷贝和深拷贝
在Python中,对象赋值,拷贝(浅拷贝和深拷贝)之间是有差异的,我们通过下列代码来介绍其区别 一.对象赋值 对象赋值不会复制对象,它只会复制一个对象引用,不会开辟新的内存空间 如下例所示,将test ...
- oracle PL/SQL语法基础
目录 数据类型 定义变量 PL/SQL控制结构 参考资料 Oracle10g数据类型总结 PL/SQL之基础篇 数据类型 学习总结 字符类型 char.nchar.varchar.nvarchar:有 ...