Description
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
Sample Input
Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10
Sample Input 2
3 3
1 2
2 3
1 3
Sample Output
Output for Sample Input 1
2
Output for Sample Input 2
0
题目大意 : 输入一张无向图, 没有重边,问最少需要添加几条边,可以使任意两条边之间至少有两条路可走
思路 : 无向图缩点的模板题, 直接缩点然后更新每个新点的度数, 可以更新两个点也可以只更新一个点,唯一坑的是他题目上给的Input和Output都是摆设, 一直在想着输入格式的问题后来删了就A了,真神奇。。。
AC代码 :
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 1e4 + 5;
struct node
{
int v, next;
}e[maxn];
int dfn[maxn], low[maxn], suo[maxn], dep[maxn];
int head[maxn], n, m, cnt, tot, scnt, X;
stack <int> st;
void init() {
memset(e, 0, sizeof(e));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(suo, 0, sizeof(suo));
memset(dep, 0, sizeof(dep));
memset(head, -1, sizeof(head));
cnt = tot = scnt = 0;
}
void add (int from, int to) {
e[++cnt].v = to;
e[cnt].next = head[from];
head[from] = cnt;
}
void tarjan(int x, int fa) {
dfn[x] = low[x] = ++tot;
st.push(x);
for (int i = head[x]; i != -1; i = e[i].next) {
int v = e[i].v;
if (!dfn[v]) {
tarjan(v, x);
low[x] = min (low[x], low[v]);
}
else if (v != fa) low[x] = min (low[x], dfn[v]);
}
if (dfn[x] == low[x]) {
scnt++;
int k;
do {
k = st.top();
st.pop();
suo[k] = scnt;
}
while (k != x);
}
}
int main()
{
while (cin >> n >> m) {
init();
for (int i = 0; i < m; i++) {
int ui, vi;
cin >> ui >> vi;
add (ui, vi);
add (vi, ui);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i, i);
}
for (int i = 1; i <= n; i++) {
for (int j = head[i]; j != -1; j = e[j].next) {
int u = suo[i], v = suo[e[j].v];
if (u != v) dep[u]++, dep[v]++;
}
}
int ans = 0;
for (int i = 1; i <= scnt; i++) {
if (dep[i] == 2) ans++;
}
cout << (ans + 1) / 2 << endl;
}
return 0;
}