E - Bear and Forgotten Tree 2

思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界。

求连通块用链表维护。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define fi first
  4. #define se second
  5. #define mk make_pair
  6. #define PII pair<int, int>
  7. #define PLI pair<LL, int>
  8. #define ull unsigned long long
  9. using namespace std;
  10.  
  11. const int N = 3e5 + ;
  12. const int inf = 0x3f3f3f3f;
  13. const LL INF = 0x3f3f3f3f3f3f3f3f;
  14. const int mod = ;
  15. const int Mod = 1e9 + ;
  16.  
  17. int n, m, k, tot, pre, cnt1, cnt2;
  18. bool vis[N], ok[N];
  19. vector<int> ban[N];
  20.  
  21. struct node {
  22. int id, nx;
  23. } Link[N];
  24.  
  25. void add(int id) {
  26. Link[tot].id = id;
  27. Link[tot].nx = Link[].nx;
  28. Link[].nx = tot++;
  29. }
  30.  
  31. int main() {
  32. Link[].nx = -; tot = ;
  33. scanf("%d%d%d", &n, &m, &k);
  34. cnt1 = ;
  35. for(int i = ; i <= m; i++) {
  36. int u, v;
  37. scanf("%d%d", &u, &v);
  38. ban[u].push_back(v);
  39. ban[v].push_back(u);
  40. if(u == ) ok[v] = true;
  41. if(v == ) ok[u] = true;
  42. }
  43. for(int i = ; i <= n; i++) add(i);
  44.  
  45. int down = , up = ;
  46. while(Link[].nx != -) {
  47. int cnt = ;
  48. queue<int> que;
  49.  
  50. que.push(Link[Link[].nx].id);
  51. Link[].nx = Link[Link[].nx].nx;
  52.  
  53. while(!que.empty()) {
  54. int u = que.front(); que.pop();
  55. if(!ok[u]) cnt++;
  56. for(int b : ban[u]) vis[b] = true;
  57. pre = ;
  58. for(int i = Link[].nx; ~i; i = Link[i].nx) {
  59. int to = Link[i].id;
  60. if(!vis[to]) {
  61. que.push(to);
  62. Link[pre].nx = Link[i].nx;
  63. } else pre = i;
  64. }
  65. for(int b : ban[u]) vis[b] = false;
  66. }
  67. if(!cnt) {
  68. puts("impossible");
  69. return ;
  70. }
  71. down++;
  72. up += cnt;
  73. }
  74.  
  75. if(k >= down && k <= up) puts("possible");
  76. else puts("impossible");
  77. return ;
  78. }
  79.  
  80. /*
  81. */

IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表的更多相关文章

  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 bfs set 反图的生成树

    E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tre ...

  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. bzoj 2530 [Poi2011]Party 构造

    2530: [Poi2011]Party Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 364  Solved:  ...

  2. Centos7单机部署ELK

    一. 简介 1.1 介绍 ELK是三个开源工具组成,简单解释如下: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风 ...

  3. python---Scrapy模块的使用(一)

    Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中. Scrapy 使用了 Twisted异步网络库来处理网络通讯.整体 ...

  4. CCPC2018-A-Buy and Resell

    Problem Description The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1 ...

  5. 【BZOJ】2154: Crash的数字表格 莫比乌斯反演

    [题意]给定n,m,求Σlcm(i,j),1<=i<=n,1<=j<=m,n,m<=10^7. [算法]数论(莫比乌斯反演) [题解] $$ans=\sum_{i\leq ...

  6. 【BZOJ】1087: [SCOI2005]互不侵犯King

    [算法]状态压缩型DP [题解]http://www.cnblogs.com/xtx1999/p/4620227.html (orz) https://www.cnblogs.com/zbtrs/p/ ...

  7. Let's Encrypt 免费通配 https 签名证书 安装方法

    安装环境 centOs7 主要通过 acme.sh (bash脚本)来注册签名 git地址:https://github.com/Neilpang/acme.sh 申请证书流程 1.申请证书-> ...

  8. NYOJ 163 Phone List (字符串处理 字典树)

    题目链接 描述 Given a list of phone numbers, determine if it is consistent in the sense that no number is ...

  9. 1-spring xml 和 注解 解析过程

    spring mvc 入口 DispatcherServlet,类关系图如下所示 DispatcherServlet 就是一个 Servlet,那Servlet 的初始化方法 init()在哪里,通过 ...

  10. (转)USB的描述符及各种描述符之间的依赖关系

    全文链接:http://justmei.blog.163.com/blog/static/11609985320102421659260/?latestBlog 1 推荐 [原创] USB入门系列之七 ...