Los solution to a problem Valley P3469

Each topic will lead to much cut point-to-point connectivity can not be removed after

Consider running Tarjan time record of each son's size, then get rid of this and other points can not communicate with his son after the cut point

Note that each point itself can not communicate with all other points after removing the

There is a topic where demand is ordered points, so the program should total number \ (× 2 \)

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>
#define il inline
#define re register
#define gc getchar
#define LL long long
#define int LL
#define D() \
// cerr << __LINE__ << endl
using namespace std;
template <typename T>
void read(T &s)
{
    s = 0;
    char ch;
    while (ch = gc(), !isdigit(ch))
        ;
    while (s = s * 10 + ch - '0', ch = gc(), isdigit(ch))
        ;
}
const int MAXN = 600000;
int cnt = 0;
int vis[MAXN];
int tot = 0;
int del[MAXN];
int dfn[MAXN], low[MAXN];
int sta[MAXN], top;
int ans[MAXN];
int sze[MAXN];
vector<int> edge[MAXN];
vector<int> edge2[MAXN];
il void insert(int u, int v)
{
    edge[u].push_back(v);
    edge[v].push_back(u);
}
il void insert2(int u, int v)
{
    edge2[u].push_back(v);
}
int n;
void tarjan(int u, int fa)
{
    int t = 0;
    int child = 0;
    vis[u] = 1;
    dfn[u] = low[u] = ++tot;
    sta[++top] = u;
    sze[u] = 1;
    for (auto v : edge[u])
        if (!dfn[v])
        {
            tarjan(v, u);
            sze[u] += sze[v];
            ++child;
            if ((fa == -1 && child > 1) || (fa != -1 && low[v] >= dfn[u]))
            {
                ans[u] += sze[v] * t;
                t += sze[v];
            }
            low[u] = min(low[u], low[v]);
        }
        else if (vis[v])
            low[u] = min(low[u], dfn[v]);
    ans[u] += t * (n - t - 1);
    if (low[u] == dfn[u])
    {
        while (sta[top] != u)
        {
            int y = sta[top--];
            vis[y] = 0;
        }
        top--;
        vis[u] = 0;
    }
}
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int m, a, b;
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        cin >> a >> b;
        insert(a, b);
    }
    for (int i = 1; i <= n; ++i)
        if (!dfn[i])
            tarjan(i, -1);
    for (int i = 1; i <= n; ++i)
        cout << (((n - 1 + ans[i]) << 1)) << endl;
}

Guess you like

Origin www.cnblogs.com/happyLittleRabbit/p/11580228.html