HDU394-点双连通模板题

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;
}
发布了46 篇原创文章 · 获赞 2 · 访问量 3200

猜你喜欢

转载自blog.csdn.net/z1164754004z/article/details/89930178