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 题解 大水题一道 就他给的这个代价,猜都能猜到每个数只能用一次 仔细想想,我们肯定是按顺序 ...
随机推荐
- Online Meeting CodeForces - 420B (思维)
大意: 给定某一段连续的上线下线记录, 老板上线或下线时房间无人, 并且每次会议都在场, 求哪些人可能是老板. 结论1: 从未出现过的人一定可以是老板. 结论2: 出现过的人中老板最多只有1个. 结论 ...
- mvc布局(一)
negut添加Optimization @System.Web.Optimization.Styles.Render( "~/Content/styles/css/font-awesome. ...
- 浅读vuex源码,了解vuex基本原理
极简版vuex代码 class KVuex { constructor (options) { this.state = options.state this.mutations = options. ...
- centos redis自启动
#!/bin/sh # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database # Simple R ...
- multiple类型的select option在django后台如何取值
之前前端的select都是单选类型,在新的场景中允许用户选择多个条件, 前端的代码如下: <form action="{% url 'info:result-list' %}" ...
- odoo 关系字段(关联关系)
Many-to-one关联 publisher_id = fields.Many2one(comodel_name= 'res.partner', domain='',context={},ondel ...
- rsync 服务快速部署手册
一.rsync服务端安装 1.查看rsync安装包 # rpm -qa rsync rsync-3.0.6-12.el6.x86_64 2.安装rsync 系统默认都会安装rsync软件包的,如果查看 ...
- 取消任务(Task)
private static void TaskCancelDemo() { //向应该被取消的 System.Threading.CancellationToken 发送信号 Cancellatio ...
- zencart加大数据表字段长度
批量表产品名称过长导致被截断的情况,是由于产品名称超出了数据库表中字段设置的最大长度,下面通过修改数据库表字段长度来避免此类情况发生: ) ; ) ; ) ; ) ; ) ; ) ; ) ; ) ; ...
- 【2017中国大学生程序设计竞赛-哈尔滨站】B - K-th Number
原题: 题意: 给你一个长度为N的正整数组A,对于这个数组的所有子区间,若长度小于k则不管它,若长度大于等于k则取第k大放入数组B 问你B中第M大的数是谁 一眼序列分治,然而没思路 数据结构?能想到从 ...