原文链接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 其他的更多相关文章

  1. Codeforces.542E.Playing on Graph(二分图)

    题目链接 \(Description\) 给出一个n个点m条边的无向图. 你每次需要选择两个没有边相连的点,将它们合并为一个新点,直到这张图变成了一条链. 最大化这条链的长度,或输出无解. n< ...

  2. cf 542E - Playing on Graph

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

  3. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

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

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

  5. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  6. CodeForces 176C Playing with Superglue 博弈论

    Playing with Superglue 题目连接: http://codeforces.com/problemset/problem/176/C Description Two players ...

  7. 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法

    Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  8. codeforces gym100801 Problem G. Graph

    传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...

  9. codeforces 21D:Traveling Graph

    Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...

随机推荐

  1. freeswitch反注册记录

    应用情景: 使用阿里服务器,落地使用本地的模拟线路(O口网关). 1.FreeSWITCH 服务器开一个账号,比如 5000 internal , O口 SIP设置页面按照网关注册 5000 的账号信 ...

  2. android开机动画(bootanimation)

    Android开机动画有两种修改方法,android 2.0及之后,使用bootanimation程序显示开机画面,如需修改开机画面,不用修改代码,只需按格式要求做bootanimation.zip包 ...

  3. Selenium+Java自动化之如何优雅绕过验证码

    前言: 验证码问题对于每个ui自动化的同学而言,相信都是个蛋疼的问题,对于验证码的处理我个人不提倡破解,不要去想破解方法,这个验证码本来就是为了防止别人自动化登录的.如果你们公司的验证码很容易被你破解 ...

  4. [PHP]flock文件IO锁的使用

    一.flock概述 bool flock  ( resource $handle  , int $operation  [, int &$wouldblock  ] ) 参数 handle 文 ...

  5. VUE 脚手架项目搭建

    1. 概述 1.1 说明 vue-cli是一个官方发布vue.js项目脚手架,使用vue-cli可以快速创建vue项目.GitHub地址是:https://github.com/vuejs/vue-c ...

  6. android经典源码,很不错的开源框架

    高仿最美应用项目源码 项目介绍 这是仿最美应用开发的基于mvp+rxjava+retrofit的项目,很值得学   github地址: https://github.com/JJOGGER/Beaut ...

  7. Google开发者大会:你不得不知的Tensorflow小技巧

    Google开发者大会:你不得不知的Tensorflow小技巧 同步滚动:开   Google Development Days China 2018近日在中国召开了.非常遗憾,小编因为不可抗性因素滞 ...

  8. python 列表,字典,元组,字符串,常用函数

    飞机票 一.列表方法 1.ls.extend(object) 向列表ls中插入object中的每个元素,object可以是字符串,元组和列表(字符串“abc”中包含3个元组),相当于ls和object ...

  9. Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:deploy (default-cli) on project Resource: Cannot invoke Tomcat manager: Connection refused: connect -> [Help 1]

    1.问题描述 在 DOS 下执行 tomcat7-maven-plugin 插件部署,启动 Apache Tomcat 服务报错如下: D:\2018\code\XXX>mvn tomcat7: ...

  10. Confluence 6 计划任务

    管理员控制台能够允许你对 Confluence 运行的计划任务进行计划的调整,这些计划任务将会按照你的调整按时执行.可以按照计划执行的任务如下: Confluence 站点备份 存储优化任务,清理 C ...