题目链接

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. 怎样修改输入框 placehoder 提示文本的颜色?

    1. 在这个问题上, 不同浏览器的设置方法有所差异, 可以写成下面这种形式. ::-webkit-input-placeholder { /* WebKit browsers */ color: #9 ...

  2. 4.Shell内部命令

    4.Shell内部命令内部命令是由shell自身提供的.如果某个内部命令的名称是一个简单命令的第一个单词,shell会直接执行这个命令,而不会启动其它程序.对于一些不可能或者不方便通过外部程序实现的功 ...

  3. Java lesson17 homework

    package lesson17; /**1. 创建自定义类Triangle包含如下属性: 最小夹角 a(整型.单位度) 最大夹角 b(整型.单位度) 编写构造方法包含两个参数(夹角),并根据参数计算 ...

  4. winfrom 点击按钮button弹框显示颜色集

    1.窗体托一个按钮button: 2.单击事件: private void btnForeColor_Click(object sender, EventArgs e) { using (ColorD ...

  5. 日历控件datetimepicker(IE11)

    1.安装 smalot.bootstrap-datetimepicker 2.引用 bootstrap.css bootstrap-datetimepicker.min.css jquery-1.10 ...

  6. opencv 一些函数的耗时计算

    Release 模式 -------------------------------------------------- smooth gaussian : 2 cvtColor CV_BGR2La ...

  7. form表单中的enctype 属性以及post请求里Content-Type方式

    对于form表单中的enctype 属性之前理解的一般,就知道是类似于一种编码形式.后来公司做一个form表单提交数据的时候,重点是这个form表单里有文件上传,而我又要用vue来模拟form表单提交 ...

  8. spring整合shiro配置BUG,Tomcat启动不了:Error during artifact deployment. See server log for details

    现象 spring配置shiro权限控制之后,项目无法启动 [2019-08-09 09:00:35,800] Artifact export_web_manager:war exploded: Er ...

  9. day07 类

    一.目录 1.模块 2.包 3.isinstance issubclass type 4.方法和函数 5.反射 6.约束 7.继承 8.特殊成员 9.异常处理 补充知识点 10.hashlib模块 1 ...

  10. Java反射【四、成员变量的反射和构造的反射】

    获取一个类下所有字段信息 Field[] fs = c.getFields(); 获取所有字段(public) Field[] fs = c.getDeclaredFields(); 获取所有声明字段 ...