#POJ 3177 Redundant Paths (无向图缩点)

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

题目大意 : 输入一张图,求至少加几条边才能让任意两条边都至少有两条路可达

思路 : 这其实就是一个无向图的强连通问题, 先缩点,然后对新点的度数进行更新,遍历一下,度数为2的就加上,因为要想强连通一定是一个叶子连接另一个叶子或者一个叶子连着一个点 ,最后得出的数目  就是(度数为2的数目 + 1) /  2,这个不难想,但是要注意这道题有重边,比如输入的数据为2 1 , 1 2 , 2 1,正确答案应该是1,但是如果你不去重的话答案就会是0, 所以在输入的时候维护一下就OK了。

AC代码 : 

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 1e5 + 5;
const int maxm = 5e3 + 5;

struct node
{
    int v, next;
}e[maxn];
int dfn[maxn], low[maxn], suo[maxn], head[maxn], dep[maxn];
int n, m, cnt, tot, scnt;
int vis[maxm][maxm];
stack <int> st;
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()
{
    cin >> n >> m;
    memset(head, -1, sizeof(head));
    for (int i = 0; i < m; i++) {
        int ui, vi;
        cin >> ui >> vi;
        if (vis[ui][vi]) continue;
        vis[ui][vi] = vis[vi][ui] = 1;   //去重
        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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值