• poj3417


    Network
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 5665   Accepted: 1602

    Description

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN's business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN's network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.

    As the DN's best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.

    Input

    The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.

    Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.

    Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.

    Output

    Output a single integer — the number of ways to divide the network into at least two parts.

    Sample Input

    4 1
    1 2
    2 3
    1 4
    3 4
    

    Sample Output

    3

    Source

    此题可采用树上差分,差分时w[x],w[y]分别加一,而w[lca(x,y)]-=2;

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<cmath>
    using namespace std;
    
    struct my{
       int next;
       int v;
    };
    
    const int maxn=1000000+10;
    int  adj[maxn],fa,d[maxn],f[maxn][21],t,w[maxn];
    queue<int>Q;
    my bian[maxn*2];
    
    void myinsert(int u,int v){
         bian[++fa].v=v;
         bian[fa].next=adj[u];
         adj[u]=fa;
    }
    
    void bfs(){
         d[1]=1;
         Q.push(1);
         while(!Q.empty()){
            int x=Q.front();
            Q.pop();
            for (int i=adj[x];i;i=bian[i].next){
                int v=bian[i].v;
                if(d[v]) continue;
                d[v]=d[x]+1;
                f[v][0]=x;
                for (int j=1;j<=t;j++)
                    f[v][j]=f[f[v][j-1]][j-1];
                Q.push(v);
            }
         }
    }
    
    int lca(int x,int y){
        if(d[x]>d[y]) swap(x,y);
        for (int i=t;i>=0;i--) if(d[f[y][i]]>=d[x]) y=f[y][i];
        if(x==y) return x;
        for (int i=t;i>=0;i--) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
        return f[x][0];
    }
    
    void dfs(int x,int fa){
         for (int i=adj[x];i;i=bian[i].next){
            int v=bian[i].v;
            if(v==fa) continue;
            dfs(v,x);
            w[x]+=w[v];
         }
    }
    
    int main(){
        int n,m;
        int u,v;
        scanf("%d%d",&n,&m);
        for (int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            myinsert(u,v);
            myinsert(v,u);
        }
        t=int(log(n)/log(2))+1;
        bfs();
        for (int i=1;i<=m;i++){
            scanf("%d%d",&u,&v);
            w[u]++;
            w[v]++;
            w[lca(u,v)]-=2;
        }
        int  ans=0;
        dfs(1,1);
        for (int i=2;i<=n;i++){
             if(!w[i]) ans+=m;
            else if(w[i]==1) ans++;
        }
        printf("%d
    ",ans);
    return 0;
    }
  • 相关阅读:
    通过 phantomjs抓取仁医在线的练习题
    Fork/Join框架
    联表命中索引
    docker容器安装Vim
    docker 配合nginx部署vue项目
    docker安装与简单使用
    delphi IdHTTP 请求抓包分析
    delphi XML 文档注释
    关于Swing设置窗口图标后出现卡顿以及其他问题
    内部类和静态内部类的区别
  • 原文地址:https://www.cnblogs.com/lmjer/p/8719466.html
Copyright © 2020-2023  润新知