题目大意:给定 N 个点和一些有向边,求是否能够将这个有向图的点分成两个集合,使得同一个集合内的任意两个点都有双向边联通。

题解:反向思考,对于没有双向边的两个点一定不能在同一个集合中。因此,构建一个图,若两点之间有边,则表示这两个点不能在同一个集合中。进行二分图染色判定即可,若是二分图,则满足条件,反之则不满足。

代码如下

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=110; int n;bool mp[maxn][maxn];
vector<int> G[maxn];
int cor[maxn]; void read_and_parse(){
memset(mp,0,sizeof(mp));
memset(cor,0,sizeof(cor));
for(int i=1;i<=n;i++)G[i].clear();
for(int i=1,to;i<=n;i++)while(scanf("%d",&to)&&to)mp[i][to]=1;
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j&&(!mp[i][j]||!mp[j][i]))G[i].pb(j);
} bool dfs(int u,int c){
cor[u]=c;
for(auto v:G[u]){
if(cor[v]==c)return 0;
if(!cor[v]&&!dfs(v,3-c))return 0;
}
return 1;
} void solve(){
for(int i=1;i<=n;i++)if(!cor[i]&&!dfs(i,1))return (void)puts("NO");
puts("YES");
} int main(){
while(scanf("%d",&n)!=EOF){
read_and_parse();
solve();
}
return 0;
}

【HDU4751】Divide Groups的更多相关文章

  1. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  2. 【Leetcode】 - Divide Two Integers 位运算实现整数除法

    实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...

  3. 【leetcode】Divide Two Integers (middle)☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  4. 【Leetcode】【Medium】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  5. 【Leetcode】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. class Solution { public ...

  6. 【Leetcode_easy】893. Groups of Special-Equivalent Strings

    problem 893. Groups of Special-Equivalent Strings 题意: 感觉参考代码也是有点问题的... 参考 1. Leetcode_easy_893. Grou ...

  7. 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【HDU4301】Divide Chocolate

    题意 有一块n*2的巧克力,将它分成k块,问有多少种方法. 分析 emmm是dp没错了. 最容易想到的状态定义是f[i][j],意思是前i行,分成j块的方案数.但是发现没法转移.(后面会说一下为什么· ...

  9. 【基数排序】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C. Jon Snow and his Favourite Number

    发现值域很小,而且怎么异或都不会超过1023……然后可以使用类似基数排序的思想,每次扫一遍就行了. 复杂度O(k*1024). #include<cstdio> #include<c ...

随机推荐

  1. iOS transform属性的使用

    1.transform属性 在iOS开发中,通过transform属性可以修改UIView对象的平移.缩放比例和旋转角度,常用的创建transform结构体方法分两大类 (1) 创建“基于控件初始位置 ...

  2. spring boot 中使用 jpa以及jpa介绍

    1.什么是jpa呢?JPA顾名思义就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.12.jpa具有什么 ...

  3. centos7.4系统升级kernel内核

    在实验环境下,已安装了最新的CentOS 7.4操作系统,现在需要升级内核版本. 实验环境CentOS-7-x86_64-Minimal-1708.iso CentOS Linux release 7 ...

  4. PSQLException: FATAL: no pg_hba.conf entry for host "127.0.0.1", user "ambari", database "ambari", SSL off

    On your Postgres server, you will need to update your pg_hba.conf file to allow access for the ambar ...

  5. Hystrix是个什么玩意儿

    1. 什么是Hystrix Hystrix是Netflix的一个开源框架,地址如下:https://github.com/Netflix/Hystrix 中文名为“豪猪”,即平时很温顺,在感受到危险的 ...

  6. Redhat7.3更换CentOS7 yum源

    Redhat yum源是收费的,没有注册的Redhat机器是不能使用yum源的. 1.当前系统环境: 系统版本:Red Hat Enterprise Linux Server release 7.3 ...

  7. Ubuntu16.04中php如何切换版本

    其实就是一条Linux命令,如下: sudo update-alternatives --config php 会出现下面选项: There are choices for the alternati ...

  8. C++ SIMD

    SIMD Single Instruction Multiple Data

  9. VirtualBox修改UUID实现虚拟硬盘的重复利用

    其实,记录这个是为了留给自己看.每次用每次查,已经老到什么东西都记不住了.本次查询是从这里(VirtualBox 修改UUID实现虚拟硬盘复制)获得帮助的,感谢. 在VirtualBox把一个已经使用 ...

  10. 即将发布的 ASP.NET Core 2.2 会有哪些新玩意儿?

    今年 6 月份的时候时候 .NET 团队就在 GitHub 公布了 ASP.NET Core 2.2 版本的 Roadmap(文末有链接),而前两天 ASP.NET Core 2.2 预览版 2 已经 ...