标签:eterm strong cti ret follow term 编号 open 提供者
在Byteotia有n个城镇。 一些城镇之间由无向边连接。 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些)。每两个城镇之间至多只有一条直接连接的道路。人们可以从任意一个城镇直接或间接到达另一个城镇。 每个城镇都有一个公民,他们被孤独所困扰。事实证明,每个公民都想拜访其他所有公民一次(在主人所在的城镇)。所以,一共会有n*(n-1)次拜访。
不幸的是,一个程序员总罢工正在进行中,那些程序员迫切要求购买某个软件。
作为抗议行动,程序员们计划封锁一些城镇,阻止人们进入,离开或者路过那里。
正如我们所说,他们正在讨论选择哪些城镇会导致最严重的后果。
编写一个程序:
读入Byteotia的道路系统,对于每个被决定的城镇,如果它被封锁,有多少访问不会发生,输出结果。
第一行读入n,m,分别是城镇数目和道路数目
城镇编号1~n
接下来m行每行两个数字a,b,表示a和b之间有有一条无向边
输出n行,每行一个数字,为第i个城镇被锁时不能发生的访问的数量。
@[chen_zhe](/space/show?uid=8457)
翻译提供者:Park
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 .
5 5
1 2
2 3
1 3
3 4
4 5
8
8
16
14
8
Solution:
Tarjan+简单数学。
对于一个点,分两种情况判断:
1、非割点,那么割掉改点会使得剩下的$n-1$个点与该点不联通,答案就是$2\times (n-1)$。
2、割点,与其相连的割点对答案的贡献为$siz[v]\times (n-siz[v])$,非割点答案贡献为$(n-1-\sum{siz[v]})\times (1+\sum{siz[v]})$,最后还得算上该点减少的组合$n-1$。
实现过程直接在tarjan求割点的同时,统计下子树大小,计算答案就好了。
代码:
/*Code by 520 -- 8.22*/ #include<bits/stdc++.h> #define il inline #define ll long long #define RE register #define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++) #define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--) using namespace std; const int N=1000005; int n,m,low[N],dfn[N],siz[N],tot; int to[N],net[N],h[N],cnt; int scc,bl[N],stk[N],top; ll ans[N]; bool cut[N]; int gi(){ int a=0;char x=getchar(); while(x<‘0‘||x>‘9‘)x=getchar(); while(x>=‘0‘&&x<=‘9‘)a=(a<<3)+(a<<1)+(x^48),x=getchar(); return a; } il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;} void tarjan(int u){ dfn[u]=low[u]=++tot,siz[u]=1; int ppx=0,sum=0; for(RE int i=h[u];i;i=net[i]) if(!dfn[to[i]]){ tarjan(to[i]), siz[u]+=siz[to[i]], low[u]=min(low[u],low[to[i]]); if(low[to[i]]>=dfn[u]){ ppx++; ans[u]+=1ll*siz[to[i]]*(n-siz[to[i]]); sum+=siz[to[i]]; if(u!=1||ppx>1) cut[u]=1; } } else low[u]=min(low[u],dfn[to[i]]); if(cut[u]) ans[u]+=1ll*(n-sum-1)*(sum+1)+(n-1); else ans[u]=n-1<<1; } il void init(){ n=gi(),m=gi(); int u,v; For(i,1,m) { u=gi(),v=gi(); if(u!=v) add(u,v),add(v,u); } tarjan(1); For(i,1,n) printf("%lld\n",ans[i]); } int main(){ init(); return 0; }
标签:eterm strong cti ret follow term 编号 open 提供者
原文地址:https://www.cnblogs.com/five20/p/9521888.html