sicily 1034. Forest

原创 2015年11月17日 20:48:04

1034. Forest

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

In the field of computer science, forest is important and deeply researched , it is a model for many data structures . Now it’s your job here to calculate the depth and width of given forests.

     Precisely, a forest here is a directed graph with neither loop nor two edges pointing to the same node. Nodes with no edge pointing to are roots, we define that roots are at level 0 . If there’s an edge points from node A to node B , then node B is called a child of node A , and we define that B is at level (k+1) if and only if A is at level k .

      We define the depth of a forest is the maximum level number of all the nodes , the width of a forest is the maximum number of nodes at the same level.

Input

There’re several test cases. For each case, in the first line there are two integer numbers n and m (1≤n≤100, 0≤m≤100, m≤n*n) indicating the number of nodes and edges respectively , then m lines followed , for each line of these m lines there are two integer numbers a and b (1≤a,b≤n)indicating there’s an edge pointing from a to b. Nodes are represented by numbers between 1 and n .n=0 indicates end of input.

Output

For each case output one line of answer , if it’s not a forest , i.e. there’s at least one loop or two edges pointing to the same node, output “INVALID”(without quotation mark), otherwise output the depth and width of the forest, separated by a white space.

Sample Input

1 01 11 13 11 32 21 22 10 88

Sample Output

0 1INVALID1 2INVALID

题目分析

求森林最大宽度与深度
拓扑排序,若存在环则失败
每轮确定入度为零的节点数即为该层的宽度
轮数即为深度
一直WA是忽略了题目还限制了不存在两条边指向一个节点,即每个点的入度必须<=1


#include <iostream>
#include <vector>
#include <memory.h>

struct Node {
  int indegree;
  std::vector<int> child;
};

int main()
{
  int num, edge;
  while (std::cin >> num >> edge && num != 0) {
    Node nodes[num+1];
    for (int i = 1; i <= num; ++i) {
      nodes[i].indegree = 0;
    }
    bool visited[num+1];
    memset(visited, false, sizeof(visited));

    bool valid = true;
    int papa, son;
    for (int i = 0; i < edge; ++i) {
      std::cin >> papa >> son;
      nodes[son].indegree++;
      nodes[papa].child.push_back(son);
      if (nodes[son].indegree == 2)
        valid = false;
    }
    if (!valid) {
      std::cout << "INVALID" << std::endl;
      continue;
    }

    int height = -1;
    int width = 0;
    int count = 0;

    while (true) {
      std::vector<int> root;
      for (int i = 1;  i <= num; ++i) {
        if (!visited[i] && nodes[i].indegree == 0) {
          root.push_back(i);
        }
      }
      if (root.size() == 0)
        break;
      width = width < root.size() ? root.size() : width;
      for (int i = 0; i < root.size(); ++i) {
        visited[root[i]] = true;
        count++;
        for (int j = 0; j < nodes[root[i]].child.size(); ++j)
          nodes[nodes[root[i]].child[j]].indegree--;
      }
      height++;
    }
    if (count == num) std::cout << height << " " << width << std::endl;
    else std::cout << "INVALID" << std::endl;
  }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Sicily 1034 Forest

题目:http://soj.me/1034 题目大意: 给出一个森林,首先判断这个森林是否合法,然后求出这个森林的最大深度和最大宽度。 不合法的意思是:有环或者存在多个结点指向同一个结点的情况。...

Sicily 1034. Forest

要明确的是这是一个森林,存在很多棵树,我们需要判断输入是否满足产生多颗树的要求,然后再输出森林的深度和宽度。这题有一些陷阱很容易让人判断出错,即使我AC后在写这个解题报告时,也发现了我的代码存在漏洞,...
  • Ederick
  • Ederick
  • 2012年02月04日 11:46
  • 853

sicily1034. Forest

Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description In the field of computer scienc...

Sicily---1034. Forest

思路:由于题目要求合法的图应该 ①没有环 ②没有入度大于1的顶点,符合树的特点,因此,其实合法的输入其实是一堆树的集合,即为森林(o(╯□╰)o好吧,这点从题目就能看得出来)解决方法:判断是否会生成环...

SOJ 1034. Forest

Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description In the field of computer science, fo...

1034 forest

大水题,WA到吐了,结果发现犯了一弱智错误,擦 1.定义dept[]数组,主要用bfs ,将子节点的dept值累加父节点的dept值,遍历一颗或者若干树。从根节点(bepointed[]值为fals...

soj 1034 Forest_求树的深度和宽度

求树的深度和宽度
  • neng18
  • neng18
  • 2014年05月02日 20:00
  • 837

1034. Forest

1034. Forest Time Limit: 1sec    Memory Limit:32MB Description In the field of computer scien...

sicily1034

这道题也算是一道简单题,但是因为我太水,以至于提交了很多次菜

sicily 1034之思路清晰

题目在这里1034
  • xujqiao
  • xujqiao
  • 2014年08月12日 11:11
  • 193
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sicily 1034. Forest
举报原因:
原因补充:

(最多只允许输入30个字)