poj 1144 Network 图论基础 图的割点

题目

题目链接:http://poj.org/problem?id=1144

题目来源:学习割点最基础的题目。

简要题意:求割点个数。

数据范围: N<100

题解

可以参考我的另一篇文章:
http://blog.csdn.net/xc19952007/article/details/48349055

最裸的求割点的题目,求出割点直接扫一遍就行了。

实现

套上最基本的模板就行了,一个dfs解决人生问题。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 100+5;
const int M = 1e5+5;

struct Edge {
    int to, nxt;
    Edge(int to, int nxt) : to(to), nxt(nxt){}
    Edge() {}
};

int head[N];
int dep[N];
int low[N];
bool res[N];
Edge e[M];

void addEdge(int from, int to, int no) {
    e[no] = Edge(to, head[from]);
    head[from] = no;
}

void dfs(int x, int p, int d) {
    low[x] = dep[x] = d;
    int cnt = 0, nxt;
    for (int i = head[x]; ~i; i = e[i].nxt) {
        nxt = e[i].to;
        if (!dep[nxt]) {
            cnt++;
            dfs(nxt, x, d+1);
            low[x] = min(low[x], low[nxt]);
            res[x] |= x==1 ? cnt > 1 : dep[x] <= low[nxt];
        } else if (p != nxt) {
            low[x] = min(low[x], dep[nxt]);
        }
    }
}

int main() {
    int n, u, v;
    while (scanf("%d", &n) == 1 && n) {
        memset(head, -1, sizeof head);
        memset(res, 0, sizeof res);
        memset(low, 0, sizeof low);
        memset(dep, 0, sizeof dep);

        int cnt = 0;
        while (scanf("%d", &u) == 1 && u) {
            while (scanf("%d", &v) == 1) {
                addEdge(u, v, cnt++);
                addEdge(v, u, cnt++);
                if (getchar() == '\n') break;
            }
        }

        dfs(1, -1, 1);
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            if (res[i]) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值