P3469[POI2008]LO-Blockade

题意翻译

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

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

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

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

编写一个程序:

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

输入输出格式

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

城镇编号1~n

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

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

题目描述

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\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: \(n\) and \(m\) (\(1\le n\le 100000, 1\le m\le 500000\) )denoting the number of towns and roads, respectively.

The towns are numbered from 1 to \(n\).

The following mm lines contain descriptions of the roads.

Each line contains two integers \(a\) and \(b\) (\(1\le a<b\le n\)) and denotes a direct road between towns numbered \(a\) and \(b\).

输出格式:

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

输入输出样例

输入样例#1:

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

输出样例#1:

8
8
16
14
8

算法

Tarjan找割点

思路

I.设在搜索树T中以x为根的树包含的点集为SubTree(x)。

II.这里去除割点可以理解为删除与该点相连的所有边。

III.这里提到的连通块是指当某一割点去除时:1.其中任何两个点都能相互到达 2.没有更大的连通块包含该块 当然,根据II,我们把单独的X也看做一个连通块

IV.为了方便,我们直接将(x,y)(满足x不能到y)成为“点对”

V. ~S:S的补集,A^B:在A中不包括点B的所有点构成的集合

利用Tarjan查找割点的同时,我们可以找出该割点X去除后剩余的连通块(有两种情况,一种是在SubTree(X)^X中,另一种是~SubTree(X))。

只要能够理解割点的求解过程,这还是很好理解的,这里不再赘述。

然后要求“点对”。

对于一个点X,不管是否为割点,点对(i,j)为“点对”,当且仅当

i != j且i、j属于两个不同的连通块

根据定义,很容易证明这个推论。

代码有多种写法,这里选取我能想到的最简单的写法。

在枚举连通块时,ans加上s[to[i]] * ( n - s[to[i]] ),即一次性处理一个连通块的所有点,它们与其他不属于这个连通块的点都构成“点对”。当然,别忘了X与~SubTree(X)。

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 100005
#define MAXM 1000005 int n, m;
int hd[MAXN], nxt[MAXM], to[MAXM], tot;
int dfn[MAXN], low[MAXN], root, num;
LL ans[MAXN];
int s[MAXN]; void Add( int x, int y ){ nxt[++tot] = hd[x]; hd[x] = tot; to[tot] = y; } void DFS( int x ){
s[x] = 1;
low[x] = dfn[x] = ++num;
LL b(0);
for ( int i = hd[x]; i; i = nxt[i] ){
if ( !dfn[to[i]] ){
DFS( to[i] ); s[x] += s[to[i]];
low[x] = min( low[x], low[to[i]] );
if ( dfn[x] <= low[to[i]] ) ans[x] += (long long)s[to[i]] * ( n - s[to[i]] ), b += s[to[i]];//发现新的连通块!
} else low[x] = min( low[x], dfn[to[i]] );
}
ans[x] += (long long)( n - b - 1 ) * ( b + 1 ) + ( n - 1 );//算上~SubTree(X)与X
} int main(){
scanf( "%d%d", &n, &m );
for ( int i = 1; i <= m; ++i ){
int x, y; scanf( "%d%d", &x, &y ); Add( x, y ); Add( y, x );
}
DFS( 1 );
for ( int i = 1; i <= n; ++i ) printf( "%lld\n", ans[i] );
return 0;
}

