[POI2008]BLO-Blockade 题解

[POI2008]BLO-Blockade 题解

题目地址:洛谷:【P3469】[POI2008]BLO-Blockade – 洛谷、BZOJ:Problem 1123. — [POI2008]BLO

题目描述

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·(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≤n≤100 000, 1≤m≤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≤a<b≤n) and denotes a direct road between towns numbered a and b .

输出格式:
Your programme should write out exactly nnn integers to the standard output, one number per line. The ith line 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

题解

考虑一个割点所在的多个点双连通分量,割点割去后它们互不可达,因此可以统计答案。注意答案要乘以2,由于统计的是有序点对。

代码

// Code by KSkun, 2018/5
#include <cstdio>
#include <cctype>
#include <cstring>

#include <algorithm>
#include <vector>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF 
        : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    register char c = fgc();
    while(!isdigit(c)) {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(isdigit(c)) {
        res = (res << 1) + (res << 3) + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 100005;

int n, m;

std::vector<int> gra[MAXN];

int dfn[MAXN], low[MAXN], siz[MAXN], clk;
LL ans[MAXN];

inline void tarjan(int u) {
    siz[u] = 1;
    dfn[u] = low[u] = ++clk;
    ans[u] += n - 1;
    int sum = 0;
    for(int i = 0; i < gra[u].size(); i++) {
        int v = gra[u][i];
        if(!dfn[v]) {
            tarjan(v);
            low[u] = std::min(low[u], low[v]);
            siz[u] += siz[v];
            if(low[v] >= dfn[u]) {
                ans[u] += 1ll * siz[v] * sum;
                sum += siz[v];
            }
        } else {
            low[u] = std::min(low[u], dfn[v]);
        }
    }
    ans[u] += 1ll * sum * (n - sum - 1);
}

int u, v;

int main() {
    n = readint(); m = readint();
    for(int i = 1; i <= m; i++) {
        u = readint(); v = readint();
        gra[u].push_back(v);
        gra[v].push_back(u);
    }
    for(int i = 1; i <= n; i++) {
        if(!dfn[i]) tarjan(i);
    }
    for(int i = 1; i <= n; i++) {
        printf("%lld\n", ans[i] * 2);
    }
    return 0;
}


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据