IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树
E. Bear and Forgotten Tree 2
题目连接:
http://www.codeforces.com/contest/653/problem/E
Description
A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n.
Limak is a little polar bear. He once had a tree with n vertices but he lost it. He still remembers something about the lost tree though.
You are given m pairs of vertices (a1, b1), (a2, b2), ..., (am, bm). Limak remembers that for each i there was no edge between ai and bi. He also remembers that vertex 1 was incident to exactly k edges (its degree was equal to k).
Is it possible that Limak remembers everything correctly? Check whether there exists a tree satisfying the given conditions
Input
The first line of the input contains three integers n, m and k () — the number of vertices in Limak's tree, the number of forbidden pairs of vertices, and the degree of vertex 1, respectively.
The i-th of next m lines contains two distinct integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the i-th pair that is forbidden. It's guaranteed that each pair of vertices will appear at most once in the input.
Output
Print "possible" (without quotes) if there exists at least one tree satisfying the given conditions. Otherwise, print "impossible" (without quotes).
Sample Input
5 4 2
1 2
2 3
4 2
4 1
Sample Output
possible
Hint
题意
给你n个点,然后给你m个限制,每个限制说ai,bi之间不能连边。
问你能否构造出一棵生成树,且1号点的度数恰好等于k
题解:
首先忽略掉恰好等于k这个条件,实际上就是判断这个图是否连通就好了。
然后我们看看1号点的反图的度数是否大于等于k,小于k肯定不行。
然后我们把1号点去掉,跑bfs/dfs,看有多少个连通块和1号点能够相连,如果有大于k个连通块,肯定也是不行的。
小于等于k个连通块就可以。
然后现在问题是那个bfs和dfs跑连通块复杂度可能是n^2的,你遍历边的时候,会遍历到无意义的点。
所以我们需要用一个set去维护现在有哪些点还没有访问。
总之,就是你需要实现一个类似lowbit的功能,然后就可以优化你的dfs/bfs。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+5;
int n,m,k;
set<int>vis,E[maxn];
int q[maxn],st;
void solve(int x)
{
vis.erase(x);
q[st++]=x;
for(int i=0;i<st;i++)
{
int now = q[i];
int pre = 1;
while(1)
{
auto next = vis.upper_bound(pre);
if(next==vis.end())break;
int v = *next;
pre = v;
if(E[now].count(v))continue;
q[st++]=v;vis.erase(v);
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].insert(y);
E[y].insert(x);
}
if(k>n-1-E[1].size())return puts("impossible"),0;
for(int i=2;i<=n;i++)vis.insert(i);
int cnt=0;
for(int i=2;i<=n;i++)
{
if(vis.count(i))
{
cnt++;st=0;
solve(i);
int flag = 0;
for(int j=0;j<st;j++)if(!E[1].count(q[j]))flag=1;
if(flag==0)return puts("impossible"),0;
}
}
if(cnt>k)return puts("impossible"),0;
return puts("possible"),0;
}
IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树的更多相关文章
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing
B. Bear and Compressing 题目链接 Problem - B - Codeforces Limak is a little polar bear. Polar bears h ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) D. Delivery Bears 二分+网络流
D. Delivery Bears 题目连接: http://www.codeforces.com/contest/653/problem/D Description Niwel is a littl ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) C. Bear and Up-Down 暴力
C. Bear and Up-Down 题目连接: http://www.codeforces.com/contest/653/problem/C Description The life goes ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing 暴力
B. Bear and Compressing 题目连接: http://www.codeforces.com/contest/653/problem/B Description Limak is a ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题
A. Bear and Three Balls 题目连接: http://www.codeforces.com/contest/653/problem/A Description Limak is a ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)——A - Bear and Three Balls(unique函数的使用)
A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 653 A. Bear and Three Balls——(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2))
传送门 A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- angular导出文件保存在本地
$scope.ev_click = function(obj){ var ev = document.createEvent("MouseEvents"); ev.initMous ...
- rabbitmq集群搭建方法简介(测试机linux centos)【转】
本文将介绍四台机器搭建rabbitmq集群: rabbitmq IP和主机名(每台机器已安装RabbitMQ 3.5.6, Erlang 18.1) 192.168.87.73 localhost73 ...
- openjudge-NOI 2.6-2985 数字组合
题目链接:http://noi.openjudge.cn/ch0206/2985/ 题解: 跟背包问题有点相似,暂且算背包型DP吧,虽然是一道递推题…… fj表示和为j时的结果,得: 即为j减去每一个 ...
- linux系统查找具体进程
ps -ef | grep '查找内容' eg:ps -ef | grep '测试USB设备穿透'
- css3动画详解
一.Keyframes介绍: Keyframes被称为关键帧,其类似于Flash中的关键帧.在CSS3中其主要以“@keyframes”开头,后面紧跟着是动画名称加上一对花括号“{…}”,括号中就是一 ...
- fedora16下更改网卡名字
fedora16下更改网卡名字 今天由于网络启动出错的原因,在网上搜索,发现大部分人的网卡名字都是eth0,可是我的却是p3p1,所以想改成eth0. 然后google了下,发现设备命名什么的 ...
- 冒泡法的算法最佳情况下的时间复杂度为什么是O(n)
我在许多书本上看到冒泡排序的最佳时间复杂度是O(n),即是在序列本来就是正序的情况下. 但我一直不明白这是怎么算出来的,因此通过阅读<算法导论-第2版>的2.2节,使用对插入排序最佳时间复 ...
- 洛谷P1876开灯 题解
题目传送门 这道题目是道数学题(下面也写了),所以仔细研究发现:N轮之后,只有是小于N的完全平方数的灯能亮着.所以接下来就好办了: #include<bits/stdc++.h> usin ...
- linux 命令route add default dev eth0和route add default gw eth0的区别?
https://blog.csdn.net/zhaogezhuoyuezhao/article/details/7339220
- 两周撸一个掘金微信小程序
利益相关 无 声明 这并不是掘金官方小程序(貌似没有搜到掘金 APP 对应的官方小程序),完全为第三者开发者开发,仅用于学习交流,禁止用于其他用途.若要使用官方正版,可访问掘金 官方网站,或下载掘金官 ...