HDU394-点双连通模板题

博客围绕公园铁路建设问题展开,给出无向图的点和边信息,要求计算无需建设的铁路数量和可能发生冲突的铁路数量。通过点 - 双连通模板题的思路进行求解,并给出了输入输出示例及AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Railway
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4154 Accepted Submission(s): 1366

Problem Description
There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build.
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.

Input
The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v.
You can assume that there is no loop and no multiple edges.
The last test case is followed by two zeros on a single line, which means the end of the input.

Output
Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.

Sample Input

8 10
0 1
1 2
2 3
3 0
3 4
4 5
5 6
6 7
7 4
5 7
0 0

Sample Output

1 5

题意:简单点就是,给出一个无向图n个点和m条边,第一个是桥的数量,第二个是一个双连通分量中边数>点数的边的数量。
样例得出 桥的数量是1,双连通分量中{4,5,6,7}这些点构成的集合的边数>点数,所以输出边数
思路:点-双连通模板题,直接算就行了。。。
AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int maxn=10000+10;
struct Node
{
	int start,end;
	Node(int start,int end):start(start),end(end){}
};
vector<int> map[maxn];
int pre[maxn],dfn[maxn],bccno[maxn];
int n,m,top,index,cnt;
int ans1,ans2;
stack<Node> sta;
void Init()
{
	for (int i=0;i<n;i++)
		map[i].clear();
	memset(pre,0,sizeof(pre));
	memset(dfn,0,sizeof(dfn));
	memset(bccno,-1,sizeof(bccno));
	return ;
}
int tarjan(int now,int last)
{
	int num1,num2,child;
	dfn[now]=pre[now]=++index;
	for (int i=0;i<map[now].size();i++)
		{
	//		cout<<now<<"+++++++++++"<<map[now][i]<<endl;
			int next=map[now][i];
			if (next==last) continue;
			Node road=Node(now,next);
			if (pre[next]==0)
				{
					sta.push(road);
					dfn[next]=tarjan(next,now);
					dfn[now]=min(dfn[now],dfn[next]);
					if (dfn[next]>pre[now]) ans1++;		//第一问
					if (dfn[next]>=pre[now])
						{
							cnt++;
							num1=num2=0;
							while (true)
								{
									num1++;		//num1记录边的数量
									Node x=sta.top();
									sta.pop();
							//		cout<<x.start<<"====="<<x.end<<endl;
									if (bccno[x.start]!=cnt)
										{
											num2++;			//num2记录点的数量
											bccno[x.start]=cnt;
										}
									if (bccno[x.end]!=cnt)
										{
											num2++;
											bccno[x.end]=cnt;
										}
									if (x.start==now&&x.end==next) break;
								}
							if (num1>num2) ans2+=num1;		//第二问的答案
					//		cout<<endl;
						}
				}
			else if (pre[next]<pre[now])
					{
						sta.push(road);
						dfn[now]=min(dfn[now],pre[next]);
					}
		}
	return dfn[now];
}
void solve()
{
	top=index=cnt=0;
	ans1=ans2=0;
	for (int i=0;i<n;i++)
		if (dfn[i]==0) tarjan(i,-1);
	return ;
}
int main()
{
	int start,end;
	while (scanf("%d%d",&n,&m)!=EOF&&(n+m))
		{
			Init();
			for (int i=1;i<=m;i++)
				{
					scanf("%d%d",&start,&end);
					map[start].push_back(end);
					map[end].push_back(start);
				}
			solve();
	//		cout<<"cnt=="<<cnt<<endl;
			cout<<ans1<<" "<<ans2<<endl;
		}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值