BLO-Blockade
题意翻译
在Byteotia有n个城镇。 一些城镇之间由无向边连接。 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些)。每两个城镇之间至多只有一条直接连接的道路。人们可以从任意一个城镇直接或间接到达另一个城镇。 每个城镇都有一个公民,他们被孤独所困扰。事实证明,每个公民都想拜访其他所有公民一次(在主人所在的城镇)。所以,一共会有n*(n-1)次拜访。
不幸的是,一个程序员总罢工正在进行中,那些程序员迫切要求购买某个软件。
作为抗议行动,程序员们计划封锁一些城镇,阻止人们进入,离开或者路过那里。
正如我们所说,他们正在讨论选择哪些城镇会导致最严重的后果。
编写一个程序:
读入Byteotia的道路系统,对于每个被决定的城镇,如果它被封锁,有多少访问不会发生,输出结果。
输入输出格式
第一行读入n,m,分别是城镇数目和道路数目
城镇编号1~n
接下来m行每行两个数字a,b,表示a和b之间有有一条无向边
输出n行,每行一个数字,为第i个城镇被锁时不能发生的访问的数量。
题目描述
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)n\cdot (n-1)n⋅(n−1) 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 0001\le n\le 100\ 0001≤n≤100 000 , 1≤m≤500 0001\le m\le 500\ 0001≤m≤500 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≤n1\le a<b\le n1≤a<b≤n ) 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 .
输入输出样例
输入样例#1: 复制
5 5 1 2 2 3 1 3 3 4 4 5
输出样例#1: 复制
8 8 16 14 8
分析:
一道要用tarjan的题目。暂且先当做无序数对做。
很明显切断任意一个点的所有边,它对答案的贡献为n-1(与所有的节点断开)
接下来就是割点的问题了。
首先切断割点的所有边,会将整个图分为若干子树,其中一个子树有它的父亲节点,称为它的父亲数,其他则为它的子树。那么所有子树首先与父亲树是不连通的,其次所有子树互相不连通。将以上三者作和即为答案。
注意最后输出有序数对,答案*2.
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> sq[100100];
int dfn[100100],low[100100],tim=0,sonsize[100100],n,m;
long long ans[100100];
void tarjan(int u)
{
tim++;sonsize[u]=1;
dfn[u]=low[u]=tim;
int i,len=sq[u].size(),sum=0;
for (i=0;i<len;i++)
{
int v=sq[u][i];
if (!dfn[v])
{
tarjan(v);
sonsize[u]+=sonsize[v];
low[u]=min(low[u],low[v]);
if (dfn[u]<=low[v])
{
ans[u]+=(long long)sum*sonsize[v];//子树互不相连
sum+=sonsize[v];
}
}
else low[u]=min(low[u],dfn[v]);
}
ans[u]+=(long long) sum*(n-sum-1);//所有子树与父亲树不相连
}
int main()
{
scanf("%d%d",&n,&m);
int i,j;
for (i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
sq[a].push_back(b);
sq[b].push_back(a);
}
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(ans,0,sizeof(ans));
tarjan(1);
for (i=1;i<=n;i++)
{
ans[i]=(ans[i]+n-1)*2;//当前的点与其他所有点不相连,并处理有序数对
printf("%lld\n",ans[i]);
}
return 0;
}