2195 字
11 分钟
P3469题解

P3469 [POI 2008] 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 n⋅(n−1)n\cdot (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.

输入格式#

In the first line of the standard input there are two positive integers: nn and mm (1≤n≤100 0001\le n\le 100\ 000, 1≤m≤500 0001\le m\le 500\ 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 (1≤a<b≤n1\le a<b\le n) 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 ithi^{th} line should contain the number of visits that could not take place if the programmers blocked the town no. ii.

输入输出样例 #1#

输入 #1#

5 5
1 2
2 3
1 3
3 4
4 5

输出 #1#

8
8
16
14
8

说明/提示#

1≤n≤100 0001\le n\le 100\ 000,1≤m≤500 0001\le m\le 500\ 000。图保证连通。

思路#

题意换成中文:给一张连通无向图,对每个点 uu 求「删掉 uu 之后,有多少次有序访问 (a,b)(a,b) 没法进行」。

一次访问 (a,b)(a,b) 作废,只有两种情况:aa 或 bb 就是 uu 自己;或者 aa 和 bb 原本的唯一通路被 uu 掐断了。

第一种情况很好数:uu 不能出去、别人也不能进来,一共 2(n−1)2(n-1) 次。

第二种情况要看删掉 uu 之后图被切成几块。这不是一般的割点判定——得知道每一块有多大。

Tarjan 给出每一块的大小#

在 DFS 树上跑 Tarjan。对 uu 的一个儿子 vv,如果

low[v]≥dfn[u]low[v]\ge dfn[u]

那么删掉 uu 之后,vv 的整棵子树就和 uu 的祖先那边断开了,它是一块独立的连通块,大小就是 size[v]size[v]。

把满足条件的儿子记作 s1,s2,…,sks_1,s_2,\dots,s_k。除了这些独立块,剩下的部分是「uu 的祖先 + 其它没被切出来的点」,大小

r=n−1−∑isir=n-1-\sum_i s_i

拼出答案#

设 s=∑isis=\sum_i s_i。代码里写的是

ans[u]=2(n−1)+∑isi (n−si−1)+s (n−s−1)ans[u]=2(n-1)+\sum_i s_i\,(n-s_i-1)+s\,(n-s-1)

这两项看着重复,其实是两个方向。把 n−si−1n-s_i-1 拆成 (n−1−s)⏟r+(s−si)⏟其它块\underbrace{(n-1-s)}_{r}+\underbrace{(s-s_i)}_{\text{其它块}}:

∑isi(n−si−1)=s⋅r+∑i≠jsisj\sum_i s_i(n-s_i-1)=s\cdot r+\sum_{i\ne j}s_i s_j

再加上 s⋅rs\cdot r,就正好是「块与块之间的有序对数」:

2 r s⏟剩余块↔各独立块,两个方向+∑i≠jsisj⏟独立块之间\underbrace{2\,r\,s}_{\text{剩余块}\leftrightarrow\text{各独立块,两个方向}}+\underbrace{\sum_{i\ne j}s_i s_j}_{\text{独立块之间}}

拿样例验一下。图是 1−2−31-2-3 三角形、3−43-4、4−54-5,从 11 开始 DFS 得到链 1→2→3→4→51\to2\to3\to4\to5:

  • u=3u=3:儿子 44 满足 low[4]≥dfn[3]low[4]\ge dfn[3],独立块 {4,5}\{4,5\} 大小 22,r=5−1−2=2r=5-1-2=2。答案 =8+2×2+2×2=16=8+2\times2+2\times2=16,和样例对上。
  • u=4u=4:独立块 {5}\{5\} 大小 11,r=3r=3。答案 =8+1×3+1×3=14=8+1\times3+1\times3=14 ✓。
  • u=1u=1:儿子 22 的子树是 {2,3,4,5}\{2,3,4,5\},low[2]low[2] 被回边 3−13-1 压到了 11,所以 low[2]≥dfn[1]low[2]\ge dfn[1] 成立,r=0r=0。答案 =8+0+0=8=8+0+0=8 ✓。

根节点不用特判:dfndfn 从 11 开始,lowlow 最小也是 11,所以根的每个儿子都满足 low[v]≥dfn[root]low[v]\ge dfn[root],自动全被算成独立块——这正是根该有的行为。

复杂度#

一遍 DFS,O(n+m)O(n+m)。

提交曲线:48 → 42 → 82 → 100#

#状态用时 / 内存
1Unaccepted 483.88s / 17.23MB
2Unaccepted 423.81s / 16.48MB
3Unaccepted 82614ms / 25.13MB
4Accepted 100597ms / 24.14MB

前三版各错在不同地方,而且互相掩护——改掉一个就露出下一个。

错误代码 v1(48 分):三处错叠在一起#

int nxt[200050],head[200050],to[200050];
...
if(low[v]>=dfn[u]&&fa!=u) {sum+=size1[v];
ans[u]+=(size1[v])*(n-size1[v]-1);
}
...
ans[u]+=(n-1);
// ans[u]+=(sum)*(n-sum-1);
...
cout<<2*ans[i]<<endl;
  1. 数组只开了 200050。m≤5×105m\le 5\times10^5,无向边要存 2m=1062m=10^6 条,这个数组连一半都放不下。
  2. &&fa!=u:u==fa 说明 uu 是 DFS 树的根,这个条件把根从「切出独立块」的分支里整个排除了。根删掉之后明明也会断。
  3. ans[u]+=(n-1) 配 cout<<2*ans[i],以及 (sum)*(n-sum-1) 那一项被注释掉了。

第三点是这版能拿 48 的原因。输出时统一乘了 22,等价于

2(n−1)+2∑isi(n−si−1)2(n-1)+2\sum_i s_i(n-s_i-1)

而正确答案是 2(n−1)+∑isi(n−si−1)+s(n−s−1)2(n-1)+\sum_i s_i(n-s_i-1)+s(n-s-1)。当 k≤1k\le1(删掉这个点最多切出一块)时,ss 就等于那块的大小,两项合并正好是 2s(n−s−1)2s(n-s-1),两种写法数值相同。所以它在树、链这类「每个点最多切出一块」的数据上全对,一遇到会切出多块的图就崩。

一个歪打正着的常数因子,掩盖了一整项公式。

错误代码 v2(42 分):只是挪了挪分支#

第二版把两处 ans[u]+= 的位置调整了一下(一处挪进 u==fa 的特判里),本质逻辑没变,分数反而掉了 6。这种「改了但没改对」的提交最没信息量。

错误代码 v3(82 分):把公式补全,但根还是不管#

int nxt[1000050],head[1000050],to[1000050];
...
if(low[v]>=dfn[u]&&fa!=u&&v!=fa) {sum+=size1[v];
ans[u]+=(size1[v])*(n-size1[v]-1);
}
...
ans[u]+=2*(n-1);
ans[u]+=(sum)*(n-sum-1);
...
cout<<ans[i]<<endl;

改动清单:数组开到 10610^6、把 2(n−1)2(n-1) 直接写进 ans、恢复 sum*(n-sum-1)、输出不再乘 22。分数直接从 42 跳到 82。

剩下两处:

  • &&fa!=u 还在。于是根节点永远不进这个分支,s=0s=0,它给出的答案退化成 2(n−1)2(n-1)——根是不是割点都这么说。
  • &&v!=fa 是死的。这里在 if(!vis[v]) 里面,vv 没访问过,而父节点必然访问过,所以 v≠fav\ne fa 恒成立。

怎么修改:一张对照表#

错误写法正确写法理由
nxt[200050]nxt[1000050]无向边存两次,2m2m 最大 10610^6
if(low[v]>=dfn[u]&&fa!=u)if(low[v]>=dfn[u])根删掉一样会切断图,通用写法本来就不需要特判根
&&v!=fa删掉外层是 if(!vis[v]),vv 不可能等于父节点
ans[u]+=(n-1) 加输出 2*ansans[u]+=2*(n-1) 加原样输出别把系数藏在输出里,改公式的时候会互相打架
注释掉 sum*(n-sum-1)保留少了它,只有「最多切出一块」的数据能对

正确代码#

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;int cnt=0;
int nxt[1000050],head[1000050],to[1000050];
void ADD(int u,int v){
nxt[++cnt]=head[u];
head[u]=cnt;
to[cnt]=v;
}
int size1[100050];
int dfn[100050],low[100050],fat[100050];int ct;int res=0;
bool vis[100050];int top=0;int stk[100050],sz[100050],bk;
bool gd[100050];
int ans[100050];
priority_queue<int,vector<int>,greater<int>> vava;
void tarjan(int u,int fa)
{
int sum=0;
int child=0;
size1[u]=1;
fat[u]=fa;
dfn[u]=low[u]=++ct;
vis[u]=true;
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(!vis[v]){
child++;
tarjan(v,u);
size1[u]+=size1[v];
low[u]=min(low[u],low[v]);
//nn[u]=max(n[v],nn[u])
if(low[v]>=dfn[u]) {sum+=size1[v];
ans[u]+=(size1[v])*(n-size1[v]-1);
}
}
else{
if(v!=fa){low[u]=min(low[u],dfn[v]);}
}
}
/* if(u==fa&&child>=2) {sum+=n-1;
ans[u]+=2*(n-1);
}*/
/* if(low[u]==dfn[u]){
bk++;
while(true){
int x=stk[top--];
bl[x]=bk;
in_stk[x]=false;
sz[bk]++;
if(x==u) break;
}
}
*/
ans[u]+=2*(n-1);
ans[u]+=(sum)*(n-sum-1);
}
vector<int> u;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
int a,b;cin>>a>>b;
ADD(a,b);ADD(b,a);
}
for(int i=1;i<=n;i++){
if(!vis[i]){
tarjan(i,i);
}
}
for(int i=1;i<=n;i++){
cout<<ans[i]<<endl;
}
/* for(int u=1;u<=n;u++){
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(bl[v]!=bl[u]){
e[bl[u]].push_back[e[bl[v]]];
e[bl[v]].push_back(e[bl[u]]);
}
}
}
for(int i=1;i<=n;i++){
int ans=1;bool fg=false;
if(!gd[i]) cout<<(n-1)*2<<endl;
else{
/* for(auto v:e[bl[i]]){
ans*=sz[v];
}
ans*=(sz[u])//will WA
}
}*/
return 0;
}

