Lightoj 1063 - Ant Hills

题意:求最小割为1的割点的数量。

第一次做这样的题  ,不会做啊,,做了这道题我才发现我连最基本的图论都不会。这水平,太水了。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>

using namespace std;
const int N = 10009;
const int M = 20009;
struct LT {
    int nex,to;
} L[M<<1];
int F[N],cnt;
void add(int f,int t)
{
    L[cnt].to = t;
    L[cnt].nex = F[f];
    F[f] = cnt++;
}
int m,n;
int dfn[N],low[N],post[N],ind,cut[N];
void tarjan(int k,int pre)
{
    int child = 0;
    dfn[k] = low[k] = ind++;
    post[k] = 1;
    for(int i=F[k];i;i=L[i].nex)
    {
        int to = L[i].to;
        if(to==pre) continue;
        if(post[to]) low[k] = min(low[to],low[k]);
        else {
            child ++;
            tarjan(to,k);
            low[k] = min(low[k],low[to]);
            if(low[to]>=dfn[k]) cut[k]=1;
        }
    }
    if(pre==-1) cut[k] = child>1;
}
void solve()
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(cut,0,sizeof(cut));
    memset(post,0,sizeof(post));
    ind = 1;
    tarjan(1,-1);
    int ans = 0;
    for(int i=1;i<=n;i++) if(cut[i]) ans++;
    printf("%d\n",ans);
}
int main()
{
    freopen("in.txt","r",stdin);
    int cas,T=1;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d",&n,&m);
        int a,b;
        memset(F,0,sizeof(F));cnt = 1 ;
        for(int i=0;i<m;i++)
        scanf("%d%d",&a,&b),add(a,b),add(b,a);
        printf("Case %d: ",T++);
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值