bzoj 1123 tarjan求割点

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int> >

using namespace std;

const int N = 1e5 + 10;
const int M = 10 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

int n, m, idx, dfn[N], low[N], cnt[N];
LL ans[N];
vector<int> edge[N];

void tarjan(int u) {
    int sum = 0;
    cnt[u] = 1;
    dfn[u] = low[u] = ++idx;
    for(int i = 0; i < edge[u].size(); i++) {
        int v = edge[u][i];
        if(dfn[v]) {
            low[u] = min(low[u], dfn[v]);
        } else {
            tarjan(v);
            cnt[u] += cnt[v];
            low[u] = min(low[u], low[v]);
            if(dfn[u] <= low[v]) {
                ans[u] += 1ll * sum * cnt[v];
                sum += cnt[v];
            }
        }
    }
    ans[u] += 1ll * sum * (n - sum - 1);
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        int u, v; scanf("%d%d", &u, &v);
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    for(int i = 1; i <= n; i++) {
        if(!dfn[i]) {
            tarjan(i);
        }
    }

    for(int i = 1; i <= n; i++) {
        ans[i] = ans[i] + n - 1 << 1;
        printf("%lld\n", ans[i]);
    }
    return 0;
}
/*
*/

 

posted @ 2018-06-14 17:22  NotNight  阅读(76)  评论(0编辑  收藏  举报