• Sicily 1034. Forest


    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 0
    1 1
    1 1
    3 1
    1 3
    2 2
    1 2
    2 1
    0 88
    

    Sample Output

    0 1
    INVALID
    1 2
    INVALID
    

    Problem Source

    ZSUACM Team Member

    太久没打代码了导致用队列忘记将元素压入队列……MDZZ

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 #define rep(i,l,r) for(int i = l; i <= r; i++)
     7 #define clr(x,y) memset(x,y,sizeof(x))
     8 #define travel(x) for(Edge *p = last[x]; p; p = p -> pre)
     9 using namespace std;
    10 const int maxn = 110;
    11 inline int read(){
    12     int ans = 0, f = 1; char c = getchar();
    13     for(; !isdigit(c); c = getchar()) if (c == '-') f = -1;
    14     for(; isdigit(c); c = getchar()) ans = ans * 10 + c - '0';
    15     return ans * f;
    16 }
    17 struct Edge{
    18     Edge *pre; int to;
    19 }edge[10010];
    20 Edge *last[maxn], *pt;
    21 int n, m, inv[maxn], depth[maxn], width[maxn];
    22 bool valid, vis[maxn];
    23 queue <int> q;
    24 inline void addedge(int x,int y){
    25     pt->pre = last[x]; pt->to = y; last[x] = pt++;
    26 }
    27 void bfs(){
    28     while (!q.empty()) q.pop();
    29     rep(i,1,n) if (!inv[i]){
    30         q.push(i); depth[i] = 0; width[0]++; vis[i] = 1;
    31     }
    32     if (q.empty()){
    33         valid = 0; return;
    34     }
    35     while (!q.empty()){
    36         int now = q.front(); q.pop();
    37         travel(now){
    38             if (vis[p->to]){
    39                 valid = 0; return;
    40             }
    41             vis[p->to] = 1;
    42             depth[p->to] = depth[now] + 1;
    43             width[depth[p->to]]++;
    44             q.push(p->to);
    45         }
    46     }
    47     rep(i,1,n) if (!vis[i]) valid = 0;
    48 }
    49 void work(){
    50     m = read();
    51     pt = edge; clr(last,0); clr(inv,0); clr(width,0); clr(depth,0); clr(vis,0); valid = 1;
    52     rep(i,1,m){
    53         int a = read(); int b = read();
    54         addedge(a,b);
    55         inv[b]++;
    56         if (inv[b] > 1) valid = 0;
    57     }
    58     if (!valid){
    59         printf("INVALID
    ");
    60         return;
    61     }
    62     bfs();
    63     if (!valid){
    64         printf("INVALID
    ");
    65         return;
    66     }
    67     int maxdepth = 0; int maxwidth = 0;
    68     rep(i,1,n){
    69         maxdepth = max(maxdepth,depth[i]);
    70         maxwidth = max(maxwidth,width[depth[i]]); 
    71     }
    72     printf("%d %d
    ",maxdepth,maxwidth);
    73 }
    74 int main(){
    75     n = read();
    76     while (n){
    77         work(); n = read();
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    requests
    Py西游攻关之Socket网络编程
    编码的真相
    where查询条件的字段顺序打乱会影响命中索引吗?
    mysql联合索引怎样才会命中
    mysql执行计划解释
    mysql模糊查询(%)或范围查询怎样会影响命中索引
    完全符合能量守恒,胡克定理的JavaScript弹簧振子(超简洁版,超越第一版!)
    高仿真可控弹簧振子JavaScript编写
    JavaScript 3D, Cube,3D 立方体
  • 原文地址:https://www.cnblogs.com/jimzeng/p/sicily1034.html
Copyright © 2020-2023  润新知