题目链接

https://atcoder.jp/contests/agc038/tasks/agc038_d

题解

orz zjr神仙做法

考虑把所有\(C_i=0\)的提示的两点连边,那么连完之后的每个连通块都是一棵树

那么同一连通块内就不能出现\(C_i=1\)的提示,然后不同连通块之间可以任意连边,但是要满足两个连通块之间只能连一条边,还要连通

设有\(c\)个连通块,那么就要在连通块之间连\(m-(n-c)\)条边

如果没有\(C_i=1\)的提示,就只要求\(c-1\le m-(n-c)\le \frac{c(c-1)}{2}\)

如果有,就要求\(\max(3,c)\le m-(n-c)\le \frac{c(c-1)}{2}\)

时间复杂度\(O(n)\)

代码

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cassert>
  4. #include<iostream>
  5. #define llong long long
  6. using namespace std;
  7. inline int read()
  8. {
  9. int x=0; bool f=1; char c=getchar();
  10. for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
  11. for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
  12. if(f) return x;
  13. return -x;
  14. }
  15. const int N = 2e5;
  16. struct Element
  17. {
  18. int u,v,typ;
  19. } a[N+3];
  20. int uf[N+3];
  21. int n,q,c; llong m;
  22. int findfa(int u) {return uf[u]==u?u:uf[u]=findfa(uf[u]);}
  23. int main()
  24. {
  25. scanf("%d%lld%d",&n,&m,&q);
  26. for(int i=1; i<=n; i++) uf[i] = i;
  27. for(int i=1; i<=q; i++)
  28. {
  29. scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].typ); a[i].u++; a[i].v++;
  30. if(a[i].typ==0)
  31. {
  32. int uu = findfa(a[i].u),vv = findfa(a[i].v);
  33. if(uu!=vv) {uf[uu] = vv;}
  34. }
  35. }
  36. int cnt1 = 0; c = 0;
  37. for(int i=1; i<=n; i++) {if(i==findfa(i)) c++;}
  38. for(int i=1; i<=q; i++)
  39. {
  40. if(a[i].typ==1)
  41. {
  42. cnt1++;
  43. int uu = findfa(a[i].u),vv = findfa(a[i].v);
  44. if(uu==vv) {puts("No"); return 0;}
  45. }
  46. }
  47. llong l = cnt1?max(3ll,(llong)c):c-1ll,r = 1ll*c*(c-1ll)/2ll;
  48. if(m-(n-c)>=l && m-(n-c)<=r) {puts("Yes");}
  49. else {puts("No");}
  50. return 0;
  51. }

AtCoder AGC038D Unique Path (图论)的更多相关文章

  1. [LeetCode]题解(python):062 Unique path

    题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x  ...

  2. LeetCode 63. Unique Path II(所有不同路径之二)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. leetcode63—Unique Path II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. Unique Path AGC 038 D

    Unique Path AGC 038 D 考虑如果两个点之间只能有一个边它们就把它们缩起来,那么最后缩起来的每一块都只能是一棵树. 如果两个点之间必须不止一个边,并且在一个连通块,显然无解. 首先把 ...

  5. Unique path ii

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. 【leetcode】 Unique Path ||(easy)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. LeetCode题解——Unique Path(DP与优化)

    题目:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  8. [Leetcode 62]机器人走路Unique Path 动态规划

    [题目] A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below) ...

  9. AtCoder AGC022C Remainder Game (图论)

    题目链接 https://atcoder.jp/contests/agc022/tasks/agc022_c 题解 大水题一道 就他给的这个代价,猜都能猜到每个数只能用一次 仔细想想,我们肯定是按顺序 ...

随机推荐

  1. weblogic连接池

    1.在 使用JDBC连接池的过程中,最常见的一个问题就是连接池泄漏问题.一个池里面的资源是有限的,应用用完之后应该还回到池中,否则池中的资源会被耗尽. WebLogic Server提供了一个Inac ...

  2. 【POI】使用POI 创建生成XLS,打开xls文件提示【此文件中某些文本格式可能已经更改,因为它已经超出最多允许的字体数。】

    使用POI 创建生成XLS,打开xls文件提示[此文件中某些文本格式可能已经更改,因为它已经超出最多允许的字体数.] 原因: 是因为在POI处理xls的过程中,太多次调用了: HSSFFont fon ...

  3. Eclipse错误提示: Symbol 'xxxx' could not be resolved

    在eclipse中安装maven(网上资源):https://zhinan.sogou.com/guide/detail/?id=1610049267 项目名 右键->configure-> ...

  4. 【原创】大叔问题定位分享(33)oozie提交任务报错ArithmeticException: / by zero

    oozie提交workflow后执行task报错: 2019-07-04 17:19:00,559 ERROR [RMCommunicator Allocator] org.apache.hadoop ...

  5. H.264 详解

    一.MPEG-4说明 1.VOP视频编码技术 VO(Video Object):视频对象,它是场景中的某个物体,最简单的情况下就是矩形框,它是有生命期的,由时间上连续的许多帧构成. VOP(Video ...

  6. linux环境下,springboot jar启动方式

    linux环境下,springboot jar启动方式 一.前台启动(ctrl+c会关掉进程) java -jar application.jar 二.后台启动(ctrl+c不会关闭) java -j ...

  7. python之字典一

    字典的定义: 前面我们说过列表,它适合于将值组织到一个结构中并且通过编号对其进行引用.字典则是通过名字来引用值的数据结构,并且把这种数据结构称为映射,字典中的值没有特殊的顺序,都存储在一个特定的键(k ...

  8. linux命令详解——iostat

    简介 iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之后运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间 ...

  9. MySQL连表查询练习题

    1.建库 库名:linux50 字符集:utf8 校验规则:utf8_general_ci  create database linux4 charset utf8 default collate ...

  10. Mysql(三)-3:完整性约束

    一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...