zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    redis 集群
    redis--主从复制
    redis--AOF
    React——组件
    React——文件夹分析
    WEB面试
    WEB基础——接收后台文件方法
    WEB基础——AJAX
    C#进阶——IOC
    C#基础——HttpContext
  • 原文地址:https://www.cnblogs.com/lmjer/p/8719466.html
Copyright © 2011-2022 走看看