POJ 1144 无向图求割点

学长写的:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define maxn 10005
int dfn[maxn];///代表最先遍历到这个点的时间
int low[maxn];///这个点所能到达之前最早的时间点
int Father[maxn];///保存这个节点的父亲节点
int n, m, Time, top;///Time 时间点,  top用于栈操作
vector<vector<int> > G;

void Init()
{
    G.clear();
    G.resize(n+1);
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(Father, 0, sizeof(Father));
    Time = 0;
}

void Tarjan(int u,int fa)
{
    low[u] = dfn[u] = ++Time;
    Father[u] = fa;
    int len = G[u].size(), v;

for(int i=0; i<len; i++)
    {
        v = G[u][i];

if(!dfn[v])
        {
            Tarjan(v, u);
            low[u] = min(low[u], low[v]);
        }
        else if(fa != v)///假如我们在这里写上了 low[u] = min(low[v], low[u]),那么就相当于我们由v回到了v之前的节点
            low[u] = min(dfn[v], low[u]);
    }
}
void solve()
{/**
求割点
一个顶点u是割点,当且仅当满足(1)或(2)
(1) u为树根,且u有多于一个子树。
(2) u不为树根,且满足存在(u,v)为树枝边(或称 父子边,即u为v在搜索树中的父亲),使得 dfn(u)<=low(v)。
(也就是说 V 没办法绕过 u 点到达比 u dfn要小的点)
注:这里所说的树是指,DFS下的搜索树*/
    int RootSon = 0, ans = 0;///根节点儿子的数量
    bool Cut[maxn] = {false};///标记数组,判断这个点是否是割点

Tarjan(1,0);

for(int i=2; i<=n; i++)
    {
        int v = Father[i];
        if(v == 1)///也是就说 i的父亲是根节点
            RootSon ++;
        else if(dfn[v] <= low[i])
            Cut[v] = true;
    }

for(int i=2; i<=n; i++)
    {
        if(Cut[i])
            ans ++;
    }
    if(RootSon > 1)
        ans++;

printf("%d\n", ans);
}
int main()
{
    while(scanf("%d", &n), n)
    {
        int a, b;
        char ch;
        Init();
        while(scanf("%d", &a), a)
        {
            while(scanf("%d%c",&b,&ch))
            {
                G[a].push_back(b);
                G[b].push_back(a);
                if(ch == ‘\n‘)
                    break;
            }
        }
        solve();
    }
    return 0;
}

自己又写的

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
#define N 110
#define min(a, b) a<b?a:b
int n, father[N];
int visit[N], rode[N];
int rootson, ans, t, inter[N];
vector<vector<int> >G;
void Init()
{
    G.clear();
    G.resize(n+1);
    rootson=0;
    ans=0;
    t=0;
    memset(inter, 0, sizeof(inter));
    memset(visit, 0, sizeof(visit));
    memset(rode, 0, sizeof(rode));
    memset(father, 0, sizeof(father));
}
void Tarjan(int u, int fu)
{
    visit[u]=rode[u]=++t;
    father[u]=fu;
    int len=G[u].size();

    for(int i=0; i<len; i++)
    {
        int v=G[u][i];
        if(!visit[v])
        {
            Tarjan(v, u);
            rode[u]=min(rode[u], rode[v]);
        }
        else if(v!=fu)
        {
            rode[u]=min(visit[v], rode[u]);
        }
    }
}
void solve()
{
    Tarjan(1, 0);
    for(int i=2; i<=n; i++)
    {
        int v=father[i];
        if(v==1)
            rootson++;
        else if(visit[v]<=rode[i])//这一点可能不理解吧
            inter[v]=1;
    }
    for(int i=2; i<=n; i++)
    {
        if(inter[i])
            ans++;
    }
    if(rootson>1)
        ans++;
    printf("%d\n", ans);
}
int main()
{
    while(scanf("%d", &n), n)
    {
        Init();
        int a, b;
        char ch;
        while(scanf("%d", &a), a)
        {
            while(scanf("%d%c", &b, &ch)!=EOF)
            {
                G[a].push_back(b);
                G[b].push_back(a);
                if(ch==‘\n‘)
                    break;
            }
        }
        solve();
    }
    return 0;
}
时间: 2024-11-04 20:29:35

POJ 1144 无向图求割点的相关文章

POJ 1144 无向图的割点

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10207   Accepted: 4744 Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N

UVA 315 Network(无向图求割点)

题目大意 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two pl

poj 1144 Network 无向图求割点

Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect

POJ 1144 无向图割点模版题

点击打开链接 题意:给出一个无向图及边的关系,求割点个数 思路:无向图割点纯模版,不多说,这篇写的很好理解点这里 #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; c

(连通图 模板题 无向图求割点)Network --UVA--315(POJ--1144)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251 http://poj.org/problem?id=1144 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#problem/B 代码: #include<cstdio> #include<

无向图求割点 UVA 315 Network

输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cmath> #include <stack> #include <cstring> using namespa

POJ 1523 SPF 求割点的好(板子)题!

题意: 给个无向图,问有多少个割点,对于每个割点求删除这个点之后会产生多少新的点双联通分量 题还是很果的 怎么求割点请参考tarjan无向图 关于能产生几个新的双联通分量,对于每个节点u来说,我们判断他是否是割点,即判断是否满足他的儿子v的low[v]>dfn[u] 而这个时候割掉这个点就会让双联通分量增加,所以搞一个数组记录一下这个操作的次数就行 请注意在是否是根节点的问题上特判 !!注意输出格式!! 1 #include<cstdio> 2 #include<algorithm

B - Network---UVA 315(无向图求割点)

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two places

poj 2117 Electricity 【无向图求割点】【求去掉一个点后 图中最多的BCC数目】

Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4597   Accepted: 1515 Description Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them sup