首页 > 其他分享 >Luogu P3469 [POI2008]BLO-Blockade(tarjan求割点)

Luogu P3469 [POI2008]BLO-Blockade(tarjan求割点)

时间:2022-10-03 17:33:28浏览次数:85  
标签:town tarjan le POI2008 求割点 int son low include

题目链接:https://www.luogu.com.cn/problem/P3469

 

[POI2008]BLO-Blockade

题面翻译

B 城有 $n$ 个城镇,$m$ 条双向道路。

每条道路连结两个不同的城镇,没有重复的道路,所有城镇连通。

把城镇看作节点,把道路看作边,容易发现,整个城市构成了一个无向图。

请你对于每个节点 $i$ 求出,把与节点 $i$ 关联的所有边去掉以后(不去掉节点 $i$ 本身),无向图有多少个有序点 $(x,y)$,满足 $x$ 和 $y$ 不连通。

【输入格式】

第一行包含两个整数 $n$ 和 $m$。

接下来 $m$ 行,每行包含两个整数 $a$ 和 $b$,表示城镇 $a$ 和 $b$ 之间存在一条道路。

【输出格式】

输出共 $n$ 行,每行输出一个整数。

第 $i$ 行输出的整数表示把与节点 $i$ 关联的所有边去掉以后(不去掉节点 $i$ 本身),无向图有多少个有序点 $(x,y)$,满足 $x$ 和 $y$ 不连通。

【数据范围】

$n\le 100000$,$m\le500000$

题目描述

There are exactly $n$ 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)$ 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 100\ 000$, $1\le m\le 500\ 000$) denoting the number of towns and roads, respectively.

The towns are numbered from 1 to $n$.

The following $m$ 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 $n$ integers to the standard output, one number per line. The $i^{th}$ line should contain the number of visits that could not take place if the programmers blocked the town no. $i$.

样例 #1

样例输入 #1

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

样例输出 #1

8
8
16
14
8

代码:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<vector>
  4 #include<map>
  5 #include<queue>
  6 #include<set>
  7 #include<algorithm>
  8 #include<stack>
  9 #include<cmath>
 10 #include<cstring>
 11 #include<string>
 12 using namespace std;
 13 #define int long long
 14 #define gc getchar()
 15 #define rd(x) read(x)
 16 #define el '\n'
 17 #define rep(i, a, n) for(int i = (a); i <= n; ++i)
 18 #define per(i, a, n) for(int i = (a); i >= n; --i)
 19 using ll = long long;
 20 using db = double;
 21 using ldb = long double;
 22 const int mod = 1e9 + 7;
 23 const int inf = 0x3f3f3f3f;
 24 const int N = 1e5 + 10;
 25 
 26 template <typename _T>
 27 inline void read(_T & f) {
 28     f = 0; _T fu = 1; char c = gc;
 29     while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = gc; }
 30     while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = gc; }
 31     f *= fu;
 32 }
 33 
 34 template <typename T>
 35 void print(T x) {
 36     if (x < 0) putchar('-'), x = -x;
 37     if (x < 10) putchar(x + 48);
 38     else print(x / 10), putchar(x % 10 + 48);
 39 }
 40 
 41 template <typename T>
 42 void print(T x, char t) {
 43     print(x); putchar(t);
 44 }
 45 
 46 struct node {
 47     int to, next;
 48 }e[N << 4];
 49 
 50 int head[N << 4], tot;
 51 
 52 void add(int u,int v) {
 53     e[tot].to = v;
 54     e[tot].next = head[u];
 55     head[u] = tot++;
 56 }
 57 
 58 int n, m;
 59 
 60 int vis[N], dfn[N], low[N], cnt, flag[N];
 61 int res = 0;
 62 vector<int>son[N];
 63 int sontree[N];
 64 
 65 void tarjan(int u,int fa) {
 66     sontree[u] = 1;
 67     vis[u] = 1;
 68     dfn[u] = low[u] = ++cnt;
 69     int child = 0;
 70     int sum = 0;
 71     for(int i = head[u]; i + 1; i = e[i].next) {
 72         int v = e[i].to;
 73         if(!vis[v]) {
 74             child++;
 75             tarjan(v, u);
 76             sontree[u] += sontree[v];
 77             low[u] = min(low[u], low[v]);
 78             if(low[v] >= dfn[u]) {
 79                 sum += sontree[v];
 80                 son[u].push_back(sontree[v]);
 81                 if(u != fa) flag[u] = 1;
 82             }
 83         }else if(v != fa) {
 84             low[u] = min(low[u], dfn[v]);
 85         }
 86     }
 87     if(u == fa && child >= 2 && !flag[u]) {
 88         flag[u] = 1;
 89     }
 90     if(flag[u]) {
 91         if(n - sum - 1 != 0) son[u].push_back(n - sum - 1);
 92     }
 93 
 94     return;
 95 }
 96 
 97 signed main() {
 98 
 99     //int n, m;
100     cin >> n >> m;
101     memset(head, -1, sizeof(head));
102     for(int i = 1; i <= m; i++) {
103         int u, v;
104         cin >> u >> v;
105         add(u, v),add(v, u);
106     }
107     tarjan(1, 1);
108     for(int i = 1; i <= n; i++) {
109         int ans = 2 * (n - 1);
110         if(flag[i] && son[i].size() > 1) {
111             for(int j = 0; j < son[i].size(); j++) {
112                 for(int k = j + 1; k < son[i].size(); k++) {
113                     ans += 2 * (son[i][j] * son[i][k]);
114                 }
115             }
116         }
117         cout << ans << el;
118     }
119 
120     return 0;
121 }

 

   

