#洛谷 P3469 [POI2008]BLO-Blockade (tarjan)

展开

题目描述

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.

题意翻译

题目描述

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

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

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

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

编写一个程序:

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

输入输出格式

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

城镇编号1~n

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

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

翻译提供者:Park

输入输出样例

输入 #1复制

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

题目大意 : 输入一张N个点, M条边的无向图,保证图连通, N行输出,表示删除该点后会使多少个点对无法互相到达

思路 :按照求割点的思路, 当一个点 V 可以经过的最小点都无法比当前的点 U 还小, 那么当前点 U 就是一个割点, 点 V 往后的点也就是一个连通块, 不难看出, 该题需要求出某个点的所有连通块的数量的乘积, 加上自身的连通块内的情况, 所以直接tarjan, 满足割点的条件后, 该点的值加上 子树的点数乘以 剩余的点数 , 当所有子树遍历完之后, 剩下的点就是和当前点 U 形成连通块的点, 对于U点需要特判, 因为该点无法到达任意一点, 最后开个long long, 就没问题了(不开的话WA两个点)

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct Edge
{
	ll v, next;
}e[MAXN << 1];
ll head[MAXN], n, m, cnt;
ll dfn[MAXN], low[MAXN], num[MAXN], tot;
ll ans[MAXN];
void add(ll from, ll to) {
	e[++cnt].v = to;
	e[cnt].next = head[from];
	head[from] = cnt;
}
void tarjan(ll x, ll fa) {
	dfn[x] = low[x] = ++tot;
	num[x] = 1; ll sum = 0; 
	for (ll i = head[x]; i != -1; i = e[i].next) {
		ll vi = e[i].v;
		if (!dfn[vi]) {
			tarjan(vi, x);
			Min(low[x], low[vi]);
			num[x] += num[vi];  // 孩子数量
			if (low[vi] >= dfn[x]) ans[x] += num[vi] * (n - num[vi]), sum += num[vi];
		}
		else Min(low[x], dfn[vi]);
	}
	ans[x] += (n - sum - 1) * (sum + 1) + n - 1;// 剩下的连通块数量(自身单独判)
}

int main()
{
	cin >> n >> m; MEM(head, -1);
	for (ll i = 0; i < m; i++) {
		ll ui, vi;
		sc("%lld %lld", &ui, &vi);
		add(ui, vi); add(vi, ui);
	}
	for (ll i = 1; i <= n; i++) {
		if (!dfn[i]) tarjan(i, i);
	}
	for (ll i = 1; i <= n; i++) printf("%lld\n", ans[i]);
	return 0;
}

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值