UVA315 (无向图求割点)

题目大意:给定一个无向图,问共存在多少个割点。(割点:去掉此点后此图会断开连接)割点有两种存在:一种是第一次搜索的根节点,若其子节点数超过两个,则此点去掉后图会

断开连接,因此此点为割点;或者此点为搜索树的子节点,若其子节点的最早达到时间状态比其自身要晚,则说明此点不得不经过,并以此来更新其子节点,故其也是割点。

详见代码。

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
#define N 10100
vector<vector<int> >G;
int n, dfn[N], low[N], Time, father[N], vis[N];
void Init()
{
    G.clear();
    G.resize(n+1);
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(father, 0, sizeof(father));
    memset(vis, 0, sizeof(vis));
    Time = 0;
}
void Trajin(int u, int fa)
{
    father[u] = fa;
    dfn[u] = low[u] = ++Time;
    int i, len = G[u].size(), v;
    for(i=0; i<len; i++)
    {
        v = G[u][i];
        if(!dfn[v])
        {
            Trajin(v, u);
            low[u] = min(low[u], low[v]);
        }
        else if(fa!=v)
            low[u] = min(low[u], dfn[v]);
    }
}
int main()
{
    while(scanf("%d", &n), n)
    {
        Init();
        int a, b;
        char x;
        while(scanf("%d", &a), a)
        {
            while(scanf("%d%c", &b, &x))
            {
                G[a].push_back(b);
                G[b].push_back(a);
                if(x==‘\n‘)break;
            }
        }
        Trajin(1, 0);
        int ans = 0, RootSon = 0;
        for(int i=2; i<=n; i++)
        {
            if(father[i]==1)RootSon++;
            else if(dfn[father[i]]<=low[i])
                vis[father[i]] = 1;
        }
        if(RootSon>1)ans++;
        for(int i=2; i<=n; i++)
            if(vis[i])ans++;
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-19 09:59:07

UVA315 (无向图求割点)的相关文章

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

(连通图 模板题 无向图求割点)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<

uva315(求割点数目)

传送门:Network 题意:给出一张无向图,求割点的个数. 分析:模板裸题,直接上模板. #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #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 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<cstdio>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>using namespace std;#define maxn 10005int dfn[maxn];///代表最先遍历到这个点的时间int low[maxn];///这个点所

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

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的子树的节点不再连