cf 542E - Playing on Graph
cf 542E - Playing on Graph
题目大意
给定一个\(n\le 1000\)个点的图
求经过一系列收缩操作后能否得到一条链,以及能得到的最长链是多长
收缩操作:
选择两个不直接相连的点,构造一个新点
对于原图中的每一个点,如果它与这两个点中的任意一个有连边
那么它向这个新点连一条边
最后删除选择的两个点,以及所有这两个点的连边

分析
注意到对于一个奇环,无论怎么收缩,都会产生一个新的奇环,最后奇环变成一个三角形,再也缩不了,永远无法变成一条链
只有偶环的图是二分图
对于图中的一个子图,它是二分图,我们就能构造一组解
选择一个点作为起点,从该点出发得到一棵\(bfs\)树
由于是二分图,\(bfs\)树同一层节点之间没有连边
由于是\(bfs\)树,每个点的连边只能在相邻一层的范围内,且必定和上一层节点有连边
那么我们把同一层节点缩在一起,就可以得到一条链
按照这种方法,图的直径就是答案(图的直径为最短路中的最大值)
直径时最优解可以用归纳法证明:
对于\(1\)个点的任意图显然成立
假设对于\(n\)个点的任意图成立,那么对于\(n+1\)个点的任意图
如果图中不存在环,那么是一棵树,直径显然是答案
否则,至少进行一次收缩,则答案为缩掉一对点后的直径中最大是多少
缩掉一个点后的直径一定不比原来的直径大
而原来的直径能构造出一组解
连通块间的答案是相加的:
对于图中的所有联通块,每块缩成一条链后,链可以两两合并
做法
先判断是不是二分图
然后每个联通块求一次图的直径
然后所有联通块的答案加起来
图的直径求法是每个点出发跑一次\(bfs\)
solution
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int M=1007;
const int N=1e5+7;
inline int ri(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
return f?x:-x;
}
int n,m;
struct vec{
int g[M],te;
struct edge{
int y,nxt;
edge(int _y=0,int _nxt=0){y=_y,nxt=_nxt;}
}e[N<<1];
vec(){memset(g,0,sizeof g);te=0;}
inline void push(int x,int y){e[++te]=edge(y,g[x]);g[x]=te;}
inline void push2(int x,int y){push(x,y);push(y,x);}
inline int& operator () (int x){return g[x];}
inline edge& operator [] (int x){return e[x];}
}e;
int vis[M],ok,ans;
vector<int>pt;
int d[M];
void dfs(int x){
int p,y;
for(p=e(x);p;p=e[p].nxt){
y=e[p].y;
if(!vis[y]){
vis[y]=3-vis[x];//1->2 2->1
dfs(y);
}
else if(vis[y]!=3-vis[x]) ok=0;
}
}
bool chk(){
ok=1;
memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++)
if(!vis[i]) vis[i]=1,dfs(i);
return ok;
}
int gao(int bg){
static int q[M];
int h=0,t=1,x,p,y,i,mx=0;
for(i=0;i<pt.size();i++) d[pt[i]]=-1;
q[1]=bg;d[bg]=0;
while(h!=t){
x=q[++h];
mx=max(mx,d[x]);
for(p=e(x);p;p=e[p].nxt)
if(d[y=e[p].y]==-1){
d[y]=d[x]+1;
q[++t]=y;
}
}
return mx;
}
void getpt(int x){
vis[x]=1; pt.push_back(x);
for(int p=e(x);p;p=e[p].nxt) if(!vis[e[p].y]) getpt(e[p].y);
}
int chain(int x){
int i,mx=0; pt.clear();
getpt(x);
for(i=0;i<pt.size();i++) mx=max(mx,gao(pt[i]));
return mx;
}
void solve(){
ans=0;
memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++)
if(!vis[i]) ans+=chain(i);
printf("%d\n",ans);
}
int main(){
int i;
n=ri(),m=ri();
for(i=1;i<=m;i++) e.push2(ri(),ri());
if(chk()) solve();
else puts("-1");
return 0;
}
cf 542E - Playing on Graph的更多相关文章
- Codeforces 542E Playing on Graph 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/CF542E.html 题目传送门 - CF542E 题目传送门 - 51Nod1481 题意 有一幅无向图,它有 ...
- Codeforces.542E.Playing on Graph(二分图)
题目链接 \(Description\) 给出一个n个点m条边的无向图. 你每次需要选择两个没有边相连的点,将它们合并为一个新点,直到这张图变成了一条链. 最大化这条链的长度,或输出无解. n< ...
- 【Codeforces542E】Playing on Graph [Bfs][Dfs]
Playing on Graph Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 5 4 ...
- Codeforces542E Playing on Graph 思维+DFS+BFS
解法参考https://www.cnblogs.com/BearChild/p/7683114.html这位大佬的,这位大佬讲得很好了. 这道题还是有一定的思维的. 直接贴代码: #include&l ...
- Codeforces刷题计划
Codeforces刷题计划 已完成:-- / -- [Codeforces370E]370E - Summer Reading:构造:(给定某些数,在空白处填数,要求不下降,并且相邻差值<=1 ...
- CF Playing with Paper
Playing with Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))
C. Graph Reconstruction time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- CF 724 G. Xor-matic Number of the Graph
G. Xor-matic Number of the Graph 链接 题意: 给定一个无向图,一个interesting的三元环(u,v,s)满足,从u到v的路径上的异或和等于s,三元环的权值为s, ...
- cf 251 B Playing with Permutations 暴力 分类讨论
题链;http://codeforces.com/problemset/problem/251/B B. Playing with Permutations time limit per test 2 ...
随机推荐
- java链接MySQL数据库时使用com.mysql.jdbc.Connection的包会出红线问题 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connecti ...
- C++的新特性for-each
C++实验课要求用for each 循环来实现关联容器 map 的输出,一开始完全萌比.查了好久的资料才整理出下面的: C++11新特性之一就是类似java的for each循环: map<in ...
- Linux入门-第九周
1.判断UID是否大于等于500,如果为真就显示为普通用户,如果为假就显示为系统或管理用户 AWK简介:awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报 ...
- https://www.atlassian.com
https://www.atlassian.com 解决:confluence 5.9.4 一次性恢复30个插件 - 简书 https://www.jianshu.com/p/c32d8aa739b8 ...
- 微信小程序 onLoad 函数
小程序注册完成后,加载页面,触发onLoad方法. 页面载入后触发onShow方法,显示页面. 首次显示页面,会触发onReady方法,渲染页面元素和样式,一个页面只会调用一次. 当小程序后台运行或跳 ...
- Python学习笔记:math模块(数学),random模块(随机数)
math模块 math模块用于数学意义上的一些计算,常用的方法有: math.pi:PI的值(3.141592653589793). math.floor(x):返回一个小于等于x的最大整数(浮点类型 ...
- emwin如何在windows10下vs2015或2017进行仿真。
Make sure the selected Windows SDK is installed:Properties -> Configuration Properties -> Gene ...
- Terrorist’s destroy HDU - 4679
Terrorist’s destroy HDU - 4679 There is a city which is built like a tree.A terrorist wants to destr ...
- firewall-cmd 防火墙命令详解 及 TCP Wrappers
firewall-cmd 常用参数及作用 参数 作用 --get-default-zone 查询默认的区域名称 --set-default-zone=<区域名称> 设置默认的区域,使其永久 ...
- Spring---环境搭建与包介绍
jar包下载路径 首先需要下载Spring框架 spring-framework-5.0.0.RELEASE-dist,官方地址为https://repo.spring.io/release/org/ ...