(连通图 模板题 无向图求割点)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<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;
}
时间: 2024-07-31 20:41:44

(连通图 模板题 无向图求割点)Network --UVA--315(POJ--1144)的相关文章

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

无向图求割点 UVA 315 Network

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

B - Network - uva 315(求割点)

题意:给一个无向连通图,求出割点的数量. 首先输入一个N(多实例,0结束),下面有不超过N行的数,每行的第一个数字代表后面的都和它存在边,0表示行输入的结束(很蛋疼的输入方式). 分析:割点的模板题 ****************************************************************** #include<stdio.h>#include<string.h>#include<stack>#include<algorith

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

连通分量模板:tarjan: 求割点 &amp;&amp; 桥 &amp;&amp; 缩点 &amp;&amp; 强连通分量 &amp;&amp; 双连通分量 &amp;&amp; LCA(最近公共祖先)

PS:摘自一不知名的来自大神. 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合. 3.点连通度:最小割点集合中的顶点数. 4.割边(桥):删掉它之后,图必然会分裂为两个或两个以上的子图. 5.割边集合:如果有一个边集合,删除这个边集合以后,原图变成多个连通块,就称这个点集为割边集合. 6.边连通度:一个图的边连通度的定义为,最

poj1144Network 无向图求割点Tarjan

n个点,组成一个无向图,求这个图中割点的数量.模板题. 只是这道题在输入数据的时候有点麻烦,如样例中,第一组数据有五个点,5 1 2 3 4 表示5这个点与1 2 3 4 点相连.其中这个图的割点只是5这个点.第二组数据6个点,2 与1 3 5相连,5与2 4 6相连,其中2点与5点都是割点. 有两类节点可以成为割点: 对根节点u,若其有两棵或两棵以上的子树,则该根结点u为割点: 对非叶子节点u(非根节点),若其子树的节点均没有指向u的祖先节点的回边,说明删除u之后,根结点与u的子树的节点不再连

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 1144 无向图求割点

学长写的: #include<cstdio>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>using namespace std;#define maxn 10005int dfn[maxn];///代表最先遍历到这个点的时间int low[maxn];///这个点所

Network UVA - 315 无向图找割点

题意: 给你一个无向图,你需要找出来其中有几个割点 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int maxn=105; 8 int cnt,head[maxn],n,dfn[maxn],low[maxn],num,q