【HDU4751】Divide Groups
题目大意:给定 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的更多相关文章
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【Leetcode】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. class Solution { public ...
- 【Leetcode_easy】893. Groups of Special-Equivalent Strings
problem 893. Groups of Special-Equivalent Strings 题意: 感觉参考代码也是有点问题的... 参考 1. Leetcode_easy_893. Grou ...
- 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【HDU4301】Divide Chocolate
题意 有一块n*2的巧克力,将它分成k块,问有多少种方法. 分析 emmm是dp没错了. 最容易想到的状态定义是f[i][j],意思是前i行,分成j块的方案数.但是发现没法转移.(后面会说一下为什么· ...
- 【基数排序】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 ...
随机推荐
- Docker for Win10中文乱码问题
environment:win10 docker+centos7+nginx1.9.9 issue:在docker运行nginx(centos),volume本地html目录挂载到nginx的htm ...
- 当我们按下电源键,Android 究竟做了些什么?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由goo发表于云+社区专栏 相信我们对Android系统都不陌生,而Android系统博大精深,被各种各样的智能设备承载的同时,我们会否 ...
- win10系统关闭自动更新
win10关闭自动更新 步骤①右键“此电脑”选择“管理”选项 步骤②(如下图所示): 步骤③: 步骤④: 好啦!这样就大功告成了!
- man -f/-k [keyword]在fedora 29 中报错nothing appropriate
我们在使用 man 手册的时候,可以使用man -f [keyword]去查询keyword的在线文档,但是这时候会报错:(图来源自网络) 这是因为我们还没有建立 man 手册的索引缓存: 我们可以使 ...
- Log4j分级别保存日志到单个文件中,并记录IP和用户信息
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration S ...
- Python操作db2
官方文档:https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.5.0/com.ibm.db2.luw.apdv.python.doc/doc ...
- android的listview以及画线--to thi tha
https://www.cnblogs.com/896240130Master/p/6135165.html 这个的 https://www.jianshu.com/p/5522470760c1
- c# 正则验证
1.验证百分数 bool tempBool = Regex.IsMatch(str, @"[1-9]{0,1}[0-9](\\.[0-9])?%");
- Jetson TX2(3)opencv3 打开usb摄像头
ubuntu2604 opencv3.4.0 https://blog.csdn.net/ultimate1212/article/details/80936175?utm_source=blogxg ...
- MongoDB 创建基础索引、组合索引、唯一索引以及优化
一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引 ...