【判断二分图】C. Catch】的更多相关文章

Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 423    Accepted Submission(s): 161 Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration mor…
785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图. graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点.每个节点都是一个在0到graph.length-1之间的整数.这图中没有自环和平行边: graph[i] 中不存在i,并且graph[i]中没有重复的值. 示例 1: 输入: [[1,3], […
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/C [题意] 给定一个无向图,给定小偷的起始位置 从这个起始位置开始,小偷可以在单位时间内逃到相邻的点 问是否存在某一时刻,小偷可能到达这个无向图的任意点 [思路] 首先这个无向图必须是连通的 如果小偷能够在某一奇数步到达某一点,那么他可以在任意奇数步到达这一点:偶数步同理 如果这个图是一个二分图(即奇数步到达的点在一个集合,偶数步到达的点在一个集合),那么对于任意时刻(不是奇数…
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply t…
The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C k…
#include <iostream> #include <cstdio> #include <string.h> #include <vector> #define inf 0xfffffff using namespace std; const int maxn = 1e5 + 1e2; const int maxp = 1e3 + 1e2; //邻接表vector实现 vector<int> G[maxp]; int color[maxp]…
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8418    Accepted Submission(s): 3709 Problem Description There are a group of students. Some of them may know each ot…
题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该环形的边的数量为偶数).暂时不证明,后证. 那么怎么判断一个图的回路是奇环还是偶环呢? 交叉染色法. 随机选择一个点,染成红色,把所有跟它相邻的点染成绿色,再由被染色的绿点出发,把相邻的点染成红色…… 即对于一个点和他相邻的点(两个点之间有边相连叫做相邻),颜色必定不同,如果相同,那么是奇环. 例如…
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5225    Accepted Submission(s): 2374 Problem Description There are a group of students. Some of them may know each ot…
d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍历整个图,那么编程要注意了,应该从每个点出发遍历一次. s2.带权并查集来判断,略复杂.先略过.先上个博客:http://blog.csdn.net/zsc09_leaf/article/details/6727622 c.邻接矩阵,bfs #include<iostream> #include&…
给一个无向图,判断是否为二分图 是否有奇环.用染色法解决,也可以用并查集解决,时间复杂度是相同的,但是并查集用的内存少. 这里给出Bfs染色的代码 #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<vector> #include<queue> using namespace st…
http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011/10/19/2217911.html #include "stdio.h" #include "string.h" #define N 1010 int time; int n,m; bool map[N][N]; struct node { int x,y; //i…
题意:第一行给出数字n个学生,m条关系,关系表示a与b认识,判断给定数据是否可以构成二分图,如果可以,要两个互相认识的人住一个房间,问最大匹配数(也就是房间需要的最小数量) 思路:要看是否可以构成二分图,就是二分图的判断,这里主要记录一下自己想的判断方法:用一个数组记录分类,0表示这个人没有被分类,1,2表示二分图两个部分,边输入边判断是否是二分图,如果两个人都是0,那么将这两个人分别赋值1,2即可,若两人数值不相同,但有一个为0,将为零的这个赋值1或2,,若两人值相等且不为0,说明矛盾了,fl…
D. Vitaly and Cycle       time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output After Vitaly was expelled from the university, he became interested in the graph theory. Vitaly especially liked th…
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are given all pairs of students who know…
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.…
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each o…
1. 具体题目 给定一个无向图graph,当这个图为二分图时返回true.如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图.graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点.每个节点都是一个在0到graph.length-1之间的整数.这图中没有自环和平行边: graph[i] 中不存在i,并且graph[i]中没有重复的值. 示例 1: 输入: [[1,3], [0,2],…
#include<stdio.h> #include<string.h> #include<queue> using namespace std; #define maxn 210 int map[maxn][maxn],color[maxn]; int vis[maxn],match[maxn],n; int bfs(int u,int n) { int i; queue<int>q; q.push(u); color[u]=; while(!q.empt…
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4091    Accepted Submission(s): 1876 Problem Description There are a group of students. Some of them may know each ot…
Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0,2], [1,3], [0,2]] Output: true Explanation: The graph looks like this: 0----1 | | | | 3----2 We can divide the vertices into two groups: {0, 2} and {1…
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3775    Accepted Submission(s): 1771 Problem Description There are a group of students. Some of them may know each ot…
/*1A 31ms*/ #include<stdio.h> #include<string.h> #define N 300 int n; struct node { int u,v,next; }bian[N*N*2]; int color[N],vis[N],link[N],visit[N],ma[N][N],f[N],head[N],yong; void addedge(int u,int v) { bian[yong].u=u; bian[yong].v=v; bian[y…
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8939    Accepted Submission(s): 3925 Problem DescriptionThere are a group of students. Some of them may know each othe…
和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<map> using namespace std; typedef long…
题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No. 首先判断是否为二分图,然后判断最大匹配 Sample Input 4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6Sample Output No 3 #include<iostream> #include<string.h> #include…
用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 但是如果这个图不是二分图,那么就会这样 把与1相邻的点2,3染成白色,然后入队列,然后2出队列,要把与2相邻的点2,3染成黑色,但是都染过了,所以不用染色 但是3的颜色应该与2相反(如果是二分图的话),可是没有相反,所以就不是二分图 #include <stdio.h> #include <…
题意 首先判断所有的人可不可以分成两部分,每部分内的所有人都相互不认识.如果可以分成 则求两部分最多相互认识的对数. 解题 类似分成两组,同组互不相关,就可能使判断是否为二分图 能否分成两部分 则是判断是否是一个二分图.无向图G为二分图的充分必要条件是:G至少有两个顶点,且当存在回路时,其所有回路的长度均为偶数.回路就是环路,也就是判断是否存在奇数环.判断二分图方法:用染色法,把图中的点染成黑色和白色.首先取一个点染成白色,然后将其相邻的点染成黑色,如果发现有相邻且同色的点,那么就退出,可知这个…
圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇圈里(由奇数个点组成的环)每个点都是可以参加的.而一个奇圈一定在点双连通分量里,所以我们把原图的每个点双连通分量找出来,然后判断是否有奇圈.用到了几个引理: 非二分图至少有一个奇圈. 点双连通分量如果有奇圈,那么每个点都在某个奇圈里(不一定是同一个). 于是问题转化为对每个点双连通分量,判断它是不是…
题目链接 题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. 分析:以骑士为借点建立无向图G,如果两个骑士可以相邻(即不相互憎恶)建立一条有向边,题目转化成求不在任意一个简单奇圈上(包含奇数个节点的回路)的节点个数, 对于每个双连通分量,看是否存在奇环,若存在那么这个双连通分量中的任意两骑士都可以同时出现在一个奇环里:训练指南P317解释, 双联通分量B中有一…