POJ3177(无向图变双连通图)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11514 | Accepted: 4946 |
Description
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
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
题意:给定一个连通图,问至少加几条边可使这个图为双连通的。
思路:将原图G的双连通分量浓缩为一个点得到图G',则答案为(图G'中的叶子个数+1)/2。
存在重边
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=;
bool mp[MAXN][MAXN];//存在重边用邻接矩阵,用int的话会MLE
int n,m;
int dfn[MAXN],low[MAXN],time;
int stack[MAXN],top;
int ins[MAXN];
int belong[MAXN],cnt;
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++time;
stack[top++]=u;
ins[u]=true;
for(int v=;v<=n;v++)
{
if(mp[u][v])
{
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(v!=fa&&ins[v]) low[u]=min(low[u],dfn[v]);
}
} if(dfn[u]==low[u])//连通分量,发现一个处理一个
{
int v;
cnt++;
do{
v=stack[--top];
ins[v]=false;
belong[v]=cnt;
}while(u!=v);
}
}
int deg[MAXN];
void cal()
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(mp[i][j]&&belong[i]!=belong[j])
{
deg[belong[i]]++;
deg[belong[j]]++;
}
}
int res=;
for(int i=;i<=cnt;i++)
{
if(deg[i]==)
res++;
}
printf("%d\n",(res+)/);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(ins,false,sizeof(ins));
time=;
cnt=;
memset(belong,,sizeof(belong));
memset(mp,false,sizeof(mp));
memset(deg,,sizeof(deg));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=true;
}
tarjan(,-);
cal(); }
}
tarjan算法模板:
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=;
vector<int> mp[MAXN];
int n,m;
int dfn[MAXN],low[MAXN],index;
int bridge[MAXN];
int critical[MAXN];
int root;
int stack[MAXN],top;
bool ins[MAXN];
int belong[MAXN],cnt;
void tarjan(int u,int fa)
{
stack[u]=top++;
ins[u]=true;
dfn[u]=low[u]=++index;
int son=;
for(int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
son++;
if((u==root&&son>)||(u!=root&&dfn[u]<=low[v]))
{
critical[u]=;
}
if(dfn[u]<low[v])
{
bridge[v]=;
}
}
else if(v!=fa) low[u]=min(dfn[v],low[u]);
}
if(dfn[u]==low[u])
{
int v;
cnt++;
do{
v=stack[--top];
ins[v]=false;
belong[v]=cnt;
}while(u!=v);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u].push_back(v);
mp[v].push_back(u);
}
for(int i=;i<=n;i++)
if(!dfn[i])
{
root=i;
tarjan(i,-);
} printf("割点分别为:\n");
for(int i=;i<=n;i++)
{
if(critical[i])
printf("%d ",i);
}
printf("\n");
printf("割边一端的节点为:\n");
for(int i=;i<=n;i++)
{
if(bridge[i])
printf("%d ",i);
}
printf("\n");
printf("连通分量数目为:\n");
printf("%d\n",cnt);
return ;
}
/*
5 6
1 2
1 3
2 3
3 4
4 5
3 5
*/
最新文章
- NHibernate Profiler使用方法
- 第 5 章 基础 DOM 和 CSS 操作
- Python爬虫个人梳理(代码有空写)
- 从源码角度理清memcache缓存服务
- 烂泥:vcenter5.5无AD下的安装与配置
- iOS学习资源个人整理
- 编译Linux系统下的jrtplib3.9和jthread1.3(arm和ubuntu)
- 开发XMPP IM
- emacs配置详解及C/C++IDE全功能配置演示(附配置文件)
- (二十一)unity4.6学习Ugui中文文档-------交互-Supported Events &;amp; Raycasters
- [转载]PS各个工具的字母快捷键和英文全名
- 微信小程序之实现页面缩放式侧滑效果
- C语言作业06--结构体&;文件
- 在java中写出完美的单例模式
- java⑧
- 数据仓库专题20-案例篇:电商领域数据主题域模型设计v0.2(改进意见征集中)
- spring整合quartz时间任务调度框架
- Java-01-问题解答
- Flask 学习(二)路由
- java基础---->;Zip压缩的使用