Nowcoder 203J Graph Coloring I (DFS)

Source: Internet
Author: User

Topic links

Title Description A few undirected connected graphs were drawn on the blackboard, and he found that he could dye the nodes of the graphs in two colors to satisfy the different colors of the neighboring points.
LAN Lan not convinced, on the blackboard drew a complete picture of three points. Repair with LAN Lan said, this figure I can find a simple odd ring.
Lan LAN and on the blackboard drew a N-point m-edge of the undirected connected graph. It is a pity that this is not a counting problem, and the repair is not done.
Lan Lan is very proud, as a cancer of the people, have a good problem of course to share with you, so he put this problem out to you to do. Input Description: The first line of two integers n,m (1≤n,m≤3*105), the next m line two integers per line ai,bi represents an edge (1≤ai,bi≤n).
Ensure that the diagram is connected and that there are no heavy and self loops. Output Description: If you can dye the figure two, the first line outputs 0, the second line outputs n integers representing the color of each point (0≤xi≤1). If you have a variety of legal options, you can output any one.
If you can find a simple odd ring, the first line of output ring length K, the second line of output K integer represents the ring on the node number (1≤yi≤n), you need to ensure that Yi and yi+1 have an edge between the Y1 and the yn. If you have a variety of legal options, you can output any one.
If both cases are feasible, you only need to output any one of these.
If both cases are not feasible, output one line, an integer-1. Input 3 2
1 2
1 3 Output 0
0 1 1 Input 3 3
1 2
1 3
2 3 Output 3
1 2 3 Test instructions to a undirected connected graph, there are two options: coloring all the nodes in black and white, requiring that the neighboring nodes not be the same color, or outputting a ring with an odd number of nodes. Analysis of the first problem is a classic problem, directly select a point to start coloring, and then in the order of Dfs or BFS to paint adjacent points in turn, painted all the points or encountered contradictions; for the second problem, the ring of odd nodes, if the ring is painted, then it will be contradictory, So it can be combined with the first problem, if you can finish the painting of the direct output of the scheme, if you can not finish painting, then must be in the middle of an odd number of nodes encountered in the ring. Therefore, in order to better record the color of the relationship, here should use DFS, and in the face of contradictions, back to the point where the contradiction is painted, got an odd ring.
In general, there is no odd ring, then the coloring must be successful, there are odd rings, the output of odd rings, there will not be two kinds of situations are not satisfied. Summing up the original "simple ring" means simple is the ring, test instructions Understanding deviation Ah, the first to write with BFS, and then in the export path when the discovery simply does not work. Code
#include <stdio.h>#include<vector>typedef std::vector<int>Vi;#defineMAXN 300005//chain-type storage edgeintFST[MAXN], TO[MAXN <<1], NXT[MAXN <<1], z =0;voidAddintUintv) {z++; TO[Z] =v; NXT[Z]= Fst[u]; Fst[u] =Z; Z++; TO[Z] =u; NXT[Z]= Fst[v]; FST[V] =Z;}intN, M;intCLR[MAXN]; Vi RT;intIsOdd/*It's an odd ring marker.*/, Rat/*where the coloring conflict occurs, that is, the start/end point of the odd ring*/, ended/*completed the record of the ring*/;//Direct ColoringvoidDfsintCID) {intnid;  for(intln = Fst[cid]; ln ln =Nxt[ln]) {Nid=TO[LN]; if(Clr[nid] = =-1) {Clr[nid]= !Clr[cid];            DFS (NID); if(ended)return; if(isodd) {rt.push_back (NID); if(cid = rat)//has retreated back to the beginning of the ringEnded =1; return; }        }        Else if(Clr[nid] = = Clr[cid]) {//meet the contradiction, start to fall backRat =nid;            Rt.push_back (NID); IsOdd=1; Ended =0; return; }    }}intMain () {scanf ("%d%d", &n, &m);  for(inti =1; I <= N; ++i) Clr[i] =-1; intai, bi;  for(inti =0; i<m; ++i) {scanf ("%d%d", &ai, &i);    Add (AI, bi); } clr[1] =1; DFS (1); if(IsOdd = =0) {printf ("0\n");  for(inti =1; I <= N; ++i) printf ("%d", Clr[i]); }    Else {        intSZ =rt.size (); //The ring may repeat the beginning of the record, and it will be removed.        if(Rt[sz-1] = = rt[0]) sz--; printf ("%d\n", SZ);  for(inti =0; i<sz; ++i) printf ("%d", Rt[i]); }}

Nowcoder 203J Graph Coloring I (DFS)

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.