Vertex acm入门 day1---floyd算法第六题

该程序实现了一个寻找有向图中从指定起点无法到达的顶点的功能。输入包含多个有向图及其起始节点,每个图的顶点数不超过100。通过Floyd算法计算可达矩阵,然后找出每个指定起始节点的不可达顶点并输出。样例输入和输出展示了如何处理多个图和起始节点的情况。

UVA - 280

Write a program that searches a directed graph for vertices which are
inaccessible from a given starting vertex. A directed graph is
represented by n vertices where 1 ≤ n ≤ 100, numbered consecutively 1
. . . n , and a series of edges p -> q which connect the pair of nodes
p and q in one direction only. A vertex r is reachable from a vertex p
if there is an edge p -> r, or if there exists some vertex q for which
q is reachable from p and r is reachable from q. A vertex r is
inaccessible from a vertex p if r is not reachable from p.

Input

The input data for this program consists of several directed graphs and starting nodes. For each graph, there is first one line containing a single integer n. This is the number of vertices in the graph Following, there will be a group of lines, each containing a set of integers. The group is terminated by a line which contains only the integer ‘0’. Each set represent a collection of edges. The first integer in the set, i, is the starting vertex, while the next group of integers, j . . . k, define the series of edges i -> j . . . i -> k, and the last integer on the line is always ‘0’. Each possible start vertex i, 1 ≤ i ≤ n will appear once or not at all. Following each graph definition, there will be one line containing a list of ntegers. The first integer on the line will specify how many integers follow. Each of the following integers represents a start vertex to be investigated by your program. The next graph then follows. If there are no more graphs, the next line of the file will contain only the integer ‘0’.

Output

For each start vertex to be investigated, your program should identify
all the vertices which are inaccessible from the given start vertex.
Each list should appear on one line, beginning with the count of
inaccessible vertices and followed by the inaccessible vertex numbers.

题意:找出该图中不能抵达的点,也就是悬挂点
首先输入n代表有n个顶点,随后每一行首先输入顶点v,然后输入v相邻的点to(包括自身)即一条边,有向图,每一行的末尾以0结束,当输入的顶点v为0代表结束;
接着,再输入想要查找的点的数量m,然后输入所查找的顶点标号。

输出:首先输出所要查找的顶点不能抵达的顶点数量,后面跟着所不能抵达的顶点标号,共m行。

思路:使用Floyd算法,三重循环直接计算出每个点所能抵达的点,也就是可达矩阵。

Sample Input

3
1 2 0
2 2 0
3 1 2 0
0
2 1 2
0

Sample Output

2 1 3
2 1 3
#include <cstdio>
#include <cstring>
using  namespace std;
int n;
int gra[1000][1000];
void Floyd()
{     
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	for(int k=1;k<=n;k++)
	{
		if(gra[j][i]&&gra[i][k]) gra[j][k]=1;
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF&&n)
	{
		int v;
		memset(gra,0,sizeof(gra));
		while(scanf("%d",&v)!=EOF&&v)
		{
			int to;
			while(scanf("%d",&to)!=EOF&&to)
			{
				gra[v][to]=1;
			}
		}
		Floyd();
		int num;
		scanf("%d",&num);
		int vt[1000];
		for(int i=0;i<num;i++ )
		{
			scanf("%d",&vt[i]);	
		}
		int temp[1000],idx=0;
		for(int i=0;i<num;i++)
		{
			idx=0;
			for(int j=1;j<=n;j++)
			{
				if(gra[vt[i]][j]==0) temp[idx++]=j;
			}
			printf("%d",idx);
			for(int j=0;j<idx;j++)
			{
				printf(" %d",temp[j]);
			}
			printf("\n");
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值