zoukankan      html  css  js  c++  java
  • POJ3417:Network

    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

    这题还是蛮妙的啊

    首先注意到这是一棵树,树上加边,现在要删除一条树的边和一条补边使它不联通

    单单只有树的话删什么的可以,但是还有补边

    但其实补边支持的还是两个点路径之间所有的边,那么我们就染色来体现这个操作

     树上差分,当前d[x]++,d[y]++,d[LCA(x,y)]-=2;

    最后DFS一遍,就可以得到每个点到它父亲的值

    如果为0,随便删补边,如果为1,只能删与它关联的补边,如果>=2,那就不可行

    //MT_LI
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    int n,m;
    struct node{
        int x,y,next;
    }a[810000];int len,last[210000];
    int d[210000],bin[21];
    int f[210000][21],dep[210000];
    void ins(int x,int y)
    {
        len++;
        a[len].x=x;a[len].y=y;
        a[len].next=last[x];last[x]=len;
    }
    void dfs(int x,int fa)
    {
        dep[x]=dep[fa]+1;
        f[x][0]=fa;
        for(int i=1;bin[i]<=dep[x];i++)
            f[x][i]=f[f[x][i-1]][i-1];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(y!=fa)
                dfs(y,x);
        }
    }
    int LCA(int x,int y)
    {
        if(dep[x]<dep[y])swap(x,y);
        for(int i=20;i>=0;i--)
            if(dep[y]+bin[i]<=dep[x])
                x=f[x][i];
        if(x==y)return x;
        for(int i=20;i>=0;i--)
            if(dep[x]>=bin[i]&&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 k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(y!=fa)
            {
                DFS(y,x);
                d[x]+=d[y];
            }
        }
    }
    int main()
    {
        bin[0]=1;
        for(int i=1;i<=20;i++)bin[i]=bin[i-1]<<1;
        scanf("%d%d",&n,&m);
        len=0;memset(last,0,sizeof(last));
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            ins(x,y);ins(y,x);
        }
        dfs(1,0);
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            d[x]++;d[y]++;
            d[LCA(x,y)]-=2;
        }
        DFS(1,0);
        int ans=0;
        for(int i=2;i<=n;i++)
        {
            if(d[i]==0)ans+=m;
            if(d[i]==1)ans++;
        }
        printf("%d
    ",ans);
        return 0;
    }
    /*
    9 2
    1 2
    1 3
    1 4
    2 5
    2 6
    4 7
    4 8
    7 9
    6 7
    9 8
    */
    The deepest love I think,later than apart,I will live as you like
  • 相关阅读:
    基于Appium的自动化case开发及case分层结构设计
    功能自动化接入持续集成方案
    Windows上部署Appium自动化测试框架
    Mac上部署Appium测试框架
    Appium原理简述
    开篇
    数据结构和算法动态可视化
    Request实现简易注册登录
    过滤器解决中文乱码
    简易登录拦截(没有登录前直接访问主页则跳转到登录页)
  • 原文地址:https://www.cnblogs.com/MT-LI/p/9684917.html
Copyright © 2011-2022 走看看