codeforces 553D . Nudist Beach 二分
题目链接
有趣的题。
给一个图, n个点m条边。 有k个点不可选择。
现在让你选出一个非空的点集, 使得点集中strength最小的点的strength最大。
strength的定义:一个点周围的点中在集合里的点/一个点周围的点。
我们二分这个strength值。
每次二分, 我们遍历每个点, 然后将strength值小于mid的点加到队列里面,然后标记一下, 相当于将这个点去除掉。
然后遍历队列, 将队列里的点周围的点strength值小于mid的加进队列,标记, 不断重复。
最后看有没有点没有被标记就好。
具体看代码。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
int n, m, k;
int a[maxn], vis[maxn], ans[maxn], ban[maxn], de[maxn], num[maxn];
vector <int> g[maxn];
int dblcmp(double x) {
if(fabs(x)<eps)
return 0;
return x<0?-1:1;
}
int check(double ratio) {
queue <int> q;
mem(vis);
mem(num);
for(int i = 1; i <= n; i++) {
if(ban[i]) {
vis[i] = 1;
continue;
}
double tmp = 1-1.0*a[i]/de[i];
if(dblcmp(tmp-ratio)<0) {
q.push(i);
vis[i] = 1;
}
}
while(!q.empty()) {
int u = q.front(); q.pop();
for(auto &i : g[u]) {
if(ban[i] || vis[i])
continue;
num[i]++;
double tmp = 1-1.0*(a[i]+num[i])/de[i];
if(dblcmp(tmp-ratio)<0) {
q.push(i);
vis[i] = 1;
}
}
}
for(int i = 1; i <= n; i++) {
if(!vis[i])
return 1;
}
return 0;
}
void solve() {
double l = 0, r = 1;
for(int i = 0; i < 100; i++) {
double mid = (l+r)/2;
if(check(mid)) {
for(int j = 1; j <= n; j++) {
ans[j] = vis[j];
}
l = mid;
} else {
r = mid;
}
}
int cnt = 0;
for(int i = 1; i <= n; i++)
if(!ans[i])
cnt++;
cout<<cnt<<endl;
for(int i = 1; i <= n; i++)
if(!ans[i])
cout<<i<<" ";
}
int main()
{
int x, u, v;
cin>>n>>m>>k;
for(int i = 0; i < k; i++) {
scanf("%d", &x);
ban[x] = 1;
}
for(int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
g[u].pb(v);
g[v].pb(u);
de[u]++, de[v]++;
if(ban[u])
a[v]++;
if(ban[v])
a[u]++;
}
solve();
return 0;
}
codeforces 553D . Nudist Beach 二分的更多相关文章
- Codeforces 553D Nudist Beach(二分答案 + BFS)
题目链接 Nudist Beach 来源 Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...
- Codeforces 553D Nudist Beach(图论,贪心)
Solution: 假设已经选了所有的点. 如果从中删掉一个点,那么其它所有点的分值只可能减少或者不变. 如果要使若干步删除后最小的分值变大,那么删掉的点集中肯定要包含当前分值最小的点. 所以每次删掉 ...
- codeforces 553 D Nudist Beach
题意大概是.给出一个图,保证每一个点至少有一条边以及随意两点间最多一条边.非常显然这个图有众多点集,若我们给每一个点定义一个权值,那每一个点集都有一个最小权值点,如今要求出一个点集,这个点集的最小权值 ...
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces 626C Block Towers(二分)
C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- Success Rate CodeForces - 807C (数学+二分)
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...
随机推荐
- Silverlight学习(四) domainservice动态多条件查询
上次讲了silverlight+MVVN+EF的简单框架,能够实现简单的数据CURD,但是多条件动态的查询一直没有实现.在网上查阅了很多资料,发现自己走了很多误区,代码很难调试正确. 这次的查询是基于 ...
- Android 上多方式定位元素(python)
Android 上多方式定位元素(python) 在学习实际UI自动化测试的时候,首先就需要定位元素,然而定位元素也是最头疼的地方,因为元素各种控件名称的缺失会影响元素的准确定位.下面针对Androi ...
- SSAS中CUBE行权限数据级权限控制
去年做了一个数据仓库的项目,其中涉及到了CUBE数据级权限的控制.在网上找这方面的资料,找到一个[BI] 通用数据级权限控制解决方案的实现(二):Cube中的角色设置与数据级权限控制.根据这个大牛的思 ...
- 蜗牛爱课- iOS中plist的创建,数据写入与读取
iOS中plist的创建,数据写入与读取功能创建一个test.plist文件-(void)triggerStorage{ NSArray *paths=NSSearchPathForDirect ...
- HDU 4415 - Assassin’s Creed
Problem Description Ezio Auditore is a great master as an assassin. Now he has prowled in the enemie ...
- zoj1107 FatMouse and Cheese
这是一道记忆化搜索,也就是有记录的搜索. 注意点:一次走k步不能拐弯 int bfs(int x,int y) { ; ) return ans[x][y]; ;i<;i++) { ;j< ...
- C期未考试参考答案
输入10个数,要求编写一个排序函数,能够实现按绝对值从大到小排序.在主函数中输入10个数.输出排序后的10个数 #include<stdio.h>#include<math.h> ...
- php 注释
@access 使用范围:class,function,var,define,module 该标记用于指明关键字的存取权限:private.public或proteced @author 指明作者 @ ...
- 使用html,JavaScript,ajax写一个小型实例
//1.创建受捐单位数组 var arrOrgData = [ { "Id": 1, "OrgName": "红十字会" }, ...
- mysql 蠕虫复制
INSERT into user_info(version,create_user_count,create_pc_count) select version,create_user_count,cr ...