zoukankan      html  css  js  c++  java
  • 洛谷P3469 [POI2008]BLO-Blockade(割点)

    题目描述

    There are exactly nnn towns in Byteotia.

    Some towns are connected by bidirectional roads.

    There are no crossroads outside towns, though there may be bridges, tunnels and flyovers. Each pair of towns may be connected by at most one direct road. One can get from any town to any other-directly or indirectly.

    Each town has exactly one citizen.

    For that reason the citizens suffer from loneliness.

    It turns out that each citizen would like to pay a visit to every other citizen (in his host's hometown), and do it exactly once. So exactly n⋅(n−1)ncdot (n-1)n(n1) visits should take place.

    That's right, should.

    Unfortunately, a general strike of programmers, who demand an emergency purchase of software, is under way.

    As an act of protest, the programmers plan to block one town of Byteotia, preventing entering it, leaving it, and even passing through.

    As we speak, they are debating which town to choose so that the consequences are most severe.

    Task Write a programme that:

    reads the Byteotian road system's description from the standard input, for each town determines, how many visits could take place if this town were not blocked by programmers, writes out the outcome to the standard output.

    给定一张无向图,求每个点被封锁之后有多少个有序点对(x,y)(x!=y,1<=x,y<=n)满足x无法到达y

    输入格式

    In the first line of the standard input there are two positive integers: nnn and mmm (1≤n≤100 0001le nle 100 0001n100 000, 1≤m≤500 0001le mle 500 0001m500 000) denoting the number of towns and roads, respectively.

    The towns are numbered from 1 to nnn.

    The following mmm lines contain descriptions of the roads.

    Each line contains two integers aaa and bbb (1≤a<b≤n1le a<ble n1a<bn) and denotes a direct road between towns numbered aaa and bbb.

    输出格式

    Your programme should write out exactly nnn integers to the standard output, one number per line. The ithi^{th}ith line should contain the number of visits that could not take place if the programmers blocked the town no. iii.

    题意翻译

    题目描述

    在Byteotia有n个城镇。 一些城镇之间由无向边连接。 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些)。每两个城镇之间至多只有一条直接连接的道路。人们可以从任意一个城镇直接或间接到达另一个城镇。 每个城镇都有一个公民,他们被孤独所困扰。事实证明,每个公民都想拜访其他所有公民一次(在主人所在的城镇)。所以,一共会有n*(n-1)次拜访。

    不幸的是,一个程序员总罢工正在进行中,那些程序员迫切要求购买某个软件。

    作为抗议行动,程序员们计划封锁一些城镇,阻止人们进入,离开或者路过那里。

    正如我们所说,他们正在讨论选择哪些城镇会导致最严重的后果。

    编写一个程序:

    读入Byteotia的道路系统,对于每个被决定的城镇,如果它被封锁,有多少访问不会发生,输出结果。

    输入输出格式

    第一行读入n,m,分别是城镇数目和道路数目

    城镇编号1~n

    接下来m行每行两个数字a,b,表示a和b之间有有一条无向边

    输出n行,每行一个数字,为第i个城镇被锁时不能发生的访问的数量。

    翻译提供者:Park

    输入输出样例

    输入 #1
    5 5
    1 2
    2 3
    1 3
    3 4
    4 5
    
    输出 #1
    8
    8
    16
    14
    8
    首先分类讨论,当节点i不是割点的时候,删除掉与其相连的边后其他各个点还是连通的,只有i与剩下的点不连通,故此时答案为2*(n-1)(因为是有序对,所以(x,y)和(y,x)不一样,要乘以2)。
    然后讨论i是割点的情况。由割点的定义,删除掉i后整张图会分成若干个连通块。由于tarjan算法是基于搜索树的,所以这里不妨借助搜索树来分析。
    如蓝书所说,不妨假设搜索树上节点i的子节点集合里共有t个割点,那么删除掉i后整张图至多分成t+2个连通块(t个割点算上i共有t+1个割点,而最多的情况就是t+2个连通块串成一条链)。有这么三种连通块:
    1.i自己 2.t个连通块,分别由搜索树上以sk为根的子树的节点构成 3.可能还有一个连通块,由除了上述节点之外的所有点构成(为啥说可能呢,因为i可能是树根)
    因此跑tarjan的时候每次递归后先求出子树大小,然后分这两大类计算即可(如果是割点的话还得拆成三部分)。
    注意是双向边数组别开小了,答案用long long保存,注意是有序对。
    #include <bits/stdc++.h>
    #define N 100005
    #define M 1000005
    using namespace std;
    int head[N],ver[M],Next[M];
    int dfn[N],low[N],size[N];
    long long ans[N];
    bool cut[N];
    int n,m,tot=0,num=0;
    void add(int x,int y)
    {
        ver[++tot]=y,Next[tot]=head[x],head[x]=tot;
    }
    void tarjan(int x)
    {
        dfn[x]=low[x]=++num;size[x]=1;//目前只有一个点
        int flag=0,sum=0,i;
        for(i=head[x];i;i=Next[i])
        {
            int y=ver[i];
            if(!dfn[y])
            {
                tarjan(y);
                size[x]+=size[y];//递归完子节点后更新子树大小
                low[x]=min(low[x],low[y]);
                if(low[y]>=dfn[x])
                {
                    flag++;//子树的割点数目更新
                    ans[x]+=(long long)size[y]*(n-size[y]);
                    sum+=size[y];
                    if(x!=1||flag>1) cut[x]=true;//割点判定法则 
                 } 
            }
            else low[x]=min(low[x],dfn[y]); 
        }
        if(cut[x]) ans[x]+=(long long)(n-sum-1)*(sum+1)+(n-1);
        else ans[x]=2*(n-1);
         
    }
    int main()
    {
        cin>>n>>m;
        tot=1;
        int i;
        for(i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(x==y)continue;
            add(x,y),add(y,x);
        }
        tarjan(1);
        for(i=1;i<=n;i++)
        {
            printf("%lld
    ",ans[i]);
        }
        return 0;
    }


  • 相关阅读:
    while (cin>>str)退出死循环
    内存溢出(heap corruption detected:)
    二叉树的遍历--递归+非递归(两种)
    直接插入排序(带哨兵和不带哨兵)
    二项队列
    左式堆
    优先队列之二叉堆与d-堆
    centos6.4 安装code::blocks
    结构之美——优先队列基本结构(四)——二叉堆、d堆、左式堆、斜堆
    数据结构与算法分析-开放定址散列表的实现
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12830954.html
Copyright © 2011-2022 走看看