题目链接

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)\)

代码

#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<iostream>
#define llong long long
using namespace std; inline int read()
{
int x=0; bool f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
if(f) return x;
return -x;
} const int N = 2e5;
struct Element
{
int u,v,typ;
} a[N+3];
int uf[N+3];
int n,q,c; llong m; int findfa(int u) {return uf[u]==u?u:uf[u]=findfa(uf[u]);} int main()
{
scanf("%d%lld%d",&n,&m,&q);
for(int i=1; i<=n; i++) uf[i] = i;
for(int i=1; i<=q; i++)
{
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].typ); a[i].u++; a[i].v++;
if(a[i].typ==0)
{
int uu = findfa(a[i].u),vv = findfa(a[i].v);
if(uu!=vv) {uf[uu] = vv;}
}
}
int cnt1 = 0; c = 0;
for(int i=1; i<=n; i++) {if(i==findfa(i)) c++;}
for(int i=1; i<=q; i++)
{
if(a[i].typ==1)
{
cnt1++;
int uu = findfa(a[i].u),vv = findfa(a[i].v);
if(uu==vv) {puts("No"); return 0;}
}
}
llong l = cnt1?max(3ll,(llong)c):c-1ll,r = 1ll*c*(c-1ll)/2ll;
if(m-(n-c)>=l && m-(n-c)<=r) {puts("Yes");}
else {puts("No");}
return 0;
}

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. 数值优化(Numerical Optimization)学习系列-无梯度优化(Derivative-Free Optimization)

    数值优化(Numerical Optimization)学习系列-无梯度优化(Derivative-Free Optimization) 2015年12月27日 18:51:19 下一步 阅读数 43 ...

  2. SharePoint 创建母版页

    一.前言 文章成体系,如果有不明白的地方请查看前面的文章. 二.目录 1.创建HTML页面 2.将HTML文件转换为SharePoint母版页 3.在 SPD中修改母版页“PlaceHolderMai ...

  3. O036、Snapshot Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5510296.html   有时候系统损坏的很严重,通过 Rescue 操作无法修复,那么我们就得重新考虑通过备份恢复了. ...

  4. EBS常用表_Dictionary

    EBS常用表:转载于 https://blog.csdn.net/xiariqingcao/article/details/8775827 . OU.库存组织 SELECT hou.organizat ...

  5. shell中$(( ))、$( )、``与${ }的区别详解

    命令替换在bash中,$( )与` `(反引号)都是用来作命令替换的.命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组成新的命令行 [python@m ...

  6. (备忘)Java数据类型中String、Integer、int相互间的转换

    1.Integer转换成int的方法 Integer i; int k = i.intValue();即Integer.intValue(); 2.int转换成Integer int i; Integ ...

  7. python中的负数取模问题(一个大坑)

    先来看一段代码 这是什么情况?为什么会出现这种结果.我们再来看看其它语言的执行结果 我们用golang.js.c分别算了一下,结果得到的结果都是一致的,但是python为啥不一样呢? 其实之所以这么做 ...

  8. busybox date 时间的加减

    1.下载安装busybox: # wget http://busybox.net/downloads/busybox-1.29.3.tar.bz2 # tar -jxvf busybox-.tar.b ...

  9. HTML 做圆形头像

    Click me <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  10. 基于MYCAT中间件实现MYSQL读写分离

    基于mycat实现mysql读写分离 完成主从复制的配置 /* 主节点:192.168.47.101 从节点:192.168.47.102 */ /*mycat为同一网段客户端*/ /* 修改主节点基 ...