zoukankan      html  css  js  c++  java
  • poj 3694 Network

    /*哈哈我Tarjan套lca.. 丫输出case啥的没看见...*/
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define maxn 300010
    using namespace std;
    int n,m,num,head[maxn],low[maxn],dfn[maxn],topt;
    int s[maxn],top,f[maxn],sum,belong[maxn],cnt,cas;
    int Num,Head[maxn],fa[maxn],c[maxn],ee[maxn],vis[maxn];
    struct node{int v,pre;}e[maxn*2];
    struct Node{int v,pre;}E[maxn*2];
    void Add(int from,int to)
    {
        e[num].v=to;
        e[num].pre=head[from];
        head[from]=num++;
    }
    void add(int from,int to)
    {
        E[Num].v=to;
        E[Num].pre=Head[from];
        Head[from]=Num++;
    }
    void Tarjan(int x,int fa)
    {
        low[x]=dfn[x]=++topt;
        s[++top]=x;f[x]=1;
        for(int i=head[x];i!=-1;i=e[i].pre)
          {
              int v=e[i].v;
              if(i==(fa^1))continue;
              if(dfn[v]==0)
                {
                    Tarjan(v,i);low[x]=min(low[x],low[v]);
              }
            else if(f[v])low[x]=min(low[x],dfn[v]);
          }
        if(low[x]==dfn[x])
          {
              sum++;
              while(x!=s[top])
                {
                    f[s[top]]=0;belong[s[top]]=sum;top--;
              }
            f[s[top]]=0;belong[s[top]]=sum;top--;
          }
    }
    void Dfs(int now,int from,int dep,int Ei)
    {
        fa[now]=from;c[now]=dep;ee[now]=Ei;
        for(int i=Head[now];i!=-1;i=E[i].pre)
          if(E[i].v!=from)
            Dfs(E[i].v,now,dep+1,i);
    }
    void LCA(int a,int b)
    {
        if(c[a]<c[b])swap(a,b);
        int t=c[a]-c[b];
        for(int i=1;i<=t;i++)
          {
              if(vis[ee[a]]==0)cnt--;
              vis[ee[a]]=1;a=fa[a];
          }
        while(a!=b)
          {
              if(vis[ee[a]]==0)cnt--;
            vis[ee[a]]=1;a=fa[a];
            if(vis[ee[b]]==0)cnt--;
              vis[ee[b]]=1;b=fa[b];
          }
    }
    int main()
    {
        while(1)
          {
              scanf("%d%d",&n,&m);
              if(n==0&&m==0)break;
              memset(head,-1,sizeof(head));
              memset(Head,-1,sizeof(Head));
              memset(low,0,sizeof(low));
              memset(dfn,0,sizeof(dfn));
              memset(f,0,sizeof(f));
              memset(belong,0,sizeof(belong));
              memset(vis,0,sizeof(vis));
              memset(c,0,sizeof(c));
              memset(ee,0,sizeof(ee));
              memset(fa,0,sizeof(fa));
              memset(s,0,sizeof(s));
              Num=num=topt=sum=0;
              int u,v;
              for(int i=1;i<=m;i++)
                {
                    scanf("%d%d",&u,&v);
                    Add(u,v);Add(v,u);
              }
            for(int i=1;i<=n;i++)
              if(dfn[i]==0)
                Tarjan(i,-1);
            for(int u=1;u<=n;u++)
              for(int i=head[u];i!=-1;i=e[i].pre)
                if(belong[u]!=belong[e[i].v])
                  add(belong[u],belong[e[i].v]);
            Dfs(1,1,0,0);cnt=sum-1;
            scanf("%d",&m);
            printf("Case %d:
    ",++cas);
            for(int i=1;i<=m;i++)
              {
                  scanf("%d%d",&u,&v);
                  int U=belong[u];
                  int V=belong[v];
                  if(U==V)
                    {
                        printf("%d
    ",cnt);continue;
                  }
                LCA(U,V);
                printf("%d
    ",cnt);
              }
          }
        return 0;
    }
  • 相关阅读:
    微信开发:MySQL utf8mb4 字符集
    Spring 事务
    Exception
    mysql系列之多实例介绍
    python连接MySQL
    1_archlinux_安装篇
    apache中如何调用CGI脚本
    1.1_Django简介及安装
    git分支合并脚本
    用python收集系统信息
  • 原文地址:https://www.cnblogs.com/yanlifneg/p/5801444.html
Copyright © 2011-2022 走看看