【图论】bnuoj 52810 Splitting the Empire
acm.bnu.edu.cn/v3/contest_show.php?cid=9208#problem/G
【题意】
- 给定一个无向图,要求把这个无向图的点划分到不同的集合里,使得每个集合的点之间两两没有边相连
- 求最少划分到多少个集合
【思路】
- 相当于给一个图染色,相邻点染不同色(在不同集合),最少用多少种颜色
- dfs染色,每个点选择能选择的编号最小的颜色
【AC】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; const int maxn=5e4+;
const int maxm=1e6+;
int n,m,k;
struct edge
{
int to;
int nxt;
}e[maxm];
int head[maxn];
int tot;
bool vis[maxn];
int color[maxn];
bool tmp[maxn];
int ans;
void init()
{
memset(head,-,sizeof(head));
tot=;
ans=;
memset(vis,false,sizeof(vis));
memset(color,,sizeof(color));
} void add(int u,int v)
{
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
} void dfs(int u,int pa)
{
memset(tmp,false,sizeof(tmp));
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
tmp[color[v]]=true;
}
int c=-;
for(int i=;i<=n;i++)
{
if(!tmp[i])
{
c=i;
break;
}
}
color[u]=c;
ans=max(ans,c);
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(v==pa) continue;
if(vis[v]) continue;
vis[v]=true;
dfs(v,u);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
int u,v;
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
// int flag=0;
for(int i=;i<=n;i++)
{
if(!vis[i])
{
vis[i]=true;
dfs(i,-);
}
}
cout<<ans<<endl;
}
return ;
}
dfs
【图论】bnuoj 52810 Splitting the Empire的更多相关文章
- [leetcode] 题型整理之图论
图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...
- 关于 Word Splitting 和 IFS 的三个细节
在 Bash manual 里叫 Word Splitting,在 Posix 规范里叫 Field Splitting,这两者指的是同一个东西,我把它翻译成“分词”,下面我就说三点很多人都忽略掉(或 ...
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...
- bnuoj 24251 Counting Pair
一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...
- 并查集(图论) LA 3644 X-Plosives
题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...
- NOIp 2014 #2 联合权值 Label:图论 !!!未AC
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- HDU 4435 charge-station () bfs图论问题
E - charge-station Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 4435 charge-station bfs图论问题
E - charge-station Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- bnuoj 44359 快来买肉松饼
http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms Case Time Lim ...
随机推荐
- 洛谷 P1330 封锁阳光大学
题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M ...
- 状态压缩---区间dp第一题
标签: ACM 题目 Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is ...
- CPP-网络/通信:POST
BOOL PostSubmit(CString strUrl,const CString&strPara, CString&strContent){ BOOL bRet=FALSE; ...
- spring boot 下 dataTable|pagehelper 组合进行分页 筛选 排序
1)Js 需提前引用 jquery.dataTables $(function () { //提示信息 初始化设置 一般不需要改 var lang = { "sProcessing" ...
- js中声明函数的三种方式
己亥年 庚午月 癸巳日 宜入宅 忌婚嫁 函数声明方式 声明 : function first(){}: 调用:first() 函数表达式声明方式 声明: var second=function ...
- Dubbo服务的搭建
dubbo框架主要作用是基于RPC的远程调用服务管理,但是注册中心是用的zookeeper,搭建dubbo,首先要安装zookeeper,配置zookeeper... 实现功能如图所示:(存在2个系统 ...
- Vue构建项目
构建Vue项目 按照官网教程安装 //先安装脚手架 cnpm i -g vue-cli //查看项目目标列表: webpack browserify pwa 等项目模板 vue list //使用we ...
- 关于PHP版本比较函数version_compare的问题
$version1="v4.0"; $version2="v4.0.0"; print_r(version_compare($version1,$version ...
- ECshop二次开发 ECSHOP首页显示积分商城里的商品
以ECSHOP2.7.2官方默认模板为基础 1).首先打开 index.php 文件,在最末尾增加下面函数,注意千万不要写到 “?>” 的外面去,要加在“?>”的前面,加以下代码: /** ...
- verilog behavioral modeling --procedural assignments
1.procedural assignments are used for updating reg ,integer , time ,real,realtime and memory data ty ...