我想了什么#

这份代码里躺着一整个没说出口的方案。 stk[]、sz[]、bk、gd[]、in_stk[]、vava(一个从没被 push 过的优先队列)、还有 vector<int> u; 这个空声明——全是点双连通分量模板的残留。中间那段注释掉的就是点双的代码:数出每个点双的大小,再在缩点后的图上做乘法。

那条路走不通的地方自己也标了:ans*=(sz[u])//will WA。点双缩点之后块之间不是树的形状,直接乘会重不漏,注释里那句 //will WA 是当时的判断,后来就没再捡起来。

//nn[u]=max(n[v],nn[u]) 是另一条支路:想在回溯时顺便维护「子树里最大的……」——是给点双那边准备的,跟着一起废了。

根节点的特判被注释掉了,但当时没删干净。 if(u==fa&&child>=2) 那一段本来是想按模板书「根要特判」的写法处理,后来发现用 low[v]≥dfn[u]low[v]\ge dfn[u] 的通用形式时根的每个儿子都会自动满足,就注掉了。可判定的条件里还留着 &&fa!=u,等于把「特判根」用反方向又塞回去了。这是我卡在 82 分那次的原因:公式早就是对的,错的是那个残留条件。

ans[u]+=(size1[v])*(n-size1[v]-1) 在 48 分那版里出现过两次,一次在主循环,一次在根的特判里。两个分支都在加同样的东西,说明当时没想清楚到底哪个分支该负责根。定稿里只剩主循环那一处。

