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\n");
60         return;
61     }
62     bfs();
63     if (!valid){
64         printf("INVALID\n");
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\n",maxdepth,maxwidth);
73 }
74 int main(){
75     n = read();
76     while (n){
77         work(); n = read();
78     }
79     return 0;
80 }

时间: 2024-08-07 14:09:44

Sicily 1034. Forest的相关文章

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

[BZOJ 1034][ZJOI2008]泡泡堂BNB(类田忌赛马贪心)

http://www.lydsy.com:808/JudgeOnline/problem.php?id=1034 我会说这就是改版POJ的那道Tianji the horse racing么... 不过这个题的游戏规则略有些不同,赢了得2分,平了得1分,输了不扣分,所以贪心过程和POJ的那题略有一点不同,具体看代码吧. #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stri

hdu4941 Magical Forest

Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci. However, the forest will m

hdu 4941 Magical Forest (map容器)

Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 135    Accepted Submission(s): 69 Problem Description There is a forest can be seen as N * M grid. In this forest, there is so

Light OJ 1034 - Hit the Light Switches(强联通分量)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1034 题目大意:有n盏灯,有m个关系, 关系a,b表示如果a灯开关打开那么b灯也会亮起来, 现在求至少需要打开多少开关使所有灯都亮. 题目思路:先由强联通分量缩点, 得到DAG图, 然后根据DAG图,求出有多少入度为0的点, 即为所求. 代码如下: #include<bits/stdc++.h> using namespace std; const int N = 1000

Random Forest Classification of Mushrooms

There is a plethora of classification algorithms available to people who have a bit of coding experience and a set of data. A common machine learning method is the random forest, which is a good place to start. This is a use case in R of the randomFo

1034. 有理数四则运算(20)

本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0. 输出格式: 分别在4行中按照"有理数1 运算符 有理数2 = 结果"的格式顺序输出2个有理数的和.差.积.商.注意输出的每个有理数必须是该有理数的最简形式"k a/b",其中k是整数部分,a/b是最简分数部分:若为负数,则须加括号:若除法

BZOJ 1034: [ZJOI2008]泡泡堂BNB( 贪心 )

贪心...用最弱的赢最弱的,用最强的赢最强的,否则用最弱的和最强的比... (贴个官方题解:将双方的选手均按从强到弱排序,然后第一次扫描尽可能用当前剩下的选手中能赢对手当前最强选手中最弱的一个去赢得胜利,若无法做到,则暂时不考虑给对方最强的选手匹配对手.第二遍扫描使用同样策略去获取尽量多的平局.最后剩下的选手任意匹配就能获得最多的分数) -------------------------------------------------------------------- #include<cs