HDU 3749 Financial Crisis(点-双连通分量)
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.
InputThe 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.OutputFor 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
题意:
给你一个(保证输入无重边,无自环)无向图,然后有下面Q条询问,每条询问为:问你u点与v点之间有几条(除了首尾两点外,其他点不重复)的路径.如果有0条或1条输出0或1,如果有2条以上,输出”two or more”.
分析:
首先如果u点与v点不连通,直接输出0即可.(用并查集实现)
然后如果u点与v点属于同一个点-双连通分量,输出two or more.(这里有特例,两点一边的点-双连通分量应该输出1)
自己写的代码已知wrong不知道为什么,哪位大佬看一看谢谢。。
#include<cstring>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std; int n,m,q,TAK,col,tim,top,tot;
int a[],color[],dfn[],low[],stack[],ins[];
int cnt,head[],Next[],rea[];
struct Node
{
int x,y;
}b[];
map<int,int>p; void add(int u,int v)
{
Next[++cnt]=head[u];
head[u]=cnt;
rea[cnt]=v;
}
void Tarjan(int u,int fa)
{
int num=;color[u]=col;
dfn[u]=low[u]=++tim;
stack[++top]=u,ins[u]=;
for (int i=head[u];i!=-;i=Next[i])
{
int v=rea[i];
if (v==fa) continue;
if (!dfn[v])
{
Tarjan(v,u);num++;
low[u]=min(low[u],low[v]);
if (low[v]>=dfn[u])
{
tot=;
int x=-;
while(x!=u)
{
x=stack[top--];
ins[x]=;
a[++tot]=x;
}
stack[++top]=u,ins[u]=;
if (tot==) continue;
for (int j=;j<tot;j++)
for (int k=j+;k<=tot;k++)
{
int x=a[j],y=a[k];
if (x>y) swap(x,y);
if (p[x*n+y]==-) p[x*n+y]=;
}
}
}
else low[u]=min(low[u],dfn[v]);
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&q)&&(n+m+q))
{
printf("Case %d:\n",++TAK);
cnt=top=tim=;p.clear();
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(color,,sizeof(color));
memset(head,-,sizeof(head));
for (int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
x++,y++;
add(x,y),add(y,x);
}
for (int i=;i<=q;i++)
{
scanf("%d%d",&b[i].x,&b[i].y);
b[i].x++,b[i].y++;
if (b[i].x>b[i].y) swap(b[i].x,b[i].y);
p[b[i].x*n+b[i].y]=-;
}
for (int i=;i<=n;i++)
if (!dfn[i])
{
col=i;
Tarjan(i,-);
}
for (int i=;i<=q;i++)
if (color[b[i].x]!=color[b[i].y]) printf("zero\n");
else if (p[b[i].x*n+b[i].y]==-) printf("one\n");
else printf("two or more\n");
}
}
HDU 3749 Financial Crisis(点-双连通分量)的更多相关文章
- HDU 3749 Financial Crisis (点双连通+并查集)
<题目连接> 题目大意: 给你一个(保证输入无重边,无自环)无向图,然后有下面Q条询问,每条询问为:问你u点与v点之间有几条(除了首尾两点外,其他点不重复)的路径.如果有0条或1条输出0或 ...
- HDU 3749 Financial Crisis 经济危机(点双连通分量)
题意: 给一个图n个点m条边(不一定连通),接下来又q个询问,询问两个点是为“不相连”,“仅有一条路径可达”,“有两条及以上的不同路径可达”三种情况中的哪一种.注:两条以上的路径指的是路径上的点连1个 ...
- HDU 3749 Financial Crisis
Financial Crisis 题意:给一个图,包含N ( 3 <= N <= 5000 )个点, M ( 0 <= M <= 10000 )条边 and Q ( 1 < ...
- hdu 2460(tarjan求边双连通分量+LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2460 思路:题目的意思是要求在原图中加边后桥的数量,首先我们可以通过Tarjan求边双连通分量,对于边 ...
- HDU 4612 Warm up (边双连通分量+DP最长链)
[题意]给定一个无向图,问在允许加一条边的情况下,最少的桥的个数 [思路]对图做一遍Tarjan找出桥,把双连通分量缩成一个点,这样原图就成了一棵树,树的每条边都是桥.然后在树中求最长链,这样在两端点 ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥 ...
- HDU 4612 Warm up(双连通分量缩点+求树的直径)
思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
随机推荐
- spring入门笔记-(一)、spring boot HelloWorld
什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- C#DataTable学习心得[转]
一.DataSet.DataTable.DataRow.DataColumn 1] 在DataSet中添加DataTable DataSet.Tables.Add(DataTable) 实例: Dat ...
- Java8-Lomda表达式
Lomda表达式 /** * All rights Reserved, Designed By www.bingo.com * @Title TestLamda.java * @author yang ...
- 学JAVA第二十二天,StringBuffer的好处
五一的假期今天就结束了,又要回来上课了. 今天就写一下StringBuffer的好处吧. StringBuffer类的对象能够被多次的修改,并且不产生新的未使用对象. 也就是说,我们平时用String ...
- java urlEncode 和urlDecode的用法
前台进行http请求的时候 如果要对中问进行编码,要使用两次编码 String zhName=urlEncode.encode((urlEncode.encode("中文",&qu ...
- Python学习 Day 4 函数 切片 迭代 列表生成式 生成器
定义函数 def my_abs(x):#求绝对值的my_abs函数 if x >= 0: return x else: return –x def nop():#空函数 pass#占位符 参数检 ...
- 程序员段子:世界上最大的同性交友平台github
程序员(又名程序猿)因为总是冲锋在网络的最前端,还有程序猿的各种特殊性,大家在茶余饭后都有很多关于程序员的段子流传.大多都是程序员自黑的,先说在前面,程序猿还是很好的!下面看看你有没有中枪的那一条呢? ...
- KMP中next数组的理解与应用
理解 1.next数组一直往前走 next数组一直往前走,得到的所有前缀也是当前主串的后缀,当然了,也是当前主串的前缀. 2.周期性字符串 1.周期性字符串$\Leftrightarrow n \,\ ...
- 获取minist数据并转换成lmdb
caffe本身是没有数据集的,但在data目录下有获取数据的一些脚本.MNIST,一个经典的手写数字库,包含60000个训练样本和10000个测试样本,每个样本为28*28大小的黑白图片,手写数字为0 ...
- Android之多种Bitmap效果(4)
1. 将图片变为圆角 2. 获取缩略图图片 3. LOMO特效 4. 旧时光特效 5. 暖意特效 6. 根据饱和度.色相.亮度调整图片 7. 添加图片外边框 8. 添加内边框 9. 创建一个缩放的图片 ...