Codeforces Round #285 Div.2

Source: Internet
Author: User

C.misha and Forest (pictured on BFS)

Half of the game, I remember a CF didn't hit, =_=| |

The first two questions quickly cut off, c question has been card no good idea

Hold for a few days, could not help secretly aimed at someone else AC code, found that I did not see the title, the title of the picture given is the forest.

So the point of entry found!

Test instructions

A forest of n nodes, numbered from 0 to n-1, gives the degrees of each node and the number of adjacent nodes, the number of edges in the output graph and the number of all neighboring nodes.

Analysis:

Because it is a forest, so the leaf node in the diagram is a breakthrough. The most obvious characteristics of a leaf node are:

    • The degree is 1
    • It is the number of the adjacent node number that is unique to the neighbor itself.

This will find an edge.

So we can peel the leaves of the outside, like an onion , a layer of peeling.

You can use a queue to maintain, start to enqueue all the leaf nodes, and then expand inward. If the leaf node is removed and a new leaf node appears, the new node is enqueued until the queue is empty.

1#include <cstdio>2 3 Const intMAXN = (1<< -) +Ten;4 5 intDEGREE[MAXN], XORSUM[MAXN];6 intedge[maxn][2], CNT =0;7 intQ[MAXN], head =0, tail =0;8 intMain ()9 {Ten     intN; Onescanf"%d", &n); A      for(inti =0; I < n; i++) -     { -scanf"%d%d", &degree[i], &xorsum[i]); the         if(Degree[i] = =1) q[tail++] =i; -     } -  -      while(Head <tail) +     { -         intt = q[head++]; +         if(Degree[t] = =0)Continue; ADEGREE[T] =0; at         intNeighbor =Xorsum[t]; -edge[cnt][0] = t; edge[cnt++][1] =neighbor; -degree[neighbor]--; -Xorsum[neighbor] ^=T; -         if(Degree[neighbor] = =1) q[tail++] =neighbor; -     } in  -printf"%d\n", CNT); to      for(inti =0; I < CNT; ++i) printf ("%d%d\n", edge[i][0], edge[i][1]); +  -     return 0; the}
code June

Codeforces Round #285 Div.2

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: info-contact@alibabacloud.com 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.