把 2 藏在输出里是个坏习惯。 48 分那版 ans[u]+=(n-1)、输出 2*ans[i],写法上少打一个字,理解上却要倒着看一遍。而且它顺手把 ∑si(n−si−1)\sum s_i(n-s_i-1) 也翻倍了,正好补上了被注释掉的那一项——一个歪打正着的巧合,掩盖了公式缺项。

fat[] 数组存了但没人用。 它跟 fa 参数重复,是写模板时的习惯。

复杂度#

时间 O(n+m)O(n+m),一遍 DFS。

空间 O(n+m)O(n+m),链式前向星占大头。

小结#

这题的公式不算难,难在别把「根要特判」的肌肉记忆乱套。割点判定本身用 low[v]≥dfn[u]low[v]\ge dfn[u] 就够了,根和叶子都在这个式子里面;额外加特判反而会漏。

另一个教训是那 48 分:一个错误的公式 + 一个藏在输出里的常数,可以凑出「一半数据正确」的假象。这种错最难查,因为它不是全错。

第 112 篇,共 147 篇本文属于系列

P3469题解
https://www.hhluckyex.cn/posts/problen_solution/p3469/
作者
HHluckyex
发布于
2026-11-23
许可协议
CC BY-NC-SA 4.0

分享文章

生成精美分享图或复制链接,与更多人分享本文。

继续阅读

沿着主题读

基于共同的标签与分类

换条路线

从其他文章中稳定抽取