poj——3177Redundant Paths
                    洛谷—— P2860 [USACO06JAN]冗余路径Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15272   Accepted: 6436

Description

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

Hint

Explanation of the sample:

One visualization of the paths is:

   1   2   3   +---+---+         |   |       |   | 6 +---+---+ 4      / 5     /     /  7 +

Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.

   1   2   3   +---+---+     :   |   |   :   |   | 6 +---+---+ 4      / 5  :     /     :    /      : 7 + - - - - 

Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7 
Every pair of fields is, in fact, connected by two routes.

It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

Source

 

描述
为了从一个F(1=f=5000)放牧场(编号为1…F)到另一个场,Bessie和其他牧群被迫越过腐烂的苹果树附近。奶牛现在已经厌倦了常常被迫走一条特定的道路,并且想建立一些新的道路,这样他们就可以在任何一对田地之间选择至少两条独立的路线。他们目前至少有一对路线之间的每一对领域,并希望有至少两个。当然,当他们从一个领域迁移到另一个领域时,他们只能在正式的道路上旅行。
描述了R的电流设定(F-1 <= R <= 10000),每个连接两个不同领域的路径,确定新的路径的最小数目(每个连接两个领域),必须建立,至少有两个独立的路线,任何对田野之间。如果路径没有相同的路径,即使它们在同一个中间区域访问相同的路径,它们也被认为是独立的。
在同一对字段之间可能有不止一条路径,您还可以构建一个新路径,将同一字段与其他路径连接起来。
输入
第1行:两个空间分隔的整数:f和r
第2行…r + 1:每行包含两个空间分隔的整数,这是某个路径端点的字段。
输出
第1行:一个整数,这是必须建立的新路径的数量。

思路:

我们来考虑一下这道题的做法。

由于题目说,给定一张无向强连通图:判断至少需要加多少条边,使得任意两点之间至少有两条相互‘边独立’的道路,也就是说,至少加多少条边,使得这个图成为一个边双连通图。

什么是双连通图?!

双连通图分为两种:双边连通图与双点连通图,双边连通图就是不存在割边的连通图,点双联通图就是不存在割点的连通图。。(差不多是这样吧。。)
首先我们已经有了一个连通图,我们先判断这个强连通图中是否存在环,这个就与tarjan缩点(有向图时有一点差别了)这个图保证是一个强连通图,如果我们还按以前那样缩点的话,我们只会把这个图个缩成一个点,这样肯定不对啊。。。

我们在缩完点以后把它变成了一个树,这样我们只需要把它的子节点连通起来就好了

也就是说:加入的边的条数一定等于:(叶子节点数+1)/2

我们在这个地方所说的叶子结点为入读或出度为0的点。

但在这里我又想到令一种算法:我们可以先求割边,割边的条数就是我们要求的叶子结点的个数

这样说有人可能会不明白了,那好,我们来看一个图

对于这个图来说,我们先将这个图中存在的环缩成一个点,那它就变成了右图的样子

           在这里,我们以环缩点后的点5为根节点     

看这个图的子节点是不是3?!

我们下面再来看看这个图的割边各大佬应该都能看出来我用红线割掉的边是这个图的割边吧。

他割边的个数是不是也是三?!  也就是说我们上面的结论成立。

那么这个题就变成一个很裸的求割边的板子了。。

好我们来看看代码吧。。

代码:(然而这个思路只能得63分,为什么?!)我们来考虑一下上面的思路的错误所在。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 5005
using namespace std;
bool vis[N][N];
,tim;
long long dfn[N],low[N];
],cut_edge[];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-';ch=getchar();}
    return x*f;
}
struct Edge
{
    int from,next,to;
}edge[];
void add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int tarjan(int now,int pre)
{
    ;
    dfn[now]=low[now]=++tim;
    for(int i=head[now];i;i=edge[i].next)
    {
        ^pre)) continue;
        int t=edge[i].to;
        if(!dfn[t])
        {
            tarjan(t,i);
            low[now]=min(low[now],low[t]);
            ]=;
        }
        else low[now]=min(low[now],dfn[t]);
    }
}
int main()
{
    n=read(),m=read();
    ;i<=m;i++)
    {
        x=read(),y=read();
        if(!vis[x][y]&&!vis[y][x]) add(x,y),add(y,x);
        vis[x][y]=vis[y][x]=true;
     }
    tarjan(,);
    ;i<=m;i++)
     )
      ans++;
    ///printf("%d\n",ans);
    printf()>>);
    ;
}

样例的图有些不是很明显,我们来看另一个图

对于这样一个图的话:我们缩点之后把它变成了这样一个图:

这个图割边的条数为:5条,如果按照我们刚刚的结论来看的话,我们要添3条边,然而我们只需要添2条边就可以了

为什么?!

看这个图:   我们只需要在7~9,1~9之间添一条边就可以了。

那么我们还是乖乖地统计入读为2的点的个数吧。。。。。。(其实这个地方应该是度数为一的点,但是这个地方由于我们建的是双向边,每一个点都会统计两次,所以这个地方最小的将会是入读为2)

这个地方肯定有人就会问这样一个问题:为什么这个地方建的是双向边我们还能用tarjan缩点啊?! 缩点的时候所得不是一个强连通分量吗??这样的一个绝对联通的无向图不就是直接把他缩成了一个点了吗?!这样怎么还能这样做?!

想一下这个地方我们在缩点的时候为什么可以用tarjan??

这样缩点主要是归功于这一句话:if(i==(1^pre)) continue; 对,他是用两条边,但是我们这个地方只让他走一条边,这样不就和有向图缩点一样了吗?!

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 5005
using namespace std;
bool vis[N];
,tim,sum,top;
long long du[N],dfn[N],low[N],stack[N],belong[N];
];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-';ch=getchar();}
    return x*f;
}
struct Edge
{
    int from,next,to;
}edge[];
void add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int tarjan(int now,int pre)
{
    dfn[now]=low[now]=++tim;
    stack[++top]=now;
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        ^pre)) continue;
        if(!dfn[t]) tarjan(t,i),low[now]=min(low[now],low[t]);
        else low[now]=min(low[now],dfn[t]);
    }
    if(low[now]==dfn[now])
    {
        sum++; belong[now]=sum;
        for(;stack[top]!=now;top--)
          belong[stack[top]]=sum;
        top--;
    }
}
int main()
{
    n=read(),m=read();
    ;i<=m;i++)
     x=read(),y=read(),add(x,y),add(y,x);
    tarjan(,);
    ;i<=n;i++)
     for(int j=head[i];j;j=edge[j].next)
      if(belong[i]!=belong[edge[j].to]) du[belong[i]]++,du[belong[edge[j].to]]++;
    ;i<=n;i++)
     ) ans++;
    printf()>>);
    ;
}


 

poj——3177Redundant Paths的更多相关文章

  1. POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 &amp;&amp; tarjan求ebc &amp;&amp; 缩点构造缩点树】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10798   Accepted: 4626 ...

  2. POJ 1942 Paths on a Grid(组合数)

    http://poj.org/problem?id=1942 题意 :在一个n*m的矩形上有n*m个网格,从左下角的网格划到右上角的网格,沿着边画,只能向上或向右走,问有多少条不重复的路 . 思路 : ...

  3. poj 1924 Paths on a Grid(组合数学)

    题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...

  4. [ACM] POJ 1942 Paths on a Grid (组合)

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21297   Accepted: 5212 ...

  5. POJ 1942 Paths on a Grid

    // n*m 的格子 从左下角走到右上角的种数// 相当于从 n+m 的步数中选 m 步往上走// C(n+m,m) #include <iostream> #include <st ...

  6. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  7. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  8. (poj 3177) Redundant Paths

    题目链接 :http://poj.org/problem?id=3177 Description In order to <= F <= ,) grazing fields (which ...

  9. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

随机推荐

  1. java语言基础-类型运算细节

    代码一: public class varDemo{ public static void main(String[] args) { byte a2; a2=3+4; System.out.prin ...

  2. Android天天数钱游戏项目源码

    Android天天数钱游戏源码,源码功能,天天数钱,这个游戏现在很多线上的小游戏都有这个了,游戏项目是在基于android游戏代码,大家可以参考一下. 源码下载:http://code.662p.co ...

  3. Windows Server 启用匿名共享

    1.开始 → 运行 → gpedit.msc,打开组策略编辑器: 2.依次展开"计算机配置" → "windows设置" → "安全设置"  ...

  4. XtraBackUp 热备份工具

    是一款强大的在线热备份工具 备份的过程中,不锁表 使用percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm yum源安装: 1.安装Percona的库:       ...

  5. Python3简明教程(十四)—— Collections模块

    collections 是 Python 内建的一个集合模块,提供了许多有用的集合类. 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它们能帮助你解决各种实际问 ...

  6. vscode setting.json

    setting.json { "sync.gist": "#github的码##", "sync.lastUpload": "20 ...

  7. js 根据指定个数切割数组

    Part.1 问题 写项目时,遇到需要前端做 假分页 的问题:后端会将数据全部返回,前端自己做分页 Part.2 思路 拿到后端全部返回的数据后,按照 产品需求  进行分页,如每页显示 10 条数据为 ...

  8. transformer模型解读

    最近在关注谷歌发布关于BERT模型,它是以Transformer的双向编码器表示.顺便回顾了<Attention is all you need>这篇文章主要讲解Transformer编码 ...

  9. tomcat官网改版后下载方式

    位于disk/tomcat目录下 http://archive.apache.org/dist/tomcat/ 具体例如:http://archive.apache.org/dist/tomcat/t ...

  10. 【转】错误: ORA-01591: 锁被未决分布式事务处理 7.2.428982 持有--解决方案

    SQL 错误: ORA-01591: 锁被未决分布式事务处理 7.2.428982 持有 01591. 00000 -  "lock held by in-doubt distributed ...