AtCoder AGC038D Unique Path (图论)
题目链接
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 (图论)的更多相关文章
- [LeetCode]题解(python):062 Unique path
题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x ...
- LeetCode 63. Unique Path II(所有不同路径之二)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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 ...
- Unique Path AGC 038 D
Unique Path AGC 038 D 考虑如果两个点之间只能有一个边它们就把它们缩起来,那么最后缩起来的每一块都只能是一棵树. 如果两个点之间必须不止一个边,并且在一个连通块,显然无解. 首先把 ...
- Unique path ii
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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). ...
- [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) ...
- AtCoder AGC022C Remainder Game (图论)
题目链接 https://atcoder.jp/contests/agc022/tasks/agc022_c 题解 大水题一道 就他给的这个代价,猜都能猜到每个数只能用一次 仔细想想,我们肯定是按顺序 ...
随机推荐
- hdu 1215 求约数和 唯一分解定理的基本运用
http://acm.hdu.edu.cn/showproblem.php?pid=1215 题意:求解小于n的所有因子和 利用数论的唯一分解定理. 若n = p1^e1 * p2^e2 * ……*p ...
- window.setInterval
window.clearInterval与window.setInterval的用法 window.setInterval() 功能:按照指定的周期(以毫秒计)来调用函数或计算表达式. 语法:setI ...
- 解决VS2015 不能设置下面的断点**** 断点未能绑定
解决VS2015 不能设置下面的断点**** 断点未能绑定 1. 清理解决方案 , 重新生成解决方案 , 无效!! 2. 选项-- 调试 -- 启用编辑并继续 无效!! 3. 启 ...
- jquery.validate.js表单验证 jquery.validate.js的用法
jquery.validate.js这个插件已经用了2年多了,是一个不可多得的表单验证最方便快捷的插件.基于jquery的小插件,基本小白一学就会上手,对于项目表单页面比较多,元素比较多的校验,该插件 ...
- canva绘制圆角矩形
在做组态的时候,需要支持矩形圆角格式,但是因为canvas本身不带有圆角矩形,需要自行算出坐标进行绘制 方案一.统一圆角 <!DOCTYPE html> <html> < ...
- day06 Python class基础篇
一.目录 1.类与对象的概述 2.封装 3.继承 4.多态 5.类的成员 6.类与类之间的关系 7.私有 二. 内容讲解 一.类与对象的概述 类是对一系列具有相同属性的事物的抽象,相同于设计图纸,而对 ...
- vue项目性能优化总结
在使用elementUI构建公司管理系统时,发现首屏加载时间长,加载的网络资源比较多,对系统的体验性会差一点,而且用webpack打包的vuejs的vendor包会比较大.所以通过搜集网上所有对于vu ...
- 利用 Monitor.TryEnter 来规避 .NET 线程死锁的源代码
在开发多线程的应用程序时,我们会大量用到 lock (...) {} 块.如果 lock 的对象比较多,非常容易发生死锁.死锁的发生很难预料,而且一旦发生在界面线程上,界面就不再刷新响和应用户输入:如 ...
- 《Linux就该这么学》day1-day2
ps:原谅我的书法出自鲁迅的<野草> <Linux就该这么学>书本介绍: 本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极 ...
- 线上MYSQL同步报错故障处理总结(转)
前言 在发生故障切换后,经常遇到的问题就是同步报错,数据库很小的时候,dump完再导入很简单就处理好了,但线上的数据库都150G-200G,如果用单纯的这种方法,成本太高,故经过一段时间的摸索,总结了 ...