解法参考https://www.cnblogs.com/BearChild/p/7683114.html这位大佬的,这位大佬讲得很好了。

这道题还是有一定的思维的。

直接贴代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int N=+;
const int M=+;
int n,m,ecc_cnt,ans,color[N],Max[N],ecc[N];
vector<int> G[N];
bool ok; void dfs(int x,int c) {
color[x]=c; ecc[x]=ecc_cnt;
for (int i=;i<G[x].size();i++) {
int y=G[x][i];
if (color[y]==color[x]) ok=false;
if (!color[y]) dfs(y,-c);
}
} queue<int> q;
int d[N];
int bfs(int s) {
while (!q.empty()) q.pop();
memset(d,,sizeof(d));
d[s]=; q.push(s);
int res=;
while (!q.empty()) {
int x=q.front(); q.pop();
for (int i=;i<G[x].size();i++) {
int y=G[x][i];
if (!d[y]) {
d[y]=d[x]+;
res=max(res,d[y]);
q.push(y);
}
}
}
return res;
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=m;i++) {
int x,y; scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
} ecc_cnt=; ok=true;
for (int i=;i<=n;i++)
if (!ecc[i]) {
ecc_cnt++;
dfs(i,);
}
if (!ok) { puts("-1"); return ; } for (int i=;i<=n;i++)
Max[ecc[i]]=max(Max[ecc[i]],bfs(i));
for (int i=;i<=ecc_cnt;i++) ans+=Max[i]-;
cout<<ans;
return ;
}

Codeforces542E Playing on Graph 思维+DFS+BFS的更多相关文章

  1. 【Codeforces542E】Playing on Graph [Bfs][Dfs]

    Playing on Graph Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 5 4 ...

  2. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  3. 【427】Graph 实现 以及 DFS & BFS

    目录: Graph 实现 二维数组实现 Linked List 实现 DFS:深度优先搜索 stack 实现 recursion 实现 BFS:广度优先搜索(queue) 其他应用 非连通图遍历 - ...

  4. cf 542E - Playing on Graph

    cf 542E - Playing on Graph 题目大意 给定一个\(n\le 1000\)个点的图 求经过一系列收缩操作后能否得到一条链,以及能得到的最长链是多长 收缩操作: 选择两个不直接相 ...

  5. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  6. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  7. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  9. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

随机推荐

  1. elasticsearch 嵌套对象之嵌套类型

    nested类型是一种特殊的对象object数据类型(specialised version of the object datatype ),允许对象数组彼此独立地进行索引和查询. 1. 对象数组如 ...

  2. Tkinter初体验

    一.基本步骤 1.导入Tkinter模块 2.创建根窗口 3.填充组件 4.组件关联逻辑 5.进入主循环 二.Code #coding:utf-8 ''' 网关流量校验器 @author: Hongz ...

  3. 将word文档中的回车符进行删除

  4. Dubbox管理中心的部署及使用

    安装: 我们在开发时,需要知道注册中心都注册了哪些服务,以便我们开发和测试.我们可以通过部署一个管理中心来实现.其实管理中心就是一个web应用,部署到tomcat即可. (1)编译源码,得到war包 ...

  5. Java网络编程:什么是Socket编程?

    所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 我们开发的网络应用 ...

  6. springboot基于注解动态配置多数据源以及多数据源的事务统一

    参考文档:https://www.cnblogs.com/zhangboyu/p/7622412.html https://blog.csdn.net/qq_34322777/article/deta ...

  7. 转 HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解

    HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...

  8. 【UML】最简单的类图

    Rational Rose简明实用教程  https://blog.csdn.net/gz153016/article/details/49641847 Rational Rose是Rational公 ...

  9. python--表达式形式的yield、面向过程编程、内置函数

    yield的表达式形式 def init(func): def wrapper(*args, **kwargs): g = func(*args, **kwargs) next(g) return g ...

  10. Alpha冲刺阶段博客目录

    Alpha冲刺阶段博客目录 Scrum Meeting 时间 链接 内容 第六周 https://www.cnblogs.com/error0/p/11815255.html 需求分析 第七周 htt ...