解法参考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. JS同行绑定事件

    <td><a class="blue" href="javascript:void(0);" class="blue" s ...

  2. python mqtt 客户端实现

    安装paho-mqtt pip install paho-mqtt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com py ...

  3. shell input value from console

    echo "Please enter some input: " read input_variable echo "You entered: $input_variab ...

  4. 简单递归____Fibonacci数列

    #include <stdio.h> int fun(int x) { ||x==) ; else return fun(x-1)+fun(x-2); } int main() { int ...

  5. 力扣—— Swap Nodes in Pairs(两两交换链表中的节点) python实现

    题目描述: 中文: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, ...

  6. Nginx有哪些作用?

    Nginx有哪些作用? http协议代理 搭建虚拟主机 服务的反向代理 在反向代理中配置集群的负载均衡   什么是正向代理? 正向代理,意思是一个位于客户端和原始服务器(origin server)之 ...

  7. ORACLE 11G新特性之一(增加带default的字段)

    在11g之前,增加带default值的字段,实现原理如下: alter table t1 add c1 varchar2(20) default 'XX' not null; 假设t1表有4千万行数据 ...

  8. Docker部署Flask应用

    创建应用 首先,编写一个简单的Flask应用:docker_test/flask_app.py Docker 安装 请根据自己的操作系统自行安装. Docker简介 Docker 镜像 Docker镜 ...

  9. django 邮箱发送

    在django中提供了邮件接口 QQ邮箱配置 qq邮箱地扯:https://mail.qq.com settings文件 # 邮箱配置 EMAIL_USE_SSL = True EMAIL_HOST ...

  10. CF 1150 D Three Religions——序列自动机优化DP

    题目:http://codeforces.com/contest/1150/problem/D 老是想着枚举当前在给定字符串的哪个位置,以此来转移. 所以想对三个串分别建 trie 树,然后求出三个t ...