「洛谷P3469」[POI2008]BLO-Blockade 解题报告的更多相关文章

  1. 「洛谷P2891」[USACO07OPEN]吃饭Dining 解题报告

    P2891 [USACO07OPEN]吃饭Dining 题目描述 Cows are such finicky eaters. Each cow has a preference for certain ...

  2. 「洛谷P1231」教辅的组成 解题报告

    P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...

  3. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  4. 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】

    题目链接 [洛谷传送门] 题解 很显然,当这个点不是割点的时候,答案是\(2*(n-1)\) 如果这个点是割点,那么答案就是两两被分开的联通分量之间求组合数. 代码 #include <bits ...

  5. 「洛谷4197」「BZOJ3545」peak【线段树合并】

    题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...

  6. 「洛谷3338」「ZJOI2014」力【FFT】

    题目链接 [BZOJ] [洛谷] 题解 首先我们需要对这个式子进行化简,否则对着这么大一坨东西只能暴力... \[F_i=\sum_{j<i} \frac{q_iq_j}{(i-j)^2}-\s ...

  7. 「BZOJ2733」「洛谷3224」「HNOI2012」永无乡【线段树合并】

    题目链接 [洛谷] 题解 很明显是要用线段树合并的. 对于当前的每一个连通块都建立一个权值线段树. 权值线段树处理操作中的\(k\)大的问题. 如果需要合并,那么就线段树暴力合并,时间复杂度是\(nl ...

  8. 「洛谷3870」「TJOI2009」开关【线段树】

    题目链接 [洛谷] 题解 来做一下水题来掩饰ZJOI2019考炸的心情QwQ. 很明显可以线段树. 维护两个值,\(Lazy\)懒标记表示当前区间是否需要翻转,\(s\)表示区间还有多少灯是亮着的. ...

  9. 「洛谷5300」「GXOI/GZOI2019」与或和【单调栈+二进制转化】

    题目链接 [洛谷传送门] 题解 按位处理. 把每一位对应的图都处理出来 然后单调栈处理一下就好了. \(and\)操作处理全\(1\). \(or\)操作处理全\(0\). 代码 #include & ...

随机推荐

  1. Python基础:25文件

    一:文件对象 文件对象不仅可以用来访问普通的磁盘文件, 而且也可以访问任何其它类型抽象层面上的"文件". 一旦设置了合适的"钩子", 你就可以访问具有文件类型接 ...

  2. @bzoj - 4524@ [Cqoi2016]伪光滑数

    目录 @description@ @solution@ @version - 1@ @version - 2@ @accepted code@ @version - 1@ @version - 2@ ...

  3. 设置 Tomcat 的JVM运行内存

    win7,64位: Tomcat7.0.5:jdk1.7: 情况一:Tomcat注册成系统服务,如何修改JVM运行内存? WINDOW 64位 , cmd打开注册表(regedit) HKEY_LOC ...

  4. TAE words all

    // vol 1   could do with sth   rhinoplasty   angst   the wee small hours   familial   Munich   gladi ...

  5. 【u228】圣诞树

    [问题描述] 圣诞特别礼物挂在一棵圣诞树上,这棵树有n层,每层有一件礼物,每件礼物都有一个价值,有的礼物还有一些连结线,与下层的礼物相连,领取礼物的规则如下:任选一件礼物,它的下面如果有连结线,则可以 ...

  6. 深度优先遍历 and 广度优先遍历

    深度优先遍历 and 广度优先遍历 遍历在前端的应用场景不多,多数是处理DOM节点数或者 深拷贝.下面笔者以深拷贝为例,简单说明一些这两种遍历.

  7. ipv6现状,加英文的中括号访问, ipv6测试http://test-ipv6.com

    加英文的中括号就可以,如[2001:4998:c:e33::1004],我发现这是yahoo首页.但并不是所有IPv6网站都可以通过IPv6地址访问,跟IPv4一样,网站服务器端可以只绑定域名,不接受 ...

  8. ZeroNet搭建个人网站,一些搞笑图片

    ZeroNet是一个利用比特币加密和BT技术提供不受审查的网络与通信的BT平台,ZeroNet网络功能已经得到完整的种子的支持和加密连接,保证用户通信和文件共享的安全.使用ZeroNet,你可以匿名上 ...

  9. PHP判断类型

    is_bool();//判断是否为布尔型   is_float(); //判断是否为浮点型   is_int(); //判断是否为整型   is_numeric(); //判断是否为数值型   is_ ...

  10. 给js加版本号解决浏览器缓存问题

    我们知道打开网页时浏览器会加载需要的资源,比如图片.音频.js文件.css文件等.并且会把这些资源作为缓存保存下来,再次打开网页时缓存好的资源就不需要再次加载了.但是,这样有一个问题,就是当程序猿更新 ...