Unique Path

AGC 038 D

考虑如果两个点之间只能有一个边它们就把它们缩起来,那么最后缩起来的每一块都只能是一棵树。

如果两个点之间必须不止一个边,并且在一个连通块,显然无解。

首先把所有树给连好,现在的可用的边的数量只有 $ m - n + c $ 了。

然后两个连通块之间如果有超过一条边,连通块内部的点显然不只一条路径了。

其他情况,如果我们给连通块连边的时候每个连通块都只一直用一个点来往外连,就可以保证所有连通块都满足要求。

  • 如果不存在两个点之间不只一个边的情况

    那么只要可用的边的数量多余 $ c-1 $ 并且不超过把这一堆连通块连成完全图的情况,都是可以的。

  • 如果存在

    那么可用的边数量必须多余 $ c $ ,因为至少要连成一个环,否则不能满足这个不只一个边的条件。

其实还有些小情况,想一下就发现是对的。

记得开longlong

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
#define int long long
#define MAXN 200006
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
int n , m , q; int fa[MAXN];
int find( int x ) {
return x == fa[x] ? x : fa[x] = find( fa[x] );
}
struct dl{
int u , v;
} d[MAXN] ;
int cnt , c; signed main( ) {
cin >> n >> m >> q;
for( int i = 1 ; i <= n ; ++ i ) fa[i] = i;
for( int i = 1 , u , v , c ; i <= q ; ++ i ) {
scanf("%lld%lld%lld",&u,&v,&c);
++ u , ++ v;
if( !c ) {
fa[find( u )] = find( v );
} else {
d[++ cnt] = (dl) { u , v };
}
}
for( int i = 1 , u , v ; i <= cnt ; ++ i ) {
if( find( d[i].u ) == find( d[i].v ) )
return puts("No") , 0;
}
for( int i = 1 ; i <= n ; ++ i ) if( fa[i] == i )
++ c;
int ed = m - n + c;
if( !cnt ) return puts( ( ed >= c - 1 && ed <= c * ( c - 1 ) / 2 ) ? "Yes" : "No" ) , 0;
else return puts( ( ed >= c && ed <= c * ( c - 1 ) / 2 ) ? "Yes" : "No" ) , 0;
}

Unique Path AGC 038 D的更多相关文章

  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 ii

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. 【leetcode】 Unique Path ||(easy)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. 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). ...

  7. [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) ...

  8. #Leet Code# Unique Path(todo)

    描述: 使用了递归,有些计算是重复的,用了额外的空间,Version 1是m*n Bonus:一共走了m+n步,例如 m = 2, n = 3 [#, @, @, #, @],所以抽象成数学问题,解是 ...

  9. AtCoder AGC038D Unique Path (图论)

    题目链接 https://atcoder.jp/contests/agc038/tasks/agc038_d 题解 orz zjr神仙做法 考虑把所有\(C_i=0\)的提示的两点连边,那么连完之后的 ...

随机推荐

  1. 【UE4 C++ 基础知识】<13> 多线程——TaskGraph

    概述 TaskGraph 系统是UE4一套抽象的异步任务处理系统 TaskGraph 可以看作一种"基于任务的并行编程"设计思想下的实现 通过TaskGraph ,可以创建任意多线 ...

  2. 【数据结构与算法Python版学习笔记】算法分析

    什么是算法分析 算法是问题解决的通用的分步的指令的聚合 算法分析主要就是从计算资源的消耗的角度来评判和比较算法. 计算资源指标 存储空间或内存 执行时间 影响算法运行时间的其他因素 分为最好.最差和平 ...

  3. 【c++ Prime 学习笔记】第9章 顺序容器

    一个容器是特定类型对象的集合 顺序容器中元素的顺序与其加入容器的位置对应 关联容器中元素的顺序由其关联的关键字决定,关联容器分为有序关联容器和无序关联容器 所有容器类共享公有接口,不同容器按不同方式扩 ...

  4. sqlmap--tamper使用技巧

    apostrophemask.py 适用数据库:ALL 作用:将引号替换为utf-8,用于过滤单引号 使用脚本前: tamper("1 AND '1'='1") 使用脚本后: 1A ...

  5. 【二食堂】Beta - Scrum Meeting 7

    Scrum Meeting 7 例会时间:5.19 18:30~18:50 进度情况 组员 当前进度 今日任务 李健 1. 文本区域的前后端对接完成,bug已经修复issue2. 自定义关系的添加与删 ...

  6. Beta阶段第七次会议

    Beta阶段第七次会议 时间:2020.5.23 完成工作 姓名 工作 难度 完成度 ltx 1.修改小程序页面无法加载bug2.修改条件语句,使得页面能够正常显示 中 90% xyq 1.根据api ...

  7. Unity 制作不规则形状button

    在游戏开发中,我们有时需要制作不规则形状的按键. Unity3d中使用UGUI的Button控件只能实现规则的长方形按钮.而通过给Button的Image组件添加对应的贴图(sprite)我们可以实现 ...

  8. Spring Cloud Gateway 网关限流

    Spring Cloud Gateway 限流 一.背景 二.实现功能 三.网关层限流 1.使用默认的redis来限流 1.引入jar包 2.编写配置文件 3.网关正常响应 4.网关限流响应 2.自定 ...

  9. 通过串口利用printf函数输出数据

    一.printf函数格式 printf函数具有强大的输出功能 %表示格式化字符串输出 目前printf支持以下格式的输出,例如: printf("%c",a);输出单个字符. pr ...

  10. The entitlements specified in your application’s Code Signing Entitlements file do not match those s

    今天给打包 TPshop IOS (搜豹商城) ipa文件 调试运行 xcode运行提示这个错误: The entitlements specified in your application's C ...