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 题解 大水题一道 就他给的这个代价,猜都能猜到每个数只能用一次 仔细想想,我们肯定是按顺序 ...
随机推荐
- 解决github pages和github .md文件图片不显示
博客园上传的图片,在github上无法显示. 在github项目下建立img文件夹,放上图片 两种方式 项目绝对路径 https://raw.githubusercontent.com/用户名/项目名 ...
- WEBAPI 最近更新项目时 服务器总是提示:An error has occurred.
解决办法: 在webconfig中设置 <system.web><customErrors mode="Off"/></system.web> ...
- Parallel的使用
Action<int, int> ReportProcess //返回数据,刷新进度 Exception exception = null; object objLock = new ob ...
- Linux之远程文件传输
1)scp scp命令用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器硬盘变为只读re ...
- vue中的常用三元
点击事件的三元 <el-button type="primary" @click="edit == 'mod' ? sureModify() : submit()& ...
- TVM设备添加以及代码生成
因为要添加的设备是一种类似于GPU的加速卡,TVM中提供了对GPU编译器的各种支持,有openCl,OpenGL和CUDA等,这里我们选取比较熟悉的CUDA进行模仿生成.从总体上来看,TVM是一个多层 ...
- Python实现读取Excel文档中的配置并下载软件包
问题:现在遇到这样一个问题,服务器存储了很多软件包,这些包输入不同的产品,每个产品都有自己的配置,互相交叉,那么到底某一产品所有配置的软件包下载后,占用多大空间呢? 分析:从这个问题入手,了解到:软件 ...
- Delphi MSComm控件事件的介绍
- Oracle【账户管理】
Oracle学习大致体系oracle管理系统介绍(客户端和服务器端的交互模式)oracle数据库的数据管理(增删改查 查询)oracle账户管理oracle二维表管理 --创建表 --维护表 ...
- JS 四种条件控制语句
// 1.if...else if (true) { console.log("TRUE1"); } else { console.log("TRUE2"); ...