51nod_1076 2条不相交的路径(边双连通分量)

2条不相交的路径

Problem Description

给出一个无向图G的顶点V和边E。进行Q次查询,查询从G的某个顶点V[s]到另一个顶点V[t],是否存在2条不相交的路径。(两条路径不经过相同的边)
(注,无向图中不存在重边,也就是说确定起点和终点,他们之间最多只有1条路)

Input

第1行:2个数M N,中间用空格分开,M是顶点的数量,N是边的数量。(2 <= M <= 25000, 1 <= N <= 50000)
第2 - N + 1行,每行2个数,中间用空格分隔,分别是N条边的起点和终点的编号。例如2 4表示起点为2,终点为4,由于是无向图,所以从4到2也是可行的路径。
第N + 2行,一个数Q,表示后面将进行Q次查询。(1 <= Q <= 50000)
第N + 3 - N + 2 + Q行,每行2个数s, t,中间用空格分隔,表示查询的起点和终点。

Output

共Q行,如果从s到t存在2条不相交的路径则输出Yes,否则输出No。

Sample Input

4 4
1 2
2 3
1 3
1 4
5
1 2
2 3
3 1
2 4
1 4

Sample Output

Yes
Yes
Yes
No
No

题解:

求两个点之间是否有两条不相交的路径,就是求两个点是否在一个边双连通分量内。求出原图中的桥,然后删去桥,剩下的两点如果仍连通则存在两条不相交路径的边。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 25100;
const int mod = 1000000007;
struct node{
    int to, id;
    node(){}
    node(int a, int b):to(a),id(b){}
};
int cnt, dfn[maxn], low[maxn];
int fr[2*maxn], to[2*maxn], is[2*maxn], a[maxn];
vector<node> g[maxn];
int Find(int x);
void trajan(int u, int fa);

int main()
{
    int n, m, i, j, k, q;
    scanf("%d %d", &n, &m);
    for(i=0;i<m;i++){
        scanf("%d %d", &fr[i], &to[i]);
        g[fr[i]].push_back(node(to[i], i));
        g[to[i]].push_back(node(fr[i], i));
    }
    memset(dfn, -1, sizeof(dfn));
    for(i=1;i<=n;i++)
        if(dfn[i] == -1)trajan(i, 0);
    for(i=1;i<=n;i++)
        a[i] = i;
    for(i=0;i<m;i++)
        if(!is[i]){
            int x=Find(fr[i]), y=Find(to[i]);
            if(x != y)a[x] = y;
        }
    scanf("%d", &q);
    while(q--)
    {
        scanf("%d %d", &i, &j);
        if(Find(i) == Find(j))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

int Find(int x)
{
    return a[x]=a[x]==x?x:Find(a[x]);
}

void trajan(int u, int fa)
{
    dfn[u] = low[u] = ++cnt;
    for(int i=0;i<g[u].size();i++)
    {
        if(g[u][i].to == fa)continue;
        node e = g[u][i];
        if(dfn[e.to] == -1){
            trajan(e.to, u);
            low[u] = min(low[u], low[e.to]);
            if(dfn[u]<low[e.to])is[e.id] = 1;
        }
        else low[u] = min(low[u], dfn[e.to]);
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值