洛谷—— P3469 [POI2008]BLO-Blockade
题目描述
There are exactly 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 ![](
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: and (, ) denoting the number of towns and roads, respectively.
The towns are numbered from 1 to .
The following lines contain descriptions of the roads.
Each line contains two integers and () and denotes a direct road between towns numbered and .
输出格式:
Your programme should write out exactly integers to the standard output, one number per line. The line should contain the number of visits that could not take place if the programmers blocked the town no. .
输入输出样例
5 51 22 31 33 44 5
8816148
思路:
我们来看一下这道题。
思考一下我们要怎样做呢??
看到题目:给定一张无向图,求每个点被封锁之后有多少个有序点对(x,y)(x!=y,1<=x,y<=n)满足x无法到达y以后大佬们就应该猜到了这道题的大致做法:图论。
我们看到样例以后肯定事先不管他三七二十一,先连起边来再说。。
但是连完边以后呢??
有位大佬说:我们连起边以后先把与这个点直接相连的边删掉,然后再判断有几块联通快,又因为每一块联通快内的所有点又都是联通的,所以每一块联通块所不能到达的就是与他不连通的每一块的点数,累加就好了,但由于数据太大,我们就要用树形dp来优化。。。。
但蒟蒻表示并不会树形dp啊。。。。
哎,无奈只能考虑别的做法了。。。。
我们先来看对于被割掉的这个点来说他所不能到达的点恰好是整张图上所有的点(除她自己之外(废话,他肯定能自己到达自己了!!))这样他所不能联通的共有:从他出去到所有的点,从所有的点出去到他,也就是说共有:(n-1)*2对。
再就是对于其他的联通快,我们只需要预处理出每一个点出发到达的点然后再*2就好了。。
又有人要问了:怎样预处理???!!
我们来样例来看一下;;
对于割掉点3来说:不能到达的点是这样来的呢??
割掉点三之后:我们把整张图分成了这样三部分:(1,2),(3),(4,5)。
点1不能到达的点就是(1,3)(1,4)(1,5);
点2不能到达的点为(2,3),(2,4,)(2,5)
点3不能到达的点为((3,1),(3,2),(3,4,),(3,5)
点4.。。。。。。。((4,1)(4,2)(4,3)
点5.。。。。。。。(5,1)(5,2)(5,3)
由于我们要*2嘛,所以这个地方我们只能出已成一对的形式
这样就还剩下(1,3)(1,4)(1,5)(2,3)(2,4)(2,5)(3,4)(3,5)
看看这些,你能发现什么?!
是不是这样对于这个图来说好理解一些
(反掌我是没太理解。。。)
但是这里我们可以用别的来做:我们对于一个点出去他所到达的和能到达它的之外。剩下的就是他相连的联通块之间的相连的了。
我们在来处理这一部分。在tarjan里面,我们处理出这个点所能到达且是他的子树的的联通块的大小,这样他的父亲树的大小就为总点数减去这个点减去他的子树的大小,即n-1-size[i] i为当前点。 这样我们又能得知他们之间不能相连的点数就是他的父亲节点内的个数*塔子树节点内的个数、。即ans【i】=t*(n-t-1) t=size[i] 这样我们求出了从该点外不能互相到达的对数
再加上它能到达的点的对数就是了。不过这里我们处理出来的是单向到达,我们在输出时再乘以二就好了!!
代码:
#include#include #include #include #include #define N 100005#define ll long long using namespace std;int n,m,x,y,tot,tim;int dfn[N],low[N],size[N],head[N],cut_point[N];ll ans[N];int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x*f;}struct Edge{ int from,next,to;}edge[1000005];void add(int x,int y){ tot++; edge[tot].to=y; edge[tot].next=head[x]; head[x]=tot;}int tarjan(int now){ int z=0;size[now]=1; dfn[now]=low[now]=++tim; for(int i=head[now];i;i=edge[i].next) { int t=edge[i].to; if(!dfn[t]) { tarjan(t); size[now]+=size[t]; low[now]=min(low[now],low[t]); if(dfn[now]<=low[t]) { ans[now]+=(ll)z*size[t]; z+=size[t]; } } else low[now]=min(low[now],dfn[t]); } ans[now]+=(ll)z*(n-z-1);}int main(){ n=read();m=read(); for(int i=1;i<=m;i++) x=read(),y=read(),add(x,y),add(y,x); tarjan(1); for(int i=1;i<=n;i++) printf("%lld\n",(ans[i]+n-1)<<1); return 0;}