Codeforces542E Playing on Graph 思维+DFS+BFS
解法参考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的更多相关文章
- 【Codeforces542E】Playing on Graph [Bfs][Dfs]
Playing on Graph Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 5 4 ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- 【427】Graph 实现 以及 DFS & BFS
目录: Graph 实现 二维数组实现 Linked List 实现 DFS:深度优先搜索 stack 实现 recursion 实现 BFS:广度优先搜索(queue) 其他应用 非连通图遍历 - ...
- cf 542E - Playing on Graph
cf 542E - Playing on Graph 题目大意 给定一个\(n\le 1000\)个点的图 求经过一系列收缩操作后能否得到一条链,以及能得到的最长链是多长 收缩操作: 选择两个不直接相 ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- [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 ...
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- DFS/BFS视频讲解
视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...
随机推荐
- SQL数据库—<9>数据库安全
--创建登陆用户 --create login login_name from windows with default_database = database | default_language ...
- 2019-9-29-dotnet-对-DateTime-排序
title author date CreateTime categories dotnet 对 DateTime 排序 lindexi 2019-09-29 14:55:49 +0800 2019- ...
- lsyncd+rsync文件实时同步
1.rsync两端都需要安装 yum -y install rsync 2.提供lsyncd的安装源 rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x ...
- Tomcat访问任意磁盘的图片资源
项目中用户上传的大量图片存放在项目底下带来诸多不便.每次部署项目都需要拷贝出来,防止覆盖掉以前的 图片.容易丢失,前功尽弃.甚至造成经济损失.不可估量. 如何配置tomcat访问图片路径呢?首页你代码 ...
- Python--nfs服务+计划任务crond服务+shell介绍
nfs服务 NFS 是Network File System的缩写,即网络文件系统. 功能是通过网络让不同的机器.不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的 ...
- vue基础七
事件处理器 1.监听事件 可以用 v-on 指令监听 DOM 事件来触发一些 JavaScript 代码. <div id="example-1"> <butto ...
- winform 控件拖拽和缩放
核心类: using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using ...
- Linux系统之-常用命令及技巧
一. 通用命令:1.date :print or set the system date and time2. stty -a: 可以查看或者打印控制字符(Ctrl-C, Ctrl-D, Ctrl-Z ...
- BZOJ 3531: [Sdoi2014]旅行(树链剖分+线段树)
传送门 解题思路 以每个颜色为根开一棵权值线段树,下标就是\(dfs\)序,其余都是基本操作,要动态开点. 代码 #include<iostream> #include<cstdio ...
- Django中的HttpRequsest 和Httpresponse对象
HttpRequest对象:每一个用户请求在到达视图函数的同时,django会自动创建一个HttpRequest对象并把这个对象当做第一个参数传给要调用的views方法,HttpRequest对象里封 ...