POJ3177,/3352.求最少添加多少边使无向图边双连通
俩个题一样。tarjan算法应用,开始求桥,WA,同一个边双连通分量中low值未必都相同,不能用此来缩点。后来用并查集来判断,若不是桥,则在一个双连通分量中,并之,后边再查,将同一个双连通分量中的点通过并查集收缩为一个并查集的“祖宗点”,间接完成缩点!
缩点成树后,(leaves+1)/2就不用说了。。。。
#include<iostream> //0MS
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
/*struct gebian //开始时记录割边,然后用同一个连通分量中LOW值相等来判断,错误。
{
int u,v;
};*/
vector<vector<int> >edge(5001); //相当于链表,这个真心不错。
//vector<gebian>geb;
int dfn[5001];
int low[5001];
int visited[5001]; //标记访问
int time=0; //时间戳
int degree[5001];
int father[5001];
int hash[5001][5001];
int min(int a,int b)
{
if(a<=b)return a;
return b;
}
int find(int x){return x==father[x]?x:find(father[x]);}
void tarjan(int u,int fa) //dfs
{
dfn[u]=low[u]=++time;
int daxiao=edge[u].size();
for(int i=0;i<daxiao;i++)
{
int child=edge[u][i];
if(visited[child]==0)
{
visited[edge[u][i]]=1;
tarjan(edge[u][i],u);
low[u]=min(low[u],low[edge[u][i]]);
if(dfn[u]<low[edge[u][i]]) //是桥
{
/* gebian temp;
temp.u=u;
temp.v=edge[u][i];
geb.push_back(temp);*/
;
}
else //不是桥,必在同一边连通分量中
{
father[child]= u; //并之
}
}
else if(edge[u][i]!=fa)
{
low[u]=min(dfn[edge[u][i]],low[u]);
}
}
}
int solve(int n)
{
int leaves=0;
//int daxiao1=geb.size();
// cout<<daxiao1<<endl;
/* for(int i=0;i<daxiao1;i++)
{
// cout<<low[geb[i].v]<<endl;
// cout<<low[geb[i].u]<<endl;
degree[low[geb[i].v]]++;
degree[low[geb[i].u]]++;
}
for(int i=1;i<=n;i++)
{
cout<<low[i]<<" ";
degree[low[i]]++;
}*/
for(int i=1;i<=n;i++) //枚举边
{
int len=edge[i].size();
for(int j=0;j<len;j++)
{
if( hash[i][edge[i][j]]||hash[edge[i][j]][i])continue; //hash判重
int xx=find(i);int yy=find(edge[i][j]);
hash[i][edge[i][j]]=1;hash[edge[i][j]][i]=1;
if(xx!=yy)
{
degree[xx]++;
degree[yy]++;
}
}
}
for(int i=1;i<=n;i++)
{ if(degree[i]==1) //叶子
{
leaves++;
}
}
return (leaves+1)/2;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int a,b;
for(int i=0;i<=n;i++)
father[i]=i;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
edge[a].push_back(b);
edge[b].push_back(a);
}
visited[1]=1;
tarjan(1,-1);
cout<<solve(n)<<endl;
return 0;
}
POJ3177,/3352.求最少添加多少边使无向图边双连通的更多相关文章
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- poj 3177 Redundant Paths 求最少添加几条边成为双联通图: tarjan O(E)
/** problem: http://poj.org/problem?id=3177 tarjan blog: https://blog.csdn.net/reverie_mjp/article/d ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48
921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...
- 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。
题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...
- HDU 1326 Box of Bricks(水~平均高度求最少移动砖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到 ...
- 输入一个数字n 如果n为偶数则除以2,若为奇数则加1或者减1,直到n为1,求最少次数 写出一个函数
题目: 输入一个数字n 如果n为偶数则除以2,若为奇数则加1或者减1,直到n为1,求最少次数 写出一个函数 首先,这道题肯定可以用动态规划来解, n为整数时,n的解为 n/2 的解加1 n为奇数时 ...
随机推荐
- Firefox离线安装扩展教程
Firefox离线安装扩展教程 解决问题博文:解决stackoverflow打开慢不能注册登录 应网友求助在上传了需要的扩展资源后,顺便写个离线安装方法,其实百度也行,这不写下来后为需求者省事.(*^ ...
- PHP获取时间总结
查询前一年时间戳 mktime(0,0,0,date('m'),date('d'),date('Y')-1); strtotime('-12 month'); 查询前6个月时间戳 mktime(0,0 ...
- Java格式规范及注释的用法
/* 需求:演示一个Hello World的Java小程序 思路: 1.定义一个类.因为Java程序都是定义在类中,Java程序都是以类的形式存在的,类的形式其实就是字节码的最终体现 2.定义一个主函 ...
- COGS 2685. 迷妹
★ 输入文件:fans.in 输出文件:fans.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 小钟.小皓和小曦都是著名偶像派OI选手,他们都有很多迷妹. 现 ...
- SQLite-删除查询
SQLite -删除查询 SQLite DELETE查询用于从一个表删除现有记录.您可以使用WHERE子句删除查询删除选定行,否则所有记录将被删除. 语法: 删除查询的WHERE子句的基本语法如下: ...
- 吸顶条 ---- jQ写法
<script> $(function () { var barTop = $('#bar').offset().top; //on方法相当于原生的绑定 $(window).on('scr ...
- uva820 Internet Bandwidth
就是模板... #include<cstdio> #include<cstring> #include<vector> #include<queue> ...
- uva12265 Selling Land
见紫书.(c,h)的更新策略://前面的高度为0了,直接插入因为ans==-c+h,c大,h还小,那么肯定不是最优左上角,更新新加入列的列//新的一列高度最小,就删掉了其他的,只留这个高度从上到下,从 ...
- vue 数组对接字符串 传值时候,join(',') 一下 watch
vue 数组对接字符串 传值时候,join(',') 一下 watch watch: { 'tFill.otherDescArr': function (newVal, oldVal) { this. ...
- ProxyFactory
Spring定义了org.springframework.aop.framework.AopProxy接口,并提供了两个final类型的实现类. AopProxy类结构: