UVA 315【求割点数目】

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251

题意:求割点数目

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#pragma comment (linker, "/STACK:102400000,102400000")

using namespace std;

int n, m, x, y;

const int MAXN = 10010;
const int MAXM = 100010;
struct Edge
{
    int to, next;
    bool cut;//是否为桥的标记
}edge[MAXM];
int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN];
int Index, top;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];//删除一个点后增加的连通块
int bridge;

void addedge(int u, int v)
{
    edge[tot].to = v; edge[tot].next = head[u]; edge[tot].cut = false;
    head[u] = tot++;
}

void Tarjan(int u, int pre)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    int son = 0;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        if (v == pre)continue;
        if (!DFN[v])
        {
            son++;
            Tarjan(v, u);
            if (Low[u] > Low[v])Low[u] = Low[v];
            //桥
            //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。
            if (Low[v] > DFN[u])
            {
                bridge++;
                edge[i].cut = true;
                edge[i ^ 1].cut = true;
            }
            //割点
            //一个顶点u是割点,当且仅当满足(1)或(2) (1) u为树根,且u有多于一个子树。
            //(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边,
            //即u为v在搜索树中的父亲),使得DFS(u)<=Low(v)
            if (u != pre && Low[v] >= DFN[u])//不是树根
            {
                cut[u] = true;
                add_block[u]++;
            }
        }
        else if (Low[u] > DFN[v])
            Low[u] = DFN[v];
    }
    //树根,分支数大于1
    if (u == pre && son > 1)cut[u] = true;
    if (u == pre)add_block[u] = son - 1;
    Instack[u] = false;
    top--;
}

void solve(int N)
{
    memset(DFN, 0, sizeof(DFN));
    memset(Instack, false, sizeof(Instack));
    memset(add_block, 0, sizeof(add_block));
    memset(cut, false, sizeof(cut));
    Index = top = 0;
    bridge = 0;
    for (int i = 1; i <= N; i++)
        if (!DFN[i])
            Tarjan(i, i);
    int ans = 0;
    for (int i = 1; i <= N; i++)
        if (cut[i])
            ans++;
    printf("%d\n", ans);
}

void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
}

int main()
{
    while (~scanf("%d", &n) && n)
    {
        init();
        while (scanf("%d", &m) && m)
        {
            while (getchar() != ‘\n‘)
            {
                scanf("%d", &x);
                addedge(m, x);
                addedge(x, m);
            }
        }
        solve(n);
    }
    return 0;
}

版权声明:转载请注明出处。

时间: 2024-12-08 20:34:31

UVA 315【求割点数目】的相关文章

UVA 315 求割点数

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251 测模版: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #includ

POJ 1144 &amp; Uva 315 Network 【求割点数目】

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10855   Accepted: 5020 链接:http://poj.org/problem?id=1144 Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places

Uva 315 求无向图的割点的个数

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to 

UVA 315 求连通图里的割点

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20837 哎 大白书里求割点的模板不好用啊,许多细节理解起来也好烦..还好找了另一份模板 请注意,这道题里的每组数据都是只有一组连通图的 #include <iostream> #include <cstdio> #include <vector> #include <cstring> using namespace std; const

uva315(求割点数目)

传送门:Network 题意:给出一张无向图,求割点的个数. 分析:模板裸题,直接上模板. #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include <

uva 315 Network(连通图求割点)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251  Network  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers

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(tarjan求割点的个数)

题目链接:https://vjudge.net/contest/67418#problem/B 题意:给一个无向连通图,求出割点的数量.首先输入一个N(多实例,0结束),下面有不超过N行的数,每行的第一个数字代表后面的都和它存在边,0表示行输入的结束. 题解:简单的求割点模版,所谓割点就是去掉这一个点还有于这个点链接的边之后使得原来的图连通块增加. 由于这是模版题代码会加上注释. #include <iostream> #include <cstring> using namesp

UVA - 315 Network 和 UVA - 10199 (求割顶)

链接 :  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20837 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21278 求割顶的裸题. UVA - 315 #include <algorithm> #include <iostream> #include <sstream> #include <cstrin