UVA315- Network(无向图割点)

题目链接

题意: 给出一张无向图,求割点的个数

思路:非常裸的题目。直接套用模版就可以。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 1005;

struct Edge{
    int to, next;
    bool cut;
}edge[MAXN * 10];
int head[MAXN], tot;
int Low[MAXN], DFN[MAXN];
int Index, cnt;
bool cut[MAXN];

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;
    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];
            if (u != pre && Low[v] >= DFN[u]) {
                cut[u] = true;
            }
        }
        else if (Low[u] > DFN[v])
            Low[u] = DFN[v];
    }
    if (u == pre && son > 1) cut[u] = true;
}

void init() {
    memset(head, -1, sizeof(head));
    memset(DFN, 0, sizeof(DFN));
    memset(cut, false, sizeof(cut));
    tot = 0;
    Index = cnt = 0;
}

int main() {
    int n;
    while (scanf("%d", &n) && n) {
        init();
        int u, v;
        while (scanf("%d", &u) && u) {
            char ch;
            while (scanf("%d%c", &v, &ch)) {
                addedge(u, v);
                addedge(v, u);
                if (ch == '\n') break;
            }
        }

        for (int i = 1; i <= n; i++)
            if (!DFN[i])
                Tarjan(i, i);
        for (int i = 1; i <= n; i++)
            if (cut[i])
                cnt++;
        printf("%d\n", cnt);
    }
    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-10-12 08:11:58

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

UVA315 Network 连通图割点

题目大意:有向图求割点 题目思路: 一个点u为割点时当且仅当满足两个两个条件之一: 1.该点为根节点且至少有两个子节点 2.u不为树根,且满足存在(u,v)为树枝边(或称 父子边,即u为v在搜索树中的父亲),使得 dfn(u)<=low(v). 然后注意读入,很容易RE #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<vector&

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

tarkjan求无向图割点模板

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 int n,m; 6 const int maxn=1e5+2; 7 const int maxm=maxn<<1; 8 struct node 9 { 10 int to; 11 int nxt; 12 }e[maxm]; 13 int head[maxn]; 14 int tot; 15 int id; 16 int root

UVA315 (无向图求割点)

题目大意:给定一个无向图,问共存在多少个割点.(割点:去掉此点后此图会断开连接)割点有两种存在:一种是第一次搜索的根节点,若其子节点数超过两个,则此点去掉后图会 断开连接,因此此点为割点:或者此点为搜索树的子节点,若其子节点的最早达到时间状态比其自身要晚,则说明此点不得不经过,并以此来更新其子节点,故其也是割点. 详见代码. #include <stdio.h> #include <algorithm> #include <vector> #include <st

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

Network -UVa315(连通图求割点)

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

[UVA315]Network(tarjan, 求割点)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251 求割点,除了输入用strtok和sscanf处理输入以外,对于求割点的tarjan算法有了进一步理解. 特别注意88行,如果u是根并且至少两个儿子,那它一定是割点无误,还有第二个情况用如图代表: 这个例子里显然:low[4]=2,dfn[4]=4,dfn[3]=3.现dfs到

POJ 1144 Network(无向图连通分量求割点)

题目地址:POJ 1144 求割点.推断一个点是否是割点有两种推断情况: 假设u为割点,当且仅当满足以下的1条 1.假设u为树根,那么u必须有多于1棵子树 2.假设u不为树根.那么(u,v)为树枝边.当Low[v]>=DFN[u]时. 然后依据这两句来找割点就能够了. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include &

uva315(求割点数目)

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

POJ1523SPF[无向图割点]

SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8139   Accepted: 3723 Description Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a