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 反图的生成树的更多相关文章

  1. 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 ...

  2. 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 ...

  3. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表

    E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. PHP7+Nginx的配置与安装教程详解

    下面脚本之家小编把PHP7+Nginx的配置与安装教程分享给大家,供大家参考,本文写的不好还请见谅. 系统环境:centos6.5 x64 软件版本:nginx-1.10.0 php-7.0.6 安装 ...

  2. Python3 多进程

    多进程(multiprocessing)的用法和多线程(threading)类似,里面的函数也一样,start()为启动函数,join() 等待该进程运行结束,每一个进程也是由它的父进程产生 1.简单 ...

  3. ubuntu新机安装工具

    ubuntu新机安装工具:1,sudo apt-get install ssh vim2, 设置root密码,以备不时之需: 执行:sudo passwd root 然后输入当前三次密码,第一次是当前 ...

  4. 关于text-decoration无法清除继承的问题

    因为text-decoration的值可以叠加,所以即使设置了none,浏览器也是看成是叠加,而不是清除的意思.

  5. 64.Minimum Path Sum---dp

    题目链接:https://leetcode.com/problems/minimum-path-sum/description/ 题目大意:从左上到右下的路径中,找出路径和最小的路径(与62,63题相 ...

  6. 【51nod1006】simple KMP

    原题意看的挺迷糊的,后来看了http://blog.csdn.net/YxuanwKeith/article/details/52351335大爷的题意感觉清楚的多…… 做法也非常显然了,用树剖维护后 ...

  7. $FFT$(快速傅里叶变换)

    - 概念引入 - 点值表示 对于一个$n - 1$次多项式$A(x)$,可以通过确定$n$个点与值(即$x$和$y$)来表示这唯一的$A(x)$ - 复数 对于一元二次方程 $$x^2 + 1 = 0 ...

  8. hive(七)hive-运行方式、GUI接口、权限管理

    1.Hive运行方式: 命令行方式cli:控制台模式 脚本运行方式(实际生产环境中用最多) JDBC方式:hiveserver2 web GUI接口 (hwi.hue等)   1.1Hive在CLI模 ...

  9. JavaWeb知识回顾-servlet生命周期。

    Servlet生命周期 生命周期,很容易理解,拿人来说,就是你从出生到离开的这一过程.无论是什么技术,只要谈到生命周期都可以这样理解. Servlet的生命周期就是从它被创建到毁灭的过程,整个过程可以 ...

  10. 服务器fsockopen函数和pfsockopen函数开启及作用

    摘要: fsockopen()函数的作用是可以用来打开一个socket连接,另一个函数pfsockopen()也有相似的功能,只不过后者是一个“持续”(persistent)的fsockopen()函 ...