A8035 | Caterpillar
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line contains a pair of integers $n$ , $m$ ( $1<=n<=2000;0<=m<=10^{5}$ ), where $n$ represents the number of vertices in the graph and $m$ 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 $a_{i},b_{i}$ ( $1<=a_{i},b_{i}<=n;a_{i}≠b_{i}$ ), $a_{i},b_{i}$ 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.
输出格式
Print the minimal required number of operations.
输入输出样例
输入 #1
4 4 1 2 2 3 3 4 4 2
输出 #1
2
输入 #2
6 3 1 2 3 4 5 6
输出 #2
2
输入 #3
7 6 1 2 2 3 1 4 4 5 1 6 6 7
输出 #3
1
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted