【Loj #10104. 「一本通 3.6 练习 5」Blockade】题解


题目链接

首先这个点删去之后必然与剩下 \(n-1\) 个点失去相连。

如果这个点能使其它点失去相连,说明这个点为割点。

然后统计一下每个儿子与父亲的影响即可。

Code

// Problem: #10104. 「一本通 3.6 练习 5」Blockade
// Contest: LibreOJ
// URL: https://loj.ac/p/10104
// Memory Limit: 162 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)
 
#include
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define M 500010
//#define mo
#define N 100010
struct node
{
    int x, y, n; 
}d[M*2]; 
int n, m, i, j, k, u, v; 
int h[N], dep[N], f[N], zi[N], s[N], ans[N]; 
 
void cun(int x, int y)
{
    d[++k].x=x; d[k].y=y; 
    d[k].n=h[x]; h[x]=k; 
}
 
void dfs(int x)
{
    int sum=0; 
    s[x]=1; 
    for(int g=h[x]; g; g=d[g].n)
    {
        int y=d[g].y; 
        if(!dep[y])
        {
            dep[y]=dep[x]+1; 
            dfs(y); 
            s[x]+=s[y]; 
            if(dep[f[y]]>=dep[x]) 
            {
                ++zi[x]; 
                ans[x]+=sum*s[y]; 
                sum+=s[y]; 
            }
            if(dep[f[y]]=2)) printf("%lld\n", ans[i]*2); 
        else printf("%lld\n", 2*(n-1)); 
    }
    return 0; 
}