hdu3749 Financial Crisis(点-双连通分量)

题意:给你一个(保证输入无重边,无自环)无向图,然后有下面Q条询问,每条询问为:问你u点与v点之间有几条(除了首尾两点外,其他点不重复)的路径.如果有0条或1条输出0或1,如果有2条以上,输出”two or more”.

思路首先如果u点与v点不连通,直接输出0即可.

           然后如果u点与v点属于同一个点-双连通分量,输出two or more.(这里有特例,两点一边的点-双连通分量应该输出1)

           剩下的所有情况表示u与v虽然连通,但是在不同的点-双连通分量类,直接输出1即可.


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include<stack>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 5005
#define LL long long
int cas=1,T;
int pre[maxn];
int iscut[maxn];
int father[maxn];
int bccno[maxn];
int dfs_clock;
int bcc_cnt;
vector<int>G[maxn],bcc[maxn];
vector<int>belong[maxn];   //belong[i]表示第i个结点所属于的所有点双连通分量编号
struct Edge
{
	int u,v;
	Edge(int u,int v):u(u),v(v){}
};
int n,m,q;
int low[maxn];
stack<Edge>S;
/*
int dfs(int u,int fa)
{
    int lowu = pre[u] = ++dfs_clock;
	int child = 0;
	for (int i = 0;i<G[u].size();i++)
	{
		int v = G[u][i];
		Edge e = (Edge){u,v};
		if (!pre[v])
		{
			S.push(e);
			child++;
			int lowv = dfs(v,u);
			lowu = min(lowu,lowv);
			if (lowv >= pre[u])
			{
				iscut[u]=true;
				bcc_cnt++;
				bcc[bcc_cnt].clear();
				for (;;)
				{
					Edge x = S.top();S.pop();
					if (bccno[x.u] != bcc_cnt)
					{
						bcc[bcc_cnt].push_back(x.u);
						bccno[x.u]=bcc_cnt;
						belong[x.u].push_back(bcc_cnt);
					}
					if (bccno[x.v] != bcc_cnt)
					{
						bcc[bcc_cnt].push_back(x.v);
						bccno[x.v]=bcc_cnt;
						belong[x.v].push_back(bcc_cnt);
					}
					if (x.u == u && x.v==v)
						break;
				}
			}
		}
        else if (pre[v] < pre[u] && v!=fa)
		{
			S.push(e);
			lowu = min(lowu,pre[v]);
		}
	}
	if (fa < 0 && child == 1)
		iscut[u]=0;
	return lowu;
}*/
void dfs(int u,int fa)
{
    low[u]=pre[u]=++dfs_clock;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(v==fa) continue;
        Edge e=Edge(u,v);
        if(!pre[v])
        {
            S.push(e);
            dfs(v,u);
            low[u]=min(low[v],low[u]);
            if(low[v]>=pre[u])
            {
                bcc_cnt++; bcc[bcc_cnt].clear();
                while(true)
                {
                    Edge x=S.top(); S.pop();
                    if(bccno[x.u]!=bcc_cnt)
                    {
                        bcc[bcc_cnt].push_back(x.u), bccno[x.u]=bcc_cnt;
                        belong[x.u].push_back(bcc_cnt);
                    }
                    if(bccno[x.v]!=bcc_cnt)
                    {
                        bcc[bcc_cnt].push_back(x.v), bccno[x.v]=bcc_cnt;
                        belong[x.v].push_back(bcc_cnt);
                    }
                    if(x.u==u && x.v==v) break;
                }
            }
        }
        else if(pre[v]<pre[u])
        {
            S.push(e);
            low[u]=min(low[u],pre[v]);
        }
    }
}
int Find(int x)
{
	return father[x]==-1?x:father[x]=Find(father[x]);
}
void init()
{
	bcc_cnt=dfs_clock=0;
	memset(pre,0,sizeof(pre));
	memset(bccno,0,sizeof(bccno));
	memset(father,-1,sizeof(father));
//	memset(low,0,sizeof(low));
	for (int i = 0;i<=n;i++)
		G[i].clear();
	for (int i = 0;i<=n;i++)
		belong[i].clear();
}
void find_bcc()
{
//	memset(pre,0,sizeof(pre));
//	memset(iscut,0,sizeof(iscut));
//	memset(bccno,0,sizeof(bccno));
//	dfs_clock = bcc_cnt = 0;
	for (int i = 0;i<n;i++)
		if (!pre[i])
			dfs(i,-1);
}
int main()
{

	while (scanf("%d%d%d",&n,&m,&q)==3 && n)
	{
		init();
        for (int i = 0;i<m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			G[u].push_back(v);
			G[v].push_back(u);
			u=Find(u);
			v=Find(v);
			if (u!=v)
				father[u]=v;
		}
		//find_bcc();
		for (int i = 0;i<n;i++)
			if (!pre[i])
				dfs(i,-1);
		printf("Case %d:\n",cas++);
		while (q--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			if (Find(u)!=Find(v))
				printf("zero\n");
			else
			{
				bool flag = false;
				for (int i = 0;i<belong[u].size()&&!flag;i++)
					for (int j = 0;j<belong[v].size()&&!flag;j++)
					{
						if (belong[u][i] ==belong[v][j])
						{
							if (bcc[belong[u][i]].size() > 2)
								printf("two or more\n"),flag=true;
						}
					}
				if (!flag)
					printf("one\n");
			}
		}
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

Description

Because of the financial crisis, a large number of enterprises go bankrupt. In addition to this, other enterprises, which have trade relation with the bankrup enterprises, are also faced with closing down. Owing to the market collapse, profit decline and funding chain intense, the debt-ridden entrepreneurs 
have to turn to the enterprise with stably developing for help. 

Nowadays, there exist a complex net of financial trade relationship between enterprises. So if one of enterprises on the same financial chain is faced with bankrupt, leading to cashflow's obstruction, the other enterprises related with it will be influenced as well. At the moment, the foresight entrepreneurs are expected the safer cooperation between enterprises. In this sense, they want to know how many financial chains between some pairs of enterprises are independent with each other. The indepence is defined that if there exist two roads which are made up of enterprises(Vertexs) and their financial trade relations(Edge) has the common start point S and end point T, and expect S and T, none of other enterprise in two chains is included in these two roads at the same time. So that if one of enterpirse bankrupt in one of road, the other will not be obstructed. 

Now there are N enterprises, and have M pair of financial trade relations between them, the relations are Mutual. They need to ask about Q pairs of enterprises' safety situations. When two enterprises have two or more independent financial chains, we say they are safe enough, you needn't provide exact answers.
 

Input

The Input consists of multiple test cases. The first line of each test case contains three integers, N ( 3 <= N <= 5000 ), M ( 0 <= M <= 10000 ) and Q ( 1 <= Q <= 1000 ). which are the number of enterprises, the number of the financial trade relations and the number of queries. 
The next M lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means enterpirse u and enterprise v have trade relations, you can assume that the input will not has parallel edge. 
The next Q lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means entrepreneurs will ask you the financial safety of enterpirse u and enterprise v. 
The last test case is followed by three zeros on a single line, which means the end of the input.
 

Output

For each case, output the test case number formated as sample output. Then for each query, output "zero" if there is no independent financial chains between those two enterprises, output "one" if there is only one such chains, or output "two or more".
 

Sample Input

       
       
3 1 2 0 1 0 2 1 0 4 4 2 0 1 0 2 1 2 2 3 1 2 1 3 0 0 0
 

Sample Output

       
       
Case 1: zero one Case 2: two or more one
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值