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,qua,root,iscut[maxn];
 9 struct edge
10 {
11     int u,v,next;
12 }e[maxn*maxn];
13 void add_edge(int x,int y)
14 {
15     e[cnt].u=x;
16     e[cnt].v=y;
17     e[cnt].next=head[x];
18     head[x]=cnt++;
19 }
20 void tarjan(int x)
21 {
22     dfn[x]=low[x]=++num;
23     int flag=0;
24     for(int i=head[x];i!=-1;i=e[i].next)
25     {
26         int  to=e[i].v;
27         if(!dfn[to])
28         {
29             tarjan(to);
30             low[x]=min(low[x],low[to]);
31             if(low[to]>=dfn[x])
32             {
33                 flag++;
34                 if(x!=root || flag>1) iscut[x]=1,qua++;
35             }  //一个割点可能会多次经历iscut[x]=1,所以qua里面的值不是割点数目
36         }
37         else low[x]=min(dfn[to],low[x]);
38     }
39 }
40 int main()
41 {
42     while(~scanf("%d",&n) && n)
43     {
44         cnt=num=qua=0;
45         memset(iscut,0,sizeof(iscut));
46         memset(head,-1,sizeof(head));
47         memset(dfn,0,sizeof(dfn));
48         memset(low,0,sizeof(low));
49         int x,y;
50         char ch;
51         while(~scanf("%d",&x) && x)
52         {
53             while(~scanf("%d",&y))
54             {
55                 scanf("%c",&ch);
56                 add_edge(x,y);
57                 add_edge(y,x);
58                 if(ch==‘\n‘){
59                     break;
60                 }
61             }
62         }
63 //        for(int i=0;i<cnt;++i)
64 //        {
65 //            printf("%d %d\n",e[i].u,e[i].v);
66 //
67 //        }
68         for(int i=1;i<=n;++i)
69         {
70             if(!dfn[i])
71             {
72                 //printf("**\n");
73                 root=i;
74                 tarjan(i);
75             }
76         }
77         int ans=0;
78         for(int i=1;i<=n;++i)
79             if(iscut[i]) ++ans;
80         printf("%d\n",ans);
81     }
82     return 0;
83 }

原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/11665109.html

时间: 2024-10-02 21:21:33

Network UVA - 315 无向图找割点的相关文章

B - Network - uva 315(求割点)

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

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> #includ

SSummerZzz kuangbin专题 专题九 连通图 Network UVA - 315

题目链接:https://vjudge.net/problem/POJ-1236 题目:有向图,有若干个连通图,点之间有单向边边就可以单向传递信息,问: (1)至少需要发送几份信息才能使得每个点都传递到信息 (2)至少需要加几条边,才能使得“把一份信息发送到任意某个点就能传播到其他所有点”成立 思路:tarjan求强连通分量,强联通分量可以相互传递消息,然后,按强联通编号重构图, 统计每个强联通分量的入度出度情况.第一个答案,就显然是入度为0的点,第二个就是max(p,q),构成一个强联通图 这

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 求无向图的割点的个数

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 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

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

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

求无向图的割点和桥

/** * 求 无向图的割点和桥 * 可以找出割点和桥,求删掉每个点后增加的连通块. * 需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重 * 调用solve输出割点数,全局变量bridge记录边的个数 */ #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; const int maxn=1001