Codeforces Round #309 (Div. 1) D. Nudist Beach 数据结构

D. Nudist Beach
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers.

There are n cities, labeled from 1 to n, and m bidirectional roads between them. Currently, there are Life Fibers in every city. In addition, there are k cities that are fortresses of the Life Fibers that cannot be captured under any circumstances. So, the Nudist Beach can capture an arbitrary non-empty subset of cities with no fortresses.

After the operation, Nudist Beach will have to defend the captured cities from counterattack. If they capture a city and it is connected to many Life Fiber controlled cities, it will be easily defeated. So, Nudist Beach would like to capture a set of cities such that for each captured city the ratio of Nudist Beach controlled neighbors among all neighbors of that city is as high as possible.

More formally, they would like to capture a non-empty set of cities S with no fortresses of Life Fibers. The strength of a city  is defined as (number of neighbors of x in S) / (total number of neighbors of x). Here, two cities are called neighbors if they are connnected with a road. The goal is to maximize the strength of the weakest city in S.

Given a description of the graph, and the cities with fortresses, find a non-empty subset that maximizes the strength of the weakest city.

Input

The first line of input contains three integers n, m, k (2  ≤  n  ≤ 100 0001 ≤ m ≤ 100 0001 ≤ k ≤ n - 1).

The second line of input contains k integers, representing the cities with fortresses. These cities will all be distinct.

The next m lines contain the roads. The i-th of these lines will have 2 integers ai, bi (1 ≤ ai, bi ≤ nai ≠ bi). Every city will have at least one road adjacent to it.

There is no more than one road between each pair of the cities.

Output

The first line should contain an integer r, denoting the size of an optimum set (1 ≤ r ≤ n - k).

The second line should contain r integers, denoting the cities in the set. Cities may follow in an arbitrary order. This line should not contain any of the cities with fortresses.

If there are multiple possible answers, print any of them.

Sample test(s)
input
9 8 4
3 9 6 8
1 2
1 3
1 4
1 5
2 6
2 7
2 8
2 9
output
3
1 4 5
input
10 8 2
2 9
1 3
2 9
4 5
5 6
6 7
7 8
8 10
10 4
output
8
1 5 4 8 10 6 3 7
Note

The first example case achieves a strength of 1/2. No other subset is strictly better.

The second example case achieves a strength of 1. Note that the subset doesn't necessarily have to be connected.



给出一个无向图,令sum1[x]表示与定点x相邻且在s中的节点的个数,sum2[x]表示与x相邻的节点的个数,要你选出一个子集S(不能包含含fortresses的城市),是的sum1[x]/sum2[x] (x属于S)最小值最大,我开始用的二分,枚举一个值x,初始时将所有点加入s,然后bfs删去sum1[x]/sum2[x]小于x的节点,不知道哪里写搓了,题解是每次删去sum1[x]/sum2[x]最小的节点。

#include <bits/stdc++.h>
#define foreach(it,v) for(__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
typedef long long ll;
using namespace std;
const int maxn = 1e5+10;
struct Node
{
	int x,y,id;
	Node(int x,int y,int id):x(x),y(y),id(id){}
	Node(){}
	bool operator < (const Node & a) const {
		ll t1 = x * ll(a.y), t2 = a.x*ll(y);
		return t1 < t2 || (t1==t2&&id < a.id);
	}
};
vector<int> g[maxn];
void AddEdge(int u,int v)
{
	g[u].push_back(v);
	g[v].push_back(u);
}
bool ban[maxn];
int sum1[maxn],sum2[maxn];
int main(int argc, char const *argv[])
{
	int n,m,k;
	while(scanf("%d%d%d",&n,&m,&k)==3) {
		memset(ban,0,sizeof(ban[0])*(n+10));
		while(k--) {
			int x;scanf("%d",&x);
			ban[x] = 1;
		}
		memset(sum1,0,sizeof(sum1[0])*(n+10));
		memset(sum2,0,sizeof(sum2[0])*(n+10));
		for(int i = 1; i <= m; i++) {
			int u,v;scanf("%d%d",&u,&v);
			++sum2[u],++sum2[v];
			AddEdge(u,v);
			if(ban[u]||ban[v]) continue;
			++sum1[u];++sum1[v];
		}
		Node ans(0,1,0);
		set<Node> s;
		for(int i = 1; i <= n; i++) if(!ban[i]) s.insert(Node(sum1[i],sum2[i],i));
		vector<int> v;
		while(s.size()) {
			Node t = *s.begin();
			ans = max(ans,t);
			s.erase(s.begin());
			v.push_back(t.id);
			ban[t.id] = 1;
			foreach(it,g[t.id]) {
				int & v = *it;
				if(!ban[v]) {
					s.erase(Node(sum1[v],sum2[v],v));
					--sum1[v];
					s.insert(Node(sum1[v],sum2[v],v));
				}
			}
		}
		for(int i = 0; i < v.size(); i++) {
			if(ans.id == v[i]) {
				printf("%d\n", v.size() - i);
				for(; i < v.size(); ++i) printf("%d ", v[i]);
				break;
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值