标签:town,tarjan,le,POI2008,求割点,int,son,low,include
From: https://www.cnblogs.com/wabi/p/16750826.html

相关文章

  • Tarjan
    P3387【模板】缩点(强连通分量+拓扑+dp)#include<iostream>#include<queue>#include<cmath>#definefor1(i,a,b)for(inti=a;i<=b;i++)#definemp(a,b)make_pai......
  • Tarjan问题
    强连通学习资料强连通,又名\(scc\),即有向图中可以相互到达的子图,如\(\quad\)3->4;4->33与4即一对\(scc\);\(Tarjan\)的作用可以将有环图转为有向无环图既然是有向无......
  • 洛谷P1262 间谍网络(tarjan求强连通分量+缩点)
    题目链接:https://www.luogu.com.cn/problem/P1262思路:首先,我们能够知道,入读为0的点如果不能被收买的话,那么此题是无解的。其次,如果图中存在环的话,那么环中每个点的......
  • 洛谷P2002 消息扩散(tarjan缩点)
    题目链接:https://www.luogu.com.cn/problem/P2002思路:由于图中每个强连通分量(scc)中的点是可以互相到达的,所以我们可以用tarjan求图中scc,然后将所有scc缩点,最后求缩点之......
  • tarjan算法求强连通分量
    \(tarjan\)算法求强连通分量\(tarjan\)算法简介我在这篇博客中讲过\(tarjan\)算法的简介和求割点与桥,就不再讲述。强连通分量强连通图是指一个有向图内任意两点都能互......
  • tarjan
    changelog:新建此随笔,还有一些东西未完工。https://www.youtube.com/watch?v=TyWtx7q2D7Y有向图的DFS生成树主要有4种边(不一定全部出现):树边(treeedge):示意图中以黑色......
  • 解题报告——P3477 [POI2008]PER-Permutation
    这道题如果不是任意模数的话还是比较平凡的(这道题的式子其实很好推,根据康托展开的思路,一位一位考虑,只不过是多重集,可能有重复情况,排除即可,每一位的式子为:\[ans_i=\dfrac{......