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 ...
随机推荐
- asp.net linq查询环境搭建
本文是以sqlserver2008为数据库,vs2013为开发工具来介绍的. 要搭建这样一个数据库的操作环境,首先建立一个类库项目 然后在这个类库项目中添加几个类:DBDataContext数据库上下 ...
- 02安卓用户界面优化之(二)SlidingMenu使用方法
一.SlidingMenu配置方法 1.下载SlidingMenu:https://github.com/jfeinstein10/SlidingMenu 2.拷贝SlidingMenu-master ...
- asp.net uploadfile 上传文件,连接已重置问题
修改web.config中的配置 <httpRuntime maxRequestLength="/> //设置上传文件大小(kb)和响应时间(s) 针对iis7或更高版本另需要在 ...
- C/S ASP.NET页面传值汇总
一. QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法.但是对于传递数组或对象的话,就不 ...
- golang Aes
package models import ( "bytes" "crypto/aes" "crypto/cipher" "err ...
- DBI && MySQL lock
DBI: url set isolation to dirty read my $npmdb_dbh = DBI->connect("DBI:ODBC:npmdb", &qu ...
- PHP 源码加密扩展(php-beast)PHP7 版本发布
此版本主要支持PHP7,在github(https://github.com/liexusong/php-beast)上选择php7分支然后编译安装即可. 来源于:https://github.c ...
- php实现多表(四表)连接
<?php include_once "DBHelper.php"; define('HOST', '127.0.0.1'); define('USER', 'root'); ...
- 工作中的第一份LoadRunner脚本
录制的第一份脚本 虽然是第一份但是调试执行,跑场景等都成功了. Action() { web_url("login1.jsp", "URL=http://192.168. ...
- Linux man 后面的数字含义及作用
Linux的man很强大,该手册分成很多section,使用man时可以指定不同的section来浏览,各个section意义如下: 1 Executable programs or shell co ...