Sicily 1034. Forest
Posted ACMICPC
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于Sicily 1034. Forest的主要内容,如果未能解决你的问题,请参考以下文章