HDU 3749 Financial Crisis(双连通分量)

本题考察的是无向图的点双连通分量

Financial Crisis(HDU3749

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1000 Accepted Submission(s): 325

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

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 20100;
int n,m,q;
int ecnt,ji,hea,cntt;
struct Edge{
    int next,to;
}edge[MAXN*2];
int F[MAXN],head[MAXN],DFN[MAXN],low[MAXN];
void add(int u,int v){
    edge[ecnt].to = v;
    edge[ecnt].next = head[u];
    head[u] = ecnt++;
}
struct Node{
    int u,v;
}stk[MAXN];
vector<int>v[MAXN];
int sz[MAXN];
void init(){
    ecnt = 1;
    ji = hea = cntt= 0;
    for(int i = 0; i <=n+10; i++){
        F[i] = i;
        head[i] = low[i] = sz[i] = 0;
        DFN[i] = -1;
        v[i].clear();
    }
    memset(edge,0,sizeof(edge));

}
int find(int x){
    if(x == F[x])return x;
    return F[x] = find(F[x]);
}

void tarjan(int x,int f){
    DFN[x] = low[x] = ++ji;
    for(int i = head[x]; i; i = edge[i].next){
        int to = edge[i].to;
        if(to != f){
            Node e;
            e.u = x; e.v = to;
            if(DFN[to] == -1){
                stk[++hea] = e;
                tarjan(to,x);
                low[x] = min(low[x],low[to]);
                if(low[to] >= DFN[x]){//找到割点
                    Node temp;
                    set<int> s;
                    cntt++;//记录点双分量的个数,标记点
                    while(1){
                        temp = stk[hea--];
                        s.insert(temp.u);
                        s.insert(temp.v);
                        v[cntt].push_back(temp.u);
                        v[cntt].push_back(temp.v);
                        if(temp.u == x && temp.v == to)break;
                    }
                    sz[cntt] = s.size();
                }
            }
            else{
                if(DFN[to] < DFN[x])stk[++hea] = e;
                low[x] = min(low[x],DFN[to]);
            }
        }
    }
}
int check(int x,int y){
    int flagx,flagy;
    for(int i = 1; i <= cntt; i++){
        flagx = flagy = 0;
        for(int j = 0; j < v[i].size(); j++){
            if(v[i][j] == x){
                if(flagy == 1){
                    if(sz[i] > 2)return 1;
                    else return 0;//当输入的图是两点一线时,为特殊情况,应输出one
                }
                else flagx = 1;
            }
            if(v[i][j] == y){
                if(flagx == 1){
                    if(sz[i] > 2)return 1;
                    else return 0;
                }
                else flagy = 1;
            }
        }
    }
    return 0;
}
int main(){
    int T = 0;
    while(~scanf("%d %d %d",&n,&m,&q)){
        if(n == 0 && m == 0 && q == 0)break;
        init();
        for(int i = 1; i <= m; i++){
            int x,y;
            scanf("%d %d",&x,&y);
            x++;y++;
            add(x,y);add(y,x);
            int fx = find(x);
            int fy = find(y);
            if(fx != fy) F[fx] = fy;
        }
        for(int i = 1; i <= n; i++){
            if(DFN[i] == -1){
                tarjan(i,0);
            }
        }
        printf("Case %d:\n",++T);
        for(int i = 1; i <= q; i++){
            int x,y;
            scanf("%d %d",&x,&y);
            x++;y++;
            int fx = find(x),fy = find(y);
            if(fx != fy){
                printf("zero\n");
                continue;
            }
            if(check(x,y))printf("two or more\n");
            else printf("one\n");
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值