Codeforces 652E Pursuit For Artifacts 双连通分量缩点

54 篇文章 0 订阅
23 篇文章 0 订阅

题目链接

http://codeforces.com/problemset/problem/652/E

题意

给定一无向图,图中有些边被标记,某个人从s走到t,每条边只允许走一次,判断从s到t的路径中,是否可能存在被标记的边。(图保证连通)

思路

双连通分量缩点,如果缩点之后,缩点内存在被标记的边,那么将缩点标记。不同缩点之间存在标记边,在新图中同样给边加标记。这样缩点后的图,变成树,树中存在被标记的边和被标记的点。从s点所在缩点,走到t所在缩点,看路径中是否存在被标记的点或边。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define ll long long
using namespace std;
const int INF = ( 2e9 ) + 2;
const ll maxn = 3e5+10;
struct Edge
{
    int u,v;
};
struct edge
{
    int u,v,w,next;
} e[maxn*2],newe[maxn*2];
int head[maxn],head1[maxn];
int tot,tot1;
int dfn[maxn],low[maxn],Stack[maxn],Belong[maxn];
bool Instack[maxn],vis[maxn],p[maxn];
int Index,top,scc;
vector<Edge> vec;
void add(int u,int v,int w)
{
    e[tot].u=u;
    e[tot].v=v;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;
    e[tot].u=v;
    e[tot].v=u;
    e[tot].w=w;
    e[tot].next=head[v];
    head[v]=tot++;
}
void add1(int u,int v,int w)
{
    newe[tot1].u=u;
    newe[tot1].v=v;
    newe[tot1].w=w;
    newe[tot1].next=head1[u];
    head1[u]=tot1++;
    newe[tot1].u=v;
    newe[tot1].v=u;
    newe[tot1].w=w;
    newe[tot1].next=head1[v];
    head1[v]=tot1++;
}
void tarjan(int u,int fa)
{
    dfn[u]=low[u]=++Index;
    Stack[top++]=u;
    Instack[u]=1;
    int flag=1;
    for(int i=head[u]; i!=-1; i=e[i].next)
    {
        int v=e[i].v;
        if(v==fa&&flag)
        {
            flag=0;
            continue;
        }
        if(!dfn[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u])
            {
                int x;
                scc++;
                do
                {
                    x=Stack[--top];
                    Instack[x]=0;
                    Belong[x]=scc;
                }
                while(v!=x);
            }
        }
        else if(Instack[v])
            low[u]=min(low[u],low[v]);
    }
}
void Init_Map()
{
    memset(head,-1,sizeof(head));
    memset(head1,-1,sizeof(head1));
    vec.clear();
    tot=tot1=0;
}
bool dfs(int s,int t,int fa,int f)
{
    vis[s]=1;
    if(s==t&&f)return 1;
    int flag=1;
    for(int i=head1[s]; i!=-1; i=newe[i].next)
    {
        int v=newe[i].v;
        if(flag&&v==fa)
        {
            flag=0;
            continue;
        }
        if(!vis[v])
        {
            int newf=f;
            if(newe[i].w||p[v])
            newf=1;
            if(dfs(v,t,s,newf))return 1;
        }
    }
    return 0;
}
bool solve(int s,int t,int n)
{
    memset(dfn,0,sizeof(dfn));
    memset(Belong,0,sizeof(Belong));
    Index=top=scc=0;
    tarjan(1,-1);

    memset(p,0,sizeof(p));
    for(int i=0,L=vec.size(); i<L; i++)
    {
        int u=vec[i].u,v=vec[i].v;
        if(Belong[u]==Belong[v])
        {
            p[Belong[u]]=1;
        }
    }
    for(int i=0; i<tot; i+=2)
    {
        int u=e[i].u,v=e[i].v;
        if(Belong[u]!=Belong[v])
        {
            add1(Belong[u],Belong[v],e[i].w);
        }
    }
    int flag=0;
    if(p[Belong[s]])
        flag=1;
    return dfs(Belong[s],Belong[t],-1,flag);
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int u,v,w;
        Init_Map();
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            if(w)vec.push_back(Edge {u,v});
        }
        int s,t;
        scanf("%d%d",&s,&t);
        if(solve(s,t,n))
            printf("YES\n");
        else
            printf("NO\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值