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>

#include <cstring>

#include <algorithm>

#include <iostream>

using namespace std;

const int MAXN = 100+10;

int mat[MAXN][MAXN],son,subset[MAXN],dfn[MAXN],low[MAXN],vis[MAXN],tmpdfn;

//son,subset记录连通分量个数,

//如果是根节点,son==把根节点去掉后的连通分支个数,

//如果不是根节点,subset[i]+1==把该节点去掉后的连通分支个数

//所以根节点和其他节点区分开了

int n;

void init()

{

son=tmpdfn=0;

memset(vis,0,sizeof(vis));

memset(mat,0,sizeof(mat));

memset(subset,0,sizeof(subset));

memset(dfn,0,sizeof(dfn));

int u,v;

char ch;

while (scanf("%d",&u),u)

{

while(getchar()!=‘\n‘)

{

scanf("%d",&v);

mat[u][v]=mat[v][u]=1;

}

}

}

void dfs(int u)

{

dfn[u]=low[u]=tmpdfn++;

for(int i=1;i<=n;i++)

{

if(mat[u][i])

if(!vis[i])

{

vis[i]=1;

dfs(i);

low[u]=min(low[u],low[i]);

if(low[i]>=dfn[u])//关节点的第二个条件

{

if(1 == u)son++;

else subset[u]++;

}

}

else

low[u]=min(low[u],dfn[i]);

//low[u]=Min{dfn[u],Min{low[w]|w是u的一个子女},Min{dfn[v]|v与u邻接,且(u,v)是一条回边}}

//此处就是回边的情况,所以是dfn[i]而不是low[i],!!!注意low的定义

}

}

int solve()

{

init();

for(int i=1;i<=n;i++)//可能本身不是连通图

if(!vis[i])

{

vis[i]=1;

dfs(i);

if(son)

{

subset[i]=son-1;

son=0;

}

}

int ans=0;

for(int i=1;i<=n;i++)

{

if(subset[i])ans++;//subset[i]+1就是去掉割点后所形成的连通分支个数

}

return ans;

}

int main()

{

//freopen("poj1144.txt","r",stdin);

while(~scanf("%d",&n) && n)

{

printf("%d\n",solve());

}

return 0;

}


Poj 1144 Zoj 1311 求割点 模板

时间: 2024-10-07 16:52:20

Poj 1144 Zoj 1311 求割点 模板的相关文章

poj 1144 (Tarjan求割点数量)

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

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 1861 Network (Kruskal求MST模板题)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14103   Accepted: 5528   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c

poj1144Network (求割点模板题)

题目连接 题意:给出一个无向图,求出割点的个数 code: #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #define maxn 100005 using namespace std; vector<int> G[maxn]; int dfn[maxn],vis[maxn],low[max

poj 1523 SPF 无向图求割点

SPF 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 failure of a single node, 3, in the network on the left would prevent some of the s

求割点 模板

1 void dfs(int u,int father) 2 { 3 int child=0; 4 dfn[u]=low[u]=++dfs_clock; 5 6 for (int c=head[u];c;c=nxt[c]) 7 { 8 int v=to[c]; 9 if (!dfn[v]) 10 { 11 child++; 12 dfs(v,u); 13 low[u]=min(low[u],low[v]); 14 if (low[v]>=dfn[u]) 15 is_cut[u]=true; 16

poj1144 tarjan求割点 裸题

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

【lightoj-1063】Ant Hills(求割点)

求割点模板题 #include <bits/stdc++.h> using namespace std; const int N = 10004; int dfn[N], low[N]; bool ok[N]; vector<int>V[N]; int n, num, root; void dfs(int s, int f) { low[s] = dfn[s] = ++num; int child = 0; for(unsigned int i = 0; i < V[s].s

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