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>
#include <vector>
using namespace std;
#define N 100005
#define M 200300
#define inf 10000000
struct Edge{
	int from,to,next;
	bool cut;
}edge[2*M];
int head[N],edgenum;
int Low[N],DFN[N],Stack[N],Stack2[N];//Belong数组的值是1~block
int Index,top,top2;
int Belong[N],block;//新图的连通块标号(1~block)
bool Instack[N], cut[N];
int bridge; //割桥数量
int add_block[N]; //add_block[i]表示删掉i点后增加的连通分量数
void addedge(int u,int v){
	Edge E={u,v,head[u],0}; edge[edgenum]=E; head[u] = edgenum++;
	Edge E2={v,u,head[v],0};edge[edgenum]=E2;head[v] = edgenum++;
}
void Tarjan(int u,int pre){
	int v, son = 0;
	Low[u] = DFN[u] = ++Index;
	Stack[top++] = u;
	Stack2[top2++] = u;
	Instack[u] = true;
	for(int i = head[u]; ~i ;i = edge[i].next){
		v = edge[i].to;
		// 如果重边有效的话下面这句改成: if(v == pre && pre_num == 0){pre_num++;continue;} pre_num在for上面定义 int pre_num=0;
		if( v == pre )continue;
		if( !DFN[v] )
		{
			son++;
			Tarjan(v,u);
			if(Low[u] > Low[v])Low[u] = Low[v];
			if(Low[v] > Low[u])
			{
				bridge++;
				edge[i].cut = edge[i^1].cut = true;
			}
			if(u != pre && Low[v] >= DFN[u])//不是树根
			{
				cut[u] = true;
				add_block[u]++;
			}
		}
		else if(Low[u] > DFN[v])Low[u] = DFN[v];
	}
	if(Low[u] == DFN[u]){
		block++;
		do{
			v = Stack[--top];
			Instack[v] = false;
			Belong[v] = block;
		}while( v != u );
	}
	if(u == pre && son > 1)cut[u] = true;
	if(u == pre)add_block[u] = son - 1;
	Instack[u] = false;
	top2--;
}
void work(int l, int r){
	memset(DFN,0,sizeof(DFN));
	memset(Instack,false,sizeof(Instack));
	memset(cut, false, sizeof cut);
	Index = top = block = bridge = 0;
	top2 = 0;
	for(int i = l; i <= r; i++)if(!DFN[i])Tarjan(i,i);
}
vector<int>G[N];//点标从1-block
void suodian(){
	for(int i = 1; i <= block; i++)G[i].clear();
	for(int i = 0; i < edgenum; i+=2){
		int u = Belong[edge[i].from], v = Belong[edge[i].to];
		if(u==v)continue;
		G[u].push_back(v), G[v].push_back(u);
	}
}
void init(){edgenum = 0; memset(head,-1,sizeof(head));}

int g[110][110];
char buf[1010];
int main(){
	int n;
	while(scanf("%d",&n),n){
		gets(buf);
		memset(g,0,sizeof(g));
		while(gets(buf))
		{
			if(strcmp(buf,"0")==0)break;
			char *p = strtok(buf," ");
			int u;
			sscanf(p,"%d",&u);
			p = strtok(NULL," ");
			int v;
			while(p)
			{
				sscanf(p,"%d",&v);
				p = strtok(NULL," ");
				g[u][v]=g[v][u]=1;
			}
		}
		init();
		for(int i = 1;i <= n;i++)
			for(int j = i+1;j <= n;j++)
				if(g[i][j])
				{
					addedge(i,j);
				}
				work(1,n);
				int ans = 0;
				for(int i = 1; i <= n; i++)ans += cut[i];
				printf("%d\n",ans);
	}
	return 0;
}

UVA 315 求割点数,码迷,mamicode.com

时间: 2024-10-03 22:40:33

UVA 315 求割点数的相关文章

UVA 318 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 togethe

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

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

Uva 796 求桥

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 /*** Uva 796 求桥 题目要求:输出题目中所有的桥,按其所连接的点从小到大的顺序输出 解题思路:tarjan算法,所有树枝边都是桥(dfn[u]<low[v]),利用vector存储一下就可以了 */ #include <stdio.h> #include &

无向图求割顶与桥

无向图求割顶与桥 对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或割顶.对于连通图,割顶就是删除之后使图不再连通的点.如果删除边(u,v)一条边,就可以让连通图变成不连通的,那么边(u,v)是桥. 具体的概念和定义比较多,在刘汝佳<<训练指南>>P312-314页都有详细的介绍. 下面来写求无向图割顶和桥的DFS函数.我们令pre[i]表示第一次访问i点的时间戳,令low[i]表示i节点及其后代所能连回(通过反向边)的最早祖先的pre值. 下面的dfs函数返回

UVA 315 :Network (无向图求割顶)

题目链接 题意:求所给无向图中一共有多少个割顶 用的lrj训练指南P314的模板 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=109; struct Edge { int to,next; Edge(){} Edge(int _to,int _next) { to=_to; next=_next; } }edge[N*N*2]; int head[N]; int dfn[N],lo

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【求割点数目】

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