您的位置:首页 > 其它

HDU 3749 Financial Crisis 点双连通分量

2016-02-18 23:28 337 查看
询问两点间是不联通,还是只有一条路,还是有多条路。

不联通用并查集处理即可。

只有一条路说明经过了割点,求一次点双联通分量,多条路说明在点双连通分量内。

#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#define FOR(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int N = 5005, M = 20005;
int dfn
, low
, belong
, out
, bcc;
int fa
, top, ts;
int h
, p[M], v[M], cnt;
pair<int, int> stack[M];
set<int> bcc_set
;

int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void merge(int x, int y) {
x = find(x); y = find(y);
fa[x] = y;
}

void add(int x, int y) {
p[cnt] = h[x]; v[cnt] = y; h[x] = cnt++;
}

void tarjan(int u, int fa) {
dfn[u] = low[u] = ++ts;
for (int i = h[u]; i != -1; i = p[i]) {
pair<int, int> edge = make_pair(u, v[i]);
if (v[i] == fa) continue;
if (!dfn[v[i]]) {
stack[++top] = edge;
tarjan(v[i], u);
low[u] = min(low[u], low[v[i]]);
if (low[v[i]] >= dfn[u]) {
bcc_set[++bcc].clear();
int a, b;
do {
a = stack[top].first, b = stack[top].second;
--top;
if (belong[a] != bcc) belong[a] = bcc, bcc_set[bcc].insert(a);
if (belong[b] != bcc) belong[b] = bcc, bcc_set[bcc].insert(b);
} while (a != u || b != v[i]);
}
} else if (dfn[v[i]] < dfn[u]) {
stack[++top] = edge;
low[u] = min(low[u], dfn[v[i]]);
}
}
}

int main() {
int i, j, x, y, n, m, q, ans, kase = 0;
while (scanf("%d%d%d", &n, &m, &q) == 3 && n && m && q) {
memset(dfn, 0, sizeof dfn);
memset(h, -1, sizeof h);
memset(out, 0, sizeof out);
memset(belong, 0, sizeof belong);
FOR(i,1,n) fa[i] = i;
bcc = cnt = top = ts = 0;
while (m--) {
scanf("%d%d", &x, &y); ++x, ++y;
add(x, y); add(y, x); merge(x, y);
}
FOR(i,1,n) if (!dfn[i]) tarjan(i, 0);
printf("Case %d:\n", ++kase);
while (q--) {
scanf("%d%d", &x, &y); ++x, ++y;
if (find(x) != find(y)) puts("zero");
else {
FOR(i,1,bcc) if (bcc_set[i].count(x) && bcc_set[i].count(y) && bcc_set[i].size() > 2)
{ puts("two or more"); i = -1; break; }
if (i != -1) puts("one");
}
}
}
return 0;
}


Financial Crisis

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 301 Accepted Submission(s): 92

Problem 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: