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 ...
随机推荐
- (4)剑指Offer之链表相关编程题
一 链表中倒数第k个节点 题目描述: 输入一个链表,输出该链表中倒数第k个结点 问题分析: 一句话概括: 两个指针一个指针p1先开始跑,指针p1跑到k-1个节点后,另一个节点p2开始跑,当p1跑到最后 ...
- 64_s2
sipwitch-1.9.15-3.fc26.x86_64.rpm 13-Feb-2017 09:19 162822 sipwitch-cgi-1.9.15-3.fc26.x86_64.rpm 13- ...
- Python学习笔记——迭代器和生成器
1.手动遍历迭代器 使用next函数,并捕获StopIteration异常. def manual_iter(): with open('./test.py') as f: try: while Tr ...
- Ubuntu 各版本的几个国内更新源
Ubuntu 国内更新源(各版本通用) 前言:为了下载更方便,速度更快,我们在使用Linux系列系统时修改 apt源 为国内的源 1.复制源文件备份,以防万一 修改文件sources.list,在目录 ...
- C语言花括号
由于C语言本身就是函数式语言,说白了,C程序就是由函数构成的! 所以花括号肯定是用在函数之中,包括函数中的各种流程控制语句中. 实际上,C程序中花括号{}的作用:就是把多个单条语句用花括号{}括起来组 ...
- mongodb与mysql传统的关系数据库区别
转自:易百教程 MongoDB中的数据具有灵活的模式.文档在同一集合,但它们不需要具有相同的字段或结构集合,集合文档中的公共字段可以包含不同类型的数据. MongoDB中的数据具有灵活的模式.与SQL ...
- Ceph 时钟偏移问题 clock skew detected 解决方案--- 部署内网NTP服务
告警:HEALTH_WARN clock skew detected on mon.ip-10-25-195-6; 8 requests are blocked > 32 sec; Monito ...
- 父元素的mousedown事件上父元素的mousedown事件上的offsetX和offsetY错误的offsetX和offsetY错误
https://stackoverflow.com/questions/35360704/wrong-offsetx-and-offsety-on-mousedown-event-of-parent- ...
- C语言 五子棋
#include <stdlib.h> #include <stdio.h> #include <conio.h> #include <string.h> ...
- CSS Sprites的原理(图片整合技术)(CSS精灵)/雪碧图
CSS Sprites的原理(图片整合技术)(CSS精灵)/雪碧图 一.将导航背景图片,按钮背景图片等有规则的合并成一张背景图,即将多张图片合为一张整图,然后用background-positio ...