P3469 [POI2008]BLO-Blockade

心情不好就写两个题,然后发个题解就完事了啊。

题目描述

There are exactly nn 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 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.

输入输出格式

输入格式:

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

The towns are numbered from 1 to nn.

The following mm lines contain descriptions of the roads.

Each line contains two integers aa and bb (1le a<ble n1a<bn) and denotes a direct road between towns numbered aa and bb.

输出格式:

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


但是,英文是要怎样!!!幸亏我谷有中文,不然我就自闭去了啊。

题目描述

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

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

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

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

编写一个程序:

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

输入输出格式

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

城镇编号1~n

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

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

输入输出样例

输入样例#1:
5 5
1 2
2 3
1 3
3 4
4 5
输出样例#1:
8
8
16
14
8

先用一句话概括一下:
  给定一张无向图,求每个点被封锁之后有多少个有序点对(x,y)(x!=y,1<=x,y<=n)满足x无法到达y。
思路应该很显然啦啊
            
我们来看一下这张ex的图,是我原创的没错。
  除了我标黑的点以外,随便去掉哪一个都不会对图的连通性造成什么影响。
那么思路就很显然了啊。

对于非割点,答案显然是2(n-1)  (因为它不能影响别的点对连通性,能影响的只是别人到它以及它到别人)

对于割点,它把那几块弄得无法联通,即那几块中不同块的两个点肯定就无法联通了,答案也就是每组块的点的数量互相乘出来,再加上2(n-1)。

这题现在就完事了啊。


放上代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
using namespace std;

const int MA=1e5+1;
int n,m;
int head[MA],ecnt;
int dfn[MA],low[MA],cnt[MA];
long long ans[MA];
int ti,tp;
struct ss{
    int to,nxt,self;
}t[MA*10];
bool cut[MA];

void add(int a,int b) {
    t[++ecnt].nxt=head[a];
    t[ecnt].to=b;
    t[ecnt].self=a;
    head[a]=ecnt;
    return;
}

void Tar(int x) {
    dfn[x]=low[x]=++ti;
    ++cnt[x];
    int sl=0,sum=0;
    for(int i=head[x];i;i=t[i].nxt) {
        int j=t[i].to;
        if(!dfn[j]) {
            Tar(j);
            cnt[x]+=cnt[j];
            low[x]=min(low[x],low[j]);
            if(dfn[x]<=low[j]) {
                sl++;
                ans[x]+=(long long)cnt[j]*(long long)(n-cnt[j]);
                sum+=cnt[j];
                if(x!=1||sl>1) cut[x]=1;
            }
        }
        else low[x]=min(low[x],dfn[j]);
    }
    if(!cut[x]) ans[x]=2*(long long)(n-1);
    else ans[x]+=(long long)(n-sum-1)*(long long)(sum+1)+n-1;
} 

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) {
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    for(int i=1;i<=n;i++) 
        if(!dfn[i]) Tar(i);
    for(int i=1;i<=n;i++)
        printf("%lld
",ans[i]);
    return 0;
}

  写的比较着急,看起来有点丑(还WA了好几次QAQ)

谢谢!


原文地址:https://www.cnblogs.com/qxyzili--24/p/10543043.html