51nod 1470 计算机网络问题 Codeforces Round #310 (Div. 1)E. Case of Computer Network

题意

在网络中有n个点,有m条无向边。现在计划用这个网络传输q个重要消息,第i个消息是从第si个点传到第di个点(经过一条或者多条边,经过一些中间点到达)。
为了防止入侵,我们设计了一个特别的算法。但是这个算法只能用到有向网络中。可是,我们不能添加新边,因此只能修改原来的无向边,使得每一条边只能向某一个方向传输信息。
你的任务是判断一下是否存在一个方案,使得这q个消息都能被成功传送。
在这个样例中,可以这样分配边的方向1 → 2, 1 → 3, 3 → 2, 4 → 3。那么第一个消息传送的路径是 1 → 3,第二个的是4 → 3 → 2。

题解

终于做了这个题了我当时想学双连通分量就是因为这题来着
明显地有一个结论,就是一个边双连通分量一定可以找到一个不走重复边的环
这个的话用两个之间一定存在至少两条没有公共边的路径可以得证
所以双连通分量的所有点是可以随便两两到达的
于是我们可以先来一发双连通缩点
然后就变成了一个树
就好搞了
差分一下就可以了

CODE:

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int N=200005*2;
int n,m,q;
struct qq{int x,y,last;}e[N];
int num,last[N];
void init (int x,int y)
{
    num++;
    e[num].x=x;e[num].y=y;
    e[num].last=last[x];
    last[x]=num;
}
int belong[N],cnt;
int dfn[N],low[N],id;
int sta[N],top;
void dfs (int x,int fa)
{
/*  printf("YES:%d %d\n",x,fa);
    system("pause");*/
    low[x]=dfn[x]=++id;sta[++top]=x;
    for (int u=last[x];u!=-1;u=e[u].last)
    {
        int y=e[u].y;
        if (u==fa) continue;
        if (dfn[y]==-1)
        {
            dfs(y,u^1);
            low[x]=min(low[x],low[y]);
        }
        else low[x]=min(low[x],dfn[y]);
    }
    if (low[x]==dfn[x])
    {
        cnt++;int i;
        do
        {
            i=sta[top--];
            belong[i]=cnt;
        }while (i!=x);
    }
}
int f[N][21];
int dep[N];
void dfs1 (int x,int fa)
{
    f[x][0]=fa;
    for (int u=1;u<=20;u++) f[x][u]=f[f[x][u-1]][u-1];
    for (int u=last[x];u!=-1;u=e[u].last)
    {
        int y=e[u].y;
        if (y==fa) continue;
        dep[y]=dep[x]+1;
        dfs1(y,x);
    }
}
int get (int x,int y)
{
    if (dep[x]>dep[y]) swap(x,y);
    for (int u=20;u>=0;u--)
        if (dep[f[y][u]]>=dep[x]) 
            y=f[y][u];
    if (x==y) return x;
    for (int u=20;u>=0;u--)
        if (f[y][u]!=f[x][u])
        {
            x=f[x][u];y=f[y][u];
        }
    return f[x][0];
}
int c[N],c1[N];//向上的标记   向下的标记 
bool tf=true;
void solve (int x,int fa)
{
    for (int u=last[x];u!=-1;u=e[u].last)
    {
        int y=e[u].y;
        if (y==fa) continue;
        solve(y,x);
        c[x]+=c[y];
        c1[x]+=c1[y];
    }
    if (c[x]*c1[x]!=0) tf=false;
}
int main()
{
    memset(dfn,-1,sizeof(dfn));
    num=1;memset(last,-1,sizeof(last));
    scanf("%d%d%d",&n,&m,&q);
    for (int u=1;u<=m;u++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        init(x,y);init(y,x);
    }
    for (int u=1;u<=n;u++)
    {
        if (dfn[u]==-1)
            dfs(u,0);
    }
    num=0;memset(last,-1,sizeof(last));
    for (int u=2;u<=m*2+1;u+=2)
    {
        int x=e[u].x,y=e[u].y;
        if (belong[x]!=belong[y])   
        {
        //  printf("%d %d\n",belong[x],belong[y]);
            init(belong[x],belong[y]);
            init(belong[y],belong[x]);
        }
    }
    for (int u=1;u<=cnt;u++)
    {
        if (dep[u]==0)
        {
            dep[u]=1;
            dfs1(u,0);
        }
    }
    for (int u=1;u<=q;u++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        x=belong[x];y=belong[y];
        if (x==y) continue;
        int lca=get(x,y);
        if (lca==0) {printf("No\n");return 0;}//不在一颗树上 
        c[x]++;c1[y]++;
        c[lca]--;c1[lca]--;
    }
    solve(belong[1],0);
    if (tf) printf("Yes\n");
    else printf("No\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值