第一道双联通的题目,求加几条边让原图成一个双联通图,求出度数为1的双联通分量的个数+1/2、

Low[u]为u或u的子树中能通过非父子边追溯到的最早的节点,即DFS序号最小的节点的序号

#include<stdio.h>
#include<stack>
#include<string.h>
#define N 5010
using namespace std;
int n,m,first[N],num,low[N],dfs[N],idx,ans,vis[N],degree[N],belong[N];
struct edge
{
int ed,next;
}E[10010];
void addedge(int x,int y)
{
E[num].ed=y;
E[num].next=first[x];
first[x]=num++;
}
stack<int>Q;
void Tarjan(int u,int father)
{ low[u]=dfs[u]=idx++;
vis[u]=1;
Q.push(u);
for(int p=first[u];p!=-1;p=E[p].next)
{
int v=E[p].ed;
if(v==father){continue;}
if(!vis[v])Tarjan(v,u);
low[u]=low[u]>low[v]?low[v]:low[u];
}
int x;
if(low[u]==dfs[u])
{
do
{
x=Q.top();
Q.pop();
vis[x]=0;
belong[x]=ans;//缩点
}while(x!=u);
ans++;
}
}
int judge(int x,int y)
{
for(int p=first[x];p!=-1;p=E[p].next)
if(E[p].ed==y)
return 0;
return 1;
}
int main()
{
int i,x,y,sum;
while(scanf("%d%d",&n,&m)!=-1)
{
memset(first,-1,sizeof(first));
num=0;
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
if(judge(x,y)==1)//去掉重边
{
addedge(x,y);
addedge(y,x);
}
}
memset(vis,0,sizeof(vis));
idx=ans=0;
Tarjan(1,0);
memset(degree,0,sizeof(degree));
for(i=1;i<=n;i++)
{
for(int p=first[i];p!=-1;p=E[p].next)
{
int v=E[p].ed;
if(belong[i]!=belong[v])
degree[belong[v]]++;
}
}
sum=0;
for(i=0;i<ans;i++)
if(degree[i]==1)//度为1的点
sum++;
//printf("%d\n",sum);
printf("%d\n",(sum+1)/2);
}
return 0;
}

poj 3177的更多相关文章

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

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

  2. poj 3177 Redundant Paths

    题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...

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

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

  4. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

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

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

  6. POJ 3177——Redundant Paths——————【加边形成边双连通图】

    Redundant Paths Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  7. POJ - 3177 Redundant Paths(边双连通分支)(模板)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...

  8. POJ 3177 Redundant Paths(强连通分量)

    题目链接:http://poj.org/problem?id=3177 题目大意是一个无向图给你n个点m条边,让你求出最少加多少条边 可以让任意两个点相通两条及以上的路线(每条路线点可以重复,但是每条 ...

  9. (poj 3177) Redundant Paths

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

  10. poj 3177 Redundant Paths(边双连通分量+缩点)

    链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...

随机推荐

  1. VMware 虚拟机使用RedHat,出现 connect: Network is unreachable解決方法

    http://www.linuxidc.com/Linux/2015-02/113119.htm http://www.osyunwei.com/archives/7829.html

  2. Django-Rest-Framework 教程: 快速入门

    本篇中, 我们会创建一个简单的API, 用来查看和编辑django默认的user和group数据. 1. 设置 我们创建django项目tutorial, 和app quickstart: # 创建新 ...

  3. Weekend counter

    Weekend counter Sofia has given you a schedule and two dates and told you she needs help planning he ...

  4. linux操作系下RAR的使用

    ============zip文件的操作================================== zip -r data.zip data 解释:将data文件夹压缩成了data.zip格 ...

  5. MySQL存储过程事务处理

    BEGIN ; ; START TRANSACTION; #这边放sql语句,涉及到的表必须都为InnoDB THEN ROLLBACK; ELSE COMMIT; END IF; END 转自:ht ...

  6. Tomcat部署项目通过—IP地址:端口访问

    如题所示,实现效果图如下: 设置如下: (1)修改${tomcat}/config/sever.xml文件虚拟内容目录: <Engine name="Catalina" de ...

  7. Java中ThreadLocal无锁化线程封闭实现原理

    虽然现在可以说很多程序员会用ThreadLocal,但是我相信大多数程序员还不知道ThreadLocal,而使用ThreadLocal的程序员大多只是知道其然而不知其所以然,因此,使用ThreadLo ...

  8. DBA 经典面试题(1)

    1:列举几种表连接方式 hash join.  merge join. nest loop join(cluster join). index join    2:不借助第三方工具,怎样查看sql的执 ...

  9. linux下面测试网络带宽 (转载)

    利用bmon/nload/iftop/vnstat/iptraf实时查看网络带宽状况 一.添加yum源方便安装bmon# rpm -Uhv http://apt.sw.be/redhat/el5/en ...

  10. 【转】使用vnc连接linux服务器方便hadoop开发调试

    VNC(Virtual Network Computing)它能将完整的窗口界面通过网络,传输到另一台计算机的屏幕上. 类似的软件在Windows服务器中包含的"Terminal Serve ...