博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 51F Caterpillar
阅读量:6114 次
发布时间:2019-06-21

本文共 3811 字,大约阅读时间需要 12 分钟。

time limit per test 2
 seconds
memory limit per test 
256 megabytes
input 
standard input
output 
standard output

An undirected graph is called a caterpillar if it is a connected graph without cycles and it has such a path p that any vertex is located at a distance of at most 1 from the path p. The caterpillar can contain loops (edges from a vertex to itself) but cannot contain multiple (parallel) edges.

The picture contains an example of a caterpillar:

You are given an undirected graph G. You are allowed to do a merging operations, each such operation merges two vertices into one vertex. For that two any vertices a and b (a ≠ b) are chosen. These verteces are deleted together with their edges (which are incident to at least one of the vertices a or b) but a new vertex w is added together with edges (x, w) for each edge (a, w) and/or (b, w). If there was the edge (a, b) it transforms to the loop (w, w). The resulting graph (after the merging operation) may contain multiple (parallel) edges between pairs of vertices and loops. Let us note that this operation decreases the number of vertices of graph by 1 but leaves the number of edges in the graph unchanged.

The merging operation can be informally described as a unity of two vertices of the graph into one with the natural transformation of the graph edges.

You may apply this operation consecutively and make the given graph to be a caterpillar. Write a program that will print the minimal number of merging operations required to make the given graph a caterpillar.

Input

The first line contains a pair of integers nm (1 ≤ n ≤ 2000;0 ≤ m ≤ 105), where n represents the number of vertices in the graph andm is the number of edges in it. Then the following m lines contain edge descriptions, one edge description per line. Every line contains a pair of integers ai, bi (1 ≤ ai, bi ≤ n;ai ≠ bi), ai, bi which represent the indices of the vertices connected by the edge. The vertices are numbered from 1 to n. In the given graph it will be no more than one edge between any pair of vertices. The given graph is not necessarily connected.

Output

Print the minimal required number of operations.

Examples
input
4 4 1 2 2 3 3 4 4 2
output
2
input
6 3 1 2 3 4 5 6
output
2
input
7 6 1 2 2 3 1 4 4 5 1 6 6 7
output
1

 

缩点

  目标状态是一棵树,可以有自环,不能有重边。tarjan缩点后,原图形成一片森林,对于每一棵树,它最多可以保留点数res=直径上的点数+其他叶子结点树。处理森林中的每一棵树,ans=total-res

_____

  刚开始没察觉到有森林,按照树处理,WA飞

  之后因为缩点后重边加多了,T飞

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 const int mx[5]={ 0,1,0,-1,0}; 9 const int my[5]={ 0,0,1,0,-1}; 10 const int mxn=2100; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(ch<'0' || ch>'9'){ if(ch=='-')f=-1;ch=getchar();} 14 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 struct edge{ 18 int v,nxt; 19 }e[200010]; 20 int hd[mxn],mct=0; 21 void add_edge(int u,int v){ 22 e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return; 23 } 24 int mp[mxn][mxn]; 25 int n,m; 26 int dtime=0; 27 int low[mxn],dfn[mxn]; 28 int belone[mxn],cnt; 29 int st[mxn],top=0; 30 void tarjan(int u,int fa){ 31 dfn[u]=low[u]=++dtime; 32 st[++top]=u; 33 for(int i=hd[u];i;i=e[i].nxt){ 34 int v=e[i].v; 35 if(v==fa)continue; 36 if(!dfn[v]){ 37 tarjan(v,u); 38 low[u]=min(low[u],low[v]); 39 } 40 else low[u]=min(low[u],dfn[v]); 41 } 42 if(dfn[u]==low[u]){ 43 cnt++; 44 int v=0; 45 do{ 46 v=st[top--]; 47 belone[v]=cnt; 48 }while(v!=u); 49 } 50 return; 51 } 52 vector
eg[mxn]; 53 int dis[mxn]; 54 bool vis[mxn];int kct=0; 55 int tg=0; 56 void DFS(int u,int fa){ 57 vis[u]=1; 58 dis[u]=dis[fa]+1; 59 if(dis[u]>dis[tg])tg=u; 60 for(int i=0;i

 

转载于:https://www.cnblogs.com/SilverNebula/p/6131084.html

你可能感兴趣的文章
dpkg:错误:正在解析文件 '/var/lib/dpkg/updates/0014' 第 0 行附近:在字段名 #padding 中有换行符问题的解决方法...
查看>>
【Linux】Ctentos下载
查看>>
vue中config/index.js:配置的详细理解
查看>>
springboot将项目源代码打包
查看>>
微信小程序之if操作
查看>>
【全网最全的博客美化系列教程】06.推荐和反对炫酷样式的实现
查看>>
Oracle 11g服务器安装详细步骤——图文教程(系统 windows server 2012 R2)
查看>>
SQL Server如何用SQL实现一批字符串的全部组合
查看>>
054 kafka内部机制
查看>>
Java反射机制
查看>>
php 7 新特性整理小结
查看>>
学会了这项技能,你就能获得任何想要的信息!
查看>>
IOS开发--解析复杂json数据
查看>>
linux之 修改磁盘调度算法
查看>>
tp5 数据库Db查询操作
查看>>
java web 中 filter 与 servlet的关系
查看>>
WPF 自定义IconButton
查看>>
MQTT压力测试之Tsung的使用
查看>>
【php】php输出jquery的轮询,5秒跳转指定url
查看>>
我终于开通了微信公众号
查看>>