ACM MIPT - 图形存在难题 - 示例不清楚

时间:2012-09-29 14:05:29

标签: algorithm

这是参考'图存在'问题 - http://acm.mipt.ru/judge/problems.pl?problem=110。有人可以解释为什么示例1中没有树但是示例2中有一棵树吗?在两个示例中,顶点0,1,2和3彼此连接。以下是问题陈述和示例供您参考:

You are given a matrix of distances in a graph.
You should check whether this graph could be a tree or set of trees (forest).
Edge length is 0 or positive integer.

Input: The first line contains number of vertices N.
Next N lines contains matrix (only left bottom triangle of matrix).
Distance -1 corresponds to infinite distance.

Output: Output YES or NO. If YES, then next lines should contains list of edges
of the tree (any tree (forest) with given distance matrix).
Each edge is coded by two identifiers of it's ends.
Vertex identifiers are numbers 0, 1, ..., N-1.

Input#1
4
0 
1 0
1 1 0
1 1 1 0

Output#1
NO

Input#2
5
0 
1 0
2 1 0
3 2 1 0
-1 -1 -1 -1 0

Output#2
YES
0 1
1 2
2 3

1 个答案:

答案 0 :(得分:1)

这个问题并没有很好地从Russian original翻译过来。

给定的矩阵不是图中可能得出结论的边的矩阵,而是距离矩阵。每个边缘可能具有1的权重,但我不完全确定具有非负权重。必须检查树木或森林是否可以实现矩阵

在第一个例子中,所有顶点都是连接的,但第二个例子可以实现图形:

    Example 2:
    (0) - (1) - (2) - (3)  (4) 

示例1中的图表是

    Example 1:
    (0) - (1) - (2) - (3)
     |_____|_____|     |     
     |     |___________|
     |_________________|