Sicily 1034. Forest
2017-09-16 21:40
489 查看
1034. Forest
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
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
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #define rep(i,l,r) for(int i = l; i <= r; i++) #define clr(x,y) memset(x,y,sizeof(x)) #define travel(x) for(Edge *p = last[x]; p; p = p -> pre) using namespace std; const int maxn = 110; inline int read(){ int ans = 0, f = 1; char c = getchar(); for(; !isdigit(c); c = getchar()) if (c == '-') f = -1; for(; isdigit(c); c = getchar()) ans = ans * 10 + c - '0'; return ans * f; } struct Edge{ Edge *pre; int to; }edge[10010]; Edge *last[maxn], *pt; int n, m, inv[maxn], depth[maxn], width[maxn]; bool valid, vis[maxn]; queue <int> q; inline void addedge(int x,int y){ pt->pre = last[x]; pt->to = y; last[x] = pt++; } void bfs(){ while (!q.empty()) q.pop(); rep(i,1,n) if (!inv[i]){ q.push(i); depth[i] = 0; width[0]++; vis[i] = 1; } if (q.empty()){ valid = 0; return; } while (!q.empty()){ int now = q.front(); q.pop(); travel(now){ if (vis[p->to]){ valid = 0; return; } vis[p->to] = 1; depth[p->to] = depth[now] + 1; width[depth[p->to]]++; q.push(p->to); } } rep(i,1,n) if (!vis[i]) valid = 0; } void work(){ m = read(); pt = edge; clr(last,0); clr(inv,0); clr(width,0); clr(depth,0); clr(vis,0); valid = 1; rep(i,1,m){ int a = read(); int b = read(); addedge(a,b); inv[b]++; if (inv[b] > 1) valid = 0; } if (!valid){ printf("INVALID\n"); return; } bfs(); if (!valid){ printf("INVALID\n"); return; } int maxdepth = 0; int maxwidth = 0; rep(i,1,n){ maxdepth = max(maxdepth,depth[i]); maxwidth = max(maxwidth,width[depth[i]]); } printf("%d %d\n",maxdepth,maxwidth); } int main(){ n = read(); while (n){ work(); n = read(); } return 0; }
View Code
相关文章推荐
- sicily 1034. Forest
- Sicily 1034. Forest
- sicily 1034. Forest
- Sicily 1196. Conflict
- Sicily 1047. Super Snooker
- sicily 1136(线段树+最大子数组)
- Sicily 1193. Up the Stairs
- Sicily 1402. Panic Room
- Sicily 9161. VOYAGER
- Sicily 1118. War on Weather
- Sicily 1302. Magic Square
- Sicily 13914. Train Passengers
- Sicily 1934. 移动小球
- Sicily 9562. SUME
- Sicily 2372. Non-Decreasing Digits
- Sicily 1543. Completing Brackets
- Sicily 1922. Winning Ways
- Sicily 14514. Bread Sorting
- sicily 1381. a*b
- Sicily 14173. Administrative Difficulties