Divide Groups(分组)
2024-08-22 06:50:50
题目大意是说输入数字n
然后告诉你第i个人都认识谁?
让你把这些人分成两堆,使这每个堆里的人都互相认识。
做法:把不是互相认识的人建立一条边,则构建二分图,两堆的人肯定都互相认识,也就是说,互相认识的两个人肯定不相连。
——代码
#include <cstdio>
#include <cstring> using namespace std; int n, cnt;
int head[], to[], next[], color[];
bool know[][]; void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
}
//把点染成1或-1
bool dfs(int u, int c)
{
int i, v;
color[u] = c;
for(i = head[u]; i != - ; i = next[i])
{
v = to[i];
if(color[v] == c) return ;
if(color[v] == && !dfs(v, -c)) return ;
}
return ;
} bool solve()
{
int i;
for(i = ; i <= n; i++)
if(color[i] == && !dfs(i, ))
return ;
return ;
} int main()
{
int i, j, x;
while(~scanf("%d", &n))
{
memset(know, , sizeof(know));
memset(color, , sizeof(color));
for(i = ; i <= n; i++)
while(scanf("%d", &x) && x)
know[i][x] = ;
memset(head, -, sizeof(head));
for(i = ; i <= n; i++)
for(j = i + ; j <= n; j++)
if(!know[i][j] || !know[j][i])
{
add(i, j);
add(j, i);
}
if(solve()) printf("YES\n");
else printf("NO\n");
}
return ;
}
最新文章
- 轻松3步实现c#windowsform窗体美化
- google的云盘与公司网盘
- @synthesize obj = _obj 理解
- 从一个标准URL中提取文件的扩展名
- JAVA中的deflate压缩实现
- javascript写的新闻滚动代码
- Proud Merchants(POJ 3466 01背包+排序)
- Composer更新慢的解决方案
- Java基础随记-不定时更新
- js的简单介绍
- python自动化测试入门篇-jemter
- Kubernetes 安装
- MySQL 关于索引那点事
- maven POM.xml内的标签大全详解
- “融而开放、合以创新”T-HIM融合通信技术开发实战
- 【mysql】测试方案整理
- 嵌入式驱动开发之uboot---uboot 中的常见命令参数参数
- java轻量级IOC框架Guice(转)
- 牛客网提高组模拟赛第七场 T3 洞穴(附bitset介绍)
- 1 如何使用pb文件保存和恢复模型进行迁移学习(学习Tensorflow 实战google深度学习框架)