Codeforces 542E Playing on Graph 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/CF542E.html
题目传送门 - CF542E
题目传送门 - 51Nod1481
题意
有一幅无向图,它有n个点,m条边,没有自环和重边。定义合并操作,这个合并操作是把两个没有边直接连接的点合并成一个新点,把和旧的两个点至少有一个有边的点和这个新点连边。然后原来的两个旧点删除。这样就把n个点的无向图变成了n-1个点的无向图。
现在,要求你对这个图进行合并操作,最后形成一条链,而且这个链要尽可能的长。一条长度为k的链(k ≥ 0)是由k+1个点和k条边构成的,而且第i个点和第(i+1)个点要有一条边相连(1 ≤ i ≤ k)。特别的,只有一个点的图,是一条长度为0的链。经过合并出来的新点可以再次参加合并操作。
上图解释了一次合并操作,被合并的两个点用红色标出了。
题解
首先,如果原图存在奇环,那么必然是 -1 ,因为合并到最后会变成一个无法合并的三元环。
否则,图就是一个二分图。
考虑对于每一个连通块分别求解。
对于一个连通块的任意一个 bfs 生成树,必然可以把它缩成一条长度为 bfs 深度的链。于是我们对于每一个连通块枚举一下起点就好了。
最终答案就是所有连通块的答案相加。
时间复杂度 $O(nm)$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int read(){
int x=0;
char ch=getchar();
while (!isdigit(ch))
ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x;
}
const int N=1005;
int n,m;
vector <int> e[N],s;
int c[N],vis[N];
int dfs(int x,int d){
if (~c[x])
return c[x]==d;
s.push_back(x);
c[x]=d;
for (auto y : e[x])
if (!dfs(y,d^1))
return 0;
return 1;
}
int q[N],dis[N],head,tail;
int dfs2(int s){
memset(vis,0,sizeof vis);
head=tail=0;
q[++tail]=s,dis[s]=0,vis[s]=1;
while (head<tail){
int x=q[++head];
for (auto y : e[x])
if (!vis[y])
vis[y]=1,dis[y]=dis[x]+1,q[++tail]=y;
}
return dis[q[tail]];
}
int main(){
n=read(),m=read();
for (int i=1;i<=m;i++){
int a=read(),b=read();
e[a].push_back(b);
e[b].push_back(a);
}
memset(c,-1,sizeof c);
int ans=0;
for (int i=1;i<=n;i++)
if (!~c[i]){
s.clear();
if (!dfs(i,0))
return puts("-1"),0;
int now=0;
for (auto S : s)
now=max(now,dfs2(S));
ans+=now;
}
printf("%d",ans);
return 0;
}
Codeforces 542E Playing on Graph 其他的更多相关文章
- Codeforces.542E.Playing on Graph(二分图)
题目链接 \(Description\) 给出一个n个点m条边的无向图. 你每次需要选择两个没有边相连的点,将它们合并为一个新点,直到这张图变成了一条链. 最大化这条链的长度,或输出无解. n< ...
- cf 542E - Playing on Graph
cf 542E - Playing on Graph 题目大意 给定一个\(n\le 1000\)个点的图 求经过一系列收缩操作后能否得到一条链,以及能得到的最长链是多长 收缩操作: 选择两个不直接相 ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- 【Codeforces542E】Playing on Graph [Bfs][Dfs]
Playing on Graph Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 5 4 ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- CodeForces 176C Playing with Superglue 博弈论
Playing with Superglue 题目连接: http://codeforces.com/problemset/problem/176/C Description Two players ...
- 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- codeforces gym100801 Problem G. Graph
传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...
- codeforces 21D:Traveling Graph
Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...
随机推荐
- Directory 中user Var 如何添加到通道变量中?
FS默认的配置,ACL 是 拒绝的,只能通过 Digest 的方式进行认证,一旦认证成功之后,directory 中的 var 就能在通道中通过${} 的方式获取到. 如果ACL 认证通过 ,就直接走 ...
- 压缩JS的eclipse插件
主页:http://jscompressor.oncereply.me/ Update site: http://jscompressor.oncereply.me/update/
- java泛型类型变量能调用的方法
public class Person { } public class Student extends Person{ private String name; public Student(Str ...
- Golang并发模型之Context详解
对于 Golang 开发者来说context(上下文)包一定不会陌生.但很多时候,我们懒惰的只是见过它,或能起到什么作用,并不会去深究它. 应用场景:在 Go http 包的 Server 中,每一个 ...
- swift 实践- 04 -- UIButton
import UIKit class ViewController: UIViewController { // 按钮的创建 // UIButtonType.system: 前面不带图标, 默认文字为 ...
- SecureCRT中sqlplus,使用Backspace删除时 ^H^H
平时习惯用Backspace删除输入错误,但是在SecureCRT中使用是,却是: SQL> sele^H^H 网上有几个方法,觉得改SecureCRT的配置最方便.
- Confluence 6 已经存在的安装配置数据库字符集编码
针对已经存在的 Confluence 安装,如果你安装的 Confluence 版本是 6.4 或者早期的版本,我们在安装的时候没有检查你数据库的字符设置. 如果你的数据库当前没有被配置使用 UTF- ...
- selenium之 chromedriver与chrome版本映射表(更新至v2.33)
看到网上基本没有最新的chromedriver与chrome的对应关系表,便兴起整理了一份如下,希望对大家有用: chromedriver版本 支持的Chrome版本 v2.33 v60-62 v2. ...
- gnuradio 使用eclipse 编辑器记录
第1步 - 首先安装eclipse 先去官网下载,然后解压 --->下载版本是C++/C 版---->解压--->打开--->help->eclipse marketp ...
- 【mongo】可以用localhost启动,无法用ip启动问题的解决
问题: mongo安装在a.b.c.d机器上,启动mongo服务后, mongo localhost:27017可以进入数据库, mongo 127.0.0.1:27017也可以进入数据库 mongo ...