Cdoj 32 Tree War (Battle on the tree) Problem solving report

Source: Internet
Author: User

Ah ah, the last article, has more than 0 points, I also intend to write a first time to work to make money to do the feeling of tutoring ...

Then there is time to do the final question, it is not very difficult to roar?

Title Link:

Very simple topic, compare a tree with a root tree of two nodes which height.

Similarly, the use of a wide search. Do not ask why I call a wide search, do not let me pressure code to look like, programming is an art ...

Maybe I should standardize the naming of const variables, for example.

#include <cstdio> #include <cstring>using namespace std;const int maxn = 100005;int N, m;int G[MAXN], TO[MAXN], NEXT[MAXN], E;int root, Father[maxn];int QUEUE[MAXN], head, tail, Depth[maxn];int nextint () {char C; while (C = GetChar ( )) < ' 0 ' | | C > ' 9 '); int r = C-' 0 '; while ((c = GetChar ()) >= ' 0 ' && C <= ' 9 ') (r *=) + = C-' 0 '; return r;} void Addedge (int u, int v) {Next[e] = G[u]; To[e] = v; g[u] = e++;} void BFs (int root) {memset (depth, 0, sizeof depth); for (queue[head = tail = 0] = root; head <= tail; ++head) for (int E = G[queue[head]], child = to[e]; ~e; Child = To[e = Next[e]]) if (child! = Father[queue[head]]) depth[child] = Depth[queue[head]] + 1, queue[++tail] = child;} int main () {while ((N = Nextint ()) && (M = Nextint ())) {memset (g,-1, sizeof g); e = 0;memset (father, 0, sizeof FA ther); for (int i = 1; i < N; ++i) {int A = Nextint (), B = Nextint (); Addedge (A, B); father[b] = A;} for (int i = 1; I <= N; ++i) if (!father[i]) {root =I break;} BFS (Root), while (m--) {int X = Nextint (), Y = Nextint (), if (Depth[x] <= depth[y]) puts ("Lxh"); else puts ("PFZ");}} return 0;}

OK, I'll write a real line to search. In fact, the above BFS just changed the variable name and added a space and line, some variable names slightly changed, can be compared to the above BFS function.

for(q[h=t=0]=rt;h<=t;++h)for(int e=g[q[h]];~e;e=nxt[e])if(to[e]!=p[q[h]])d[to[e]]=d[q[h]]+1,q[++t]=to[e];

Kill me, this piece of code is too ugly.

Cdoj 32 Tree War (Battle on the tree) Problem solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.