POJ 1144

http://poj.org/problem?id=1144

题意:给你一些点,某些点直接有边,并且是无向边,求有多少个点是割点

割点:就是在图中,去掉一个点,无向图会构成多个子图,这就是割点

Tarjan算法求割点的办法

  1. 如果该点为根,那么它的子树必须要大于1
  2. 如果该点不为根,那么当low[v]>=dnf[u]时,为割点

Low[v]>=dnf[u]也就是说明U的子孙点只能通过U点访问U的祖先点

 1 #include <stdio.h>
 2 #include <stack>
 3 #include <string.h>
 4 #define maxn 505
 5
 6 using namespace std;
 7
 8 stack <int >s;
 9
10 int head[maxn],n,pos,dfn[maxn],low[maxn],bcnt,dindex,num[maxn],root;
11
12 bool vis[maxn];
13
14 struct node{
15     int next,to;
16 }edge[maxn];
17
18 void add(int u,int v)
19 {
20     edge[pos].to = v;
21     edge[pos].next = head[u];
22     head[u] = pos++;
23 }
24
25 void Tarjan(int u)
26 {
27     dfn[u] = low[u] = ++dindex;
28     vis[u] = true;
29     s.push(u);
30     for(int i = head[u]; i != -1 ; i = edge[i].next)
31     {
32         int v = edge[i].to;
33         if(!vis[v])
34         {
35             Tarjan(v);
36             if(low[v]<low[u]) low[u] = low[v];
37             if(low[v]>=dfn[u]&&u!=1)
38             {
39                 num[u]++;
40             }else if(u==1)
41                 root++;
42         }else if(dfn[v]<low[u])
43             low[u] = dfn[v];
44     }
45 }
46
47 int main()
48 {
49     int u,v,ans;
50   //  freopen("in.txt","r",stdin);
51     while(scanf("%d",&n),n)
52     {
53
54         memset(head,-1,sizeof(head));
55         memset(vis,false,sizeof(vis));
56         memset(dfn,0,sizeof(dfn));
57         memset(low,0,sizeof(low));
58         memset(num,0,sizeof(num));
59         pos = 1;
60         ans = 0;
61         while(scanf("%d",&u)&&u)
62         {
63             while(getchar()!=‘\n‘)
64             {
65                 scanf("%d",&v);
66                 add(u,v);
67                 add(v,u);
68             }
69         }
70         bcnt = dindex = root=0;
71         for(int i = 1;i<=n;i++)
72             if(!dfn[i]) Tarjan(i);
73         for(int i = 1 ; i<=n;i++)
74             if(num[i]) ans++;
75         if(root>1) ans++;
76         printf("%d\n",ans);
77     }
78     return 0;
79 }

https://www.byvoid.com/blog/scc-tarjan/一个很好的学习Tarjan的博客

时间: 2024-10-13 14:50:14

POJ 1144的相关文章

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 &

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 <

Poj 1144 Zoj 1311 求割点 模板

写这个就是为了手写一份好用的求割点模板: 吐槽下,题目中的 Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place.  这个at most是不可信的,应该是用大于n行的测试数据,因为这个我WA了... #include <cstdio> #includ

求无向图的割点 (poj 1144 Network)

割点 :去掉该点后原来的图不连通(出现好几个连通分量),该点被称为割点. 注意删除某点意味着和该点关联的边也全部删除 求割点的伪代码 DFS(v1,father): dfn[v1] = low[v1] = ++dfsClock vis[v1] = true child = 0 for each egde(v1,v2) in E: if(vis[v2] == false) : //(v1,v2)是父子边 DFS(v2,v1) child++ low[v1] = Min(low[v1],low[v2

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

poj 1144 Network(割点)

题目链接: http://poj.org/problem?id=1144 思路分析:该问题要求求出无向联通图中的割点数目,使用Tarjan算法即可求出无向联通图中的所有的割点,算法复杂度为O(|V| + |E|): 代码如下: #include <cstdio> #include <vector> #include <cstring> #include <iostream> using namespace std; const int MAX_N = 100

poj 1144 (Tarjan求割点数量)

题目链接:http://poj.org/problem?id=1144 描述 一个电话线公司(简称TLC)正在建立一个新的电话线缆网络.他们连接了若干个地点分别从1到N编号.没有两个地点有相同的号码.这些线是双向的并且能使两个地点保持通讯.每个地点的线都终结于电话交换机.每个地点都有一个电话交换机.从每个地点都能通过线缆到达其他任意的地点,然而它并不需要直接连接,它可以通过若干个交换机来到达目的地.有时候某个地点供电出问题时,交换机就会停止工作.TLC的工作人员意识到,除非这个地点是不可达的,否

poj 1144 Network【双连通分量求割点总数】

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11042   Accepted: 5100 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

图论(无向图的割顶):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