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. HttpClient.PatchAsJsonAsync - dotnet/runtime 项目贡献小记

    TL;DR 迫于 PatchAsJsonAsync 方法缺失,我给 dotnet/runtime 项目贡献了相关的 API,可惜要到 .NET7 才能用上. https://github.com/do ...

  2. spring源码分析(二)- 容器基础

    1.基本用法 用过Spring的都知道,bean是Spring中最基础也是最核心的.首先看一个简单的例子. 一个类和一个配置文件 package bean; public class MyBean { ...

  3. 封装一个简单的ajax请求

    记录自己第一次封装ajax,肯定有很多考虑不周到,如有错误请指出,本人必将虚心改正. /** * * @param {Object} obj =>header:请求头:url:请求地址:meth ...

  4. PM技术分享——《构建之法》初步实践

    软件理论 软件=程序+软件工程:软件开发活动(构建管理.源代码管理.软件设计.软件测试.项目管理)相关的内容的完成,才能完成把整个程序转化成为一个可用的软件的过程. 软件企业=软件+商业模式 软件开发 ...

  5. Noip模拟70 2021.10.6

    T1 暴雨 放在第一道的神仙题,不同的做法,吊人有的都在用线段树维护$set$预处理 我是直接$dp$的,可能代码的复杂度比那种的稍微小一点 设$f[i][j][p][0/1]$表示考虑了前$i$列, ...

  6. Noip模拟11 2021.7.11

    T1 math 其实看看题面,看看给的那机组数据即可看出规律了(然而当时并没有,只是发现模数的循环节,存了个vector,接下来就暴力了) 有个柿子: 其实就是裴蜀定理. 然后想一想的话就是 那么只要 ...

  7. 最近公共祖先(lca)与树上叉分

    lca的定义不在过多解释, 代码如下: inline void bfs() { queue<int>q; deep[s]=1;q.push(s); while(!q.empty()) { ...

  8. cf Two Sets (我用二分最大匹配做的)

    题意: n个数p1,p2....pn     两个数a,b 把它们分成A,B两个集合. 若x属于A,a-x一定属于A. 若x属于B,b-x一定属于B. 问是否可能将这n个数分成两个集合.若可以,输出每 ...

  9. Qt5 项目程序打包发布 详细教程

    概述 当我们用QT写好了一个软件,要把你的程序分享出去的时候,不可能把编译的目录拷贝给别人去运行.编译好的程序应该是一个主程序,加一些资源文件,再加一些动态链接库,高大上一些的还可以做一个安装文件. ...

  10. idea离线安装lombok插件

    1.查看自己idea版本,2019.1.2,必须安装相同版本的插件 2.从http://plugins.jetbrains.com/plugin/6317-lombok-plugin中下载对应版本的l ...