1. 程式人生 > >P3469 [POI2008]BLO-Blockade

P3469 [POI2008]BLO-Blockade

left per span 描述 ups int war wing 操作

P3469 [POI2008]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\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: nn and mm (1\le n\le 100\ 0001n100 000, 1\le m\le 500\ 0001m500 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\le a<b\le n1a<bn) 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 i^{th}ith line should contain the number of visits that could not take place if the programmers blocked the town no. ii.

輸入輸出樣例

輸入樣例#1: 復制
5 5
1 2
2 3
1 3
3 4
4 5
輸出樣例#1: 復制
8
8
16
14
8

/*
     思路就是求出是否為割點,如果是就算出這個割點無法聯通的點的數量ans[i] 
     最後答案即為(ans[i]+n-1)*2
*/
#include <bits/stdc++.h>

using namespace std;

const int maxn = 100006;

int dfn[maxn],low[maxn],n,m,cnt,sz[maxn];
long long ans[maxn];

vector<int> G[maxn];//vector領接表存圖

void tarjan(int u){
    long long z = 0;sz[u] = 1;//註意這裏要置初值,否則答案不對
    dfn[u] = low[u] = ++cnt;
    for(int i = 0;i < G[u].size();i++){
        int v = G[u][i];
        if(!dfn[v]){
            tarjan(v);
            low[u] = min(low[u],low[v]);
            sz[u] += sz[v];
            if(low[v] >= dfn[u]){//如果滿足子樹的low>=父節點的dfn 說明父節點為割點
                ans[u] += 1ll * z * sz[v];//現在割去的部分和以前割去的部分都不能聯通
                z += sz[v];//更新割去的點的數目
            }
        }else{
            low[u] = min(low[u],dfn[v]);
        }//常規的求割點操作 只不過不要考慮父節點啥的
    }
    ans[u] += 1ll * (n-1-z) * z;//現在割去的部分和沒有割去的部分不能聯通 n-1-z求的是除了u和割去的子樹外的其他聯通點
}

int main(){
    int x,y;
    scanf("%d%d",&n,&m);
    for(register int i = 1;i <= m;i++){
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);//雙向邊連圖
    }
    for(register int i = 1;i <= n;i++){
        if(!dfn[i]) tarjan(i);
    }
    for(register int i = 1;i <= n;i++){
        printf("%lld\n",(ans[i]+n-1)<<1);//位運算加快計算速度
    }//這裏ans數組求的是因為割點無法互相到達的點的數目,還要加上本身連接的n-1個點
    return 0;
}

P3469 [POI2008]BLO-Blockade