题目大意:给定 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. git 的 origin 的含义

    我们从progit 一书中可以看到: 远程仓库名字 “origin” 与分支名字 “master” 一样,在 Git 中并没有任何特别的含义一样. 同时“master”是当你运行git init时默认 ...

  2. MongoDB 创建索引的语法

    1.为普通字段添加索引,并且为索引命名 db.集合名.createIndex( {"字段名": 1 },{"name":'idx_字段名'}) 说明: (1)索 ...

  3. 【公众号系列】超详细SAP HANA JOB全解析

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[公众号系列]超详细SAP HANA JOB全解 ...

  4. Saltstack_使用指南07_远程执行-执行模块

    1. 主机规划 远程执行教程文档 https://docs.saltstack.com/en/latest/topics/tutorials/modules.html 所有模块文档 https://d ...

  5. iOS Accessibility指南

    开发者经常会为用户开发一些令人充满惊喜的App.但是,开发者真的为每一个潜在的用户都做适配了么?是否每个人都可以真正使用你的APP呢? 设计APP.产品或者任何类型的服务,都要考虑到所有用户,包括视力 ...

  6. #020PAT 没整明白的题L1-009 N个数求和 (20 分)

    后面的测试点过不去,两个错误一个超时. 目前未解决   L1-009 N个数求和 (20 分)   本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和 ...

  7. Linux Collection:系统信息和配置

    PAS Linux系统配置相关文件 /etc 目录中是系统和各类软件的配置文件 /var/log 系统日志 /proc 系统运行信息 PAS /boot空间不足 查看内核 dpkg --get-sel ...

  8. Python进程池Pool

    ''' 进程池,启动一个进程就要克隆一份数据,假设父进程1G,那么启动进程开销很大 避免启动太多造成系统瘫痪,就有进程池,即同一时间允许的进程数量 ps:线程没有池,因为线程启动开销小,线程有类似信号 ...

  9. Mysql共享锁、排他锁、悲观锁、乐观锁及其使用场景

    一.相关名词 |--表级锁(锁定整个表) |--页级锁(锁定一页) |--行级锁(锁定一行) |--共享锁(S锁,MyISAM 叫做读锁) |--排他锁(X锁,MyISAM 叫做写锁) |--悲观锁( ...

  10. Resolving Issues of "Library Cache Pin" or "Cursor Pin S wait on X" (Doc ID 1476663.1)

    Doc ID 1476663.1) To Bottom In this Document   Purpose   Troubleshooting Steps   Brief Definition:   ...