Zoj 1119

#include <iostream>
#include <cstring>

using namespace std;
#define min(a, b) (a < b ? a : b)

int Edge[1001][1001];
int visited[1001];
int low[1001];
int dfn[1001];
int nodes;
int tmpdfn;
int subnets[1001];
int sons;
void DFS( int u )
{
	for(int v = 1; v <= nodes; ++v)
	{
		if(Edge[u][v])
		{
			if(!visited[v])
			{
				visited[v] = 1;
				tmpdfn++;
				low[v] = dfn[v] = tmpdfn;
				DFS( v );
				low[u] = min(low[u], low[v]);
				if(low[v] >= dfn[u])
				{
					if(u != 1)
						subnets[u]++;
					if(u == 1)
						sons++;
				}
			}
			else
				low[u] = min(low[u], dfn[v]);
		}
	}
}

void inite()
{
	sons = 0;
	tmpdfn = 1;
	low[1] = dfn[1] = 1;
	memset( visited, 0, sizeof(visited));
	visited[1] = 1;
	memset( subnets, 0, sizeof(subnets));
}


int main()
{
	int i;
	int u, v;
	int kfind;
	int number = 1;
	while( 1 )
	{
		cin>>u;
		if(u == 0)
			break;
		memset(Edge, 0, sizeof(Edge));
		nodes = 0;
		cin>>v;
		if(nodes < u)
			nodes = u;
		if(nodes < v)
			nodes = v;
		Edge[u][v] = Edge[v][u] = 1;
		while(1)
		{
			cin>>u;
			if(u == 0)
				break;
			cin>>v;
			if(u > nodes)
				nodes = u;
			if(v > nodes)
				nodes = v;
			Edge[u][v] = Edge[v][u] = 1;
		}
		if(number > 1)
			cout<<endl;
		cout<<"Network #"<<number<<endl;
		number++;
		inite();
		DFS( 1 );
		if(sons > 1)
			subnets[1] = sons - 1;
		kfind = 0;
		for(i = 1; i <= nodes; ++i)
		{
			if(subnets[i])
			{
				kfind = 1;
				cout<<"  SPF node "<<i<<" leaves "<<subnets[i] + 1<<" subnets"<<endl;
			}
		}
		if( !kfind )
		{
			cout<<"  No SPF nodes"<<endl;
		}

	}
	return 0;
}

你可能感兴趣的:(关节点)