HDU 5409 CRB and Graph Tarjan求桥,点双联通+思维好题

Problem Description
A connected, undirected graph of N vertices and M edges is given to CRB.
A pair of vertices (u, v) (u < v) is called critical for edge e if and only if u and v become disconnected by removing e.
CRB’s task is to find a critical pair for each of M edges. Help him!

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers N, M denoting the number of vertices and the number of edges.
Each of the next M lines contains a pair of integers a and b, denoting an undirected edge between a and b.
1 ≤ T ≤ 12
1 ≤ N, M ≤ 105
1 ≤ a, b ≤ N
All given graphs are connected.
There are neither multiple edges nor self loops, i.e. the graph is simple.

Output
For each test case, output M lines, i-th of them should contain two integers u and v, denoting a critical pair (u, v) for the i-th edge in the input.
If no critical pair exists, output “0 0” for that edge.
If multiple critical pairs exist, output the pair with largest u. If still ambiguous, output the pair with smallest v.

Sample Input

2
3 2
3 1
2 3
3 3
1 2
2 3
3 1

Sample Output

1 2
2 3
0 0
0 0
0 0

题意:一个联通的无向图, 对于每一条边, 若删除该边后存在两点不可达,则输出这两个点, 如果存在多个则输出第一个点尽可能大,第二个点尽可能小的。 不存在输出0 0

解法:首先 若删除某一条边后存在多个联通分量则该边一定是桥, 那么我们可以先处理出所有的桥,然后把所有双联通分量缩点,缩点之后就变成了一棵树。而树上的每一条边都是一个桥, 考虑每条边的输出,删除某一边后肯定会出现两个联通分量, 需要记录两个联通分量中最大的点max1 max2, 如果max1!=n 则答案就是max1 max1+1否则max2 max2+1。现在的问题就转化为了如何求 不包含n的联通分量的最大值,因为两个联通分量肯定有一个联通分量的最大值为n, 所以 我们可以从包含n这个点的联通分量开始DFS, 每次遍历后更新一下,此时子树的最大值就是答案。

///HDU 5409
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
typedef pair<int,int> pii;
vector <pii> G[maxn];
bool isBridge[maxn], vis[maxn];
int n, m, dfsclk, pre[maxn], low[maxn], maxv[maxn], newId[maxn], newMax[maxn], IDX, U[maxn], V[maxn], ans[maxn];
void init(){
    dfsclk = 0;
    memset(pre, 0, sizeof(pre));
    memset(low, 0, sizeof(low));
    memset(isBridge, 0, sizeof(isBridge));
    for(int i=0; i<maxn; i++) G[i].clear();
}
void dfs1(int u, int pa){//Tarjan求桥
    int lowu;
    lowu = pre[u] = ++dfsclk;
    for(int  i=0; i<G[u].size(); i++){
        pii e = G[u][i];
        int v = e.first;
        int edgeID = e.second;
        if(!pre[v]){
            dfs1(v, u);
            lowu = min(lowu, low[v]);
            if(low[v] > pre[u]){
                isBridge[edgeID] = true;
            }
        }
        else if(pre[v] < pre[u] && v!=pa){
            lowu = min(lowu, pre[v]);
        }
    }
    low[u] = lowu;
}
void dfs2(int u, int pa){//重新构图,并且维护每一个连通分量的最大的标号
    vis[u] = true;
    maxv[u] = u;
    newId[u] = IDX;
    for(int i=0; i<G[u].size(); i++){
        pii e = G[u][i];
        int v = e.first;
        int edgeID = e.second;
        if(!isBridge[edgeID] && v != pa && !vis[v]){
            dfs2(v, u);
            maxv[u] = max(maxv[u], maxv[v]);
        }
    }
}
void Find_Bridge_And_Rebuild()
{
    dfs1(1, -1);
    memset(vis, false, sizeof(vis));
    IDX = 0;
    for(int i=1; i<=n; i++){
        if(!vis[i]){
            IDX++;
            dfs2(i, -1);
        }
    }
    //清空图,重构
    for(int i=1; i<=n; i++) G[i].clear();
    for(int i=0; i<m; i++){
        if(isBridge[i]){
            int u = newId[U[i]], v = newId[V[i]];
            G[u].push_back(make_pair(v, i));
            G[v].push_back(make_pair(u, i));
        }
    }
}
void solve(int u, int pa)//维护答案
{
    pre[u] = ++dfsclk;
    ans[u] = newMax[u];
    for(int i=0; i<G[u].size(); i++){
        int v = G[u][i].first;
        if(v!=pa){
            solve(v, u);
            ans[u] = max(ans[u], ans[v]);
        }
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        init();
        scanf("%d %d", &n, &m);
        for(int i=0; i<m; i++){
            scanf("%d %d", &U[i], &V[i]);
            G[U[i]].push_back(make_pair(V[i], i));
            G[V[i]].push_back(make_pair(U[i], i));
        }
        memset(newMax, -1, sizeof(newMax));
        Find_Bridge_And_Rebuild();
        for(int i=1; i<=n; i++){
            newMax[newId[i]] = max(newMax[newId[i]],maxv[i]);
        }
        int u;
        for(u = 1; u<=n; u++){
            if(newMax[u] == n){
                break;
            }
        }
        dfsclk=0;
        memset(pre, 0, sizeof(pre));
        solve(u, -1);
        for(int i=0; i<m; i++)
        {
            int u = newId[U[i]], v = newId[V[i]];
            if(u == v){
                printf("0 0\n");
            }
            else{
                if(pre[u] < pre[v]){
                    swap(u, v);
                }
                printf("%d %d\n", ans[u], ans[u]+1);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值