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", °ree[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