Wannafly challenge 27B (DFS, linked list head insertion)

Source: Internet
Author: User

# Include <bits/stdc ++. h>
Using namespace STD;
Int CNT = 0;
Int flag = 0;
Int to [400007], NEX [400007], vis [100007], head [100007];
Void add (int A, int B) {// The head Insertion Method of the linked list. When the NEX array is opened to next, it will be compiled incorrectly.
To [++ CNT] = B;
Nex [CNT] = head [a];
Head [a] = CNT;
}
Void DFS (int A, int B ){
For (INT I = head [a]; I! = 0; I = NEX [I]) {// traverse the adjacent contacts of this Vertex
Int v = to [I];
If (V = B) // you have already traversed this situation in B's adjacent points.
Continue;
If (vis [v]) {// indicates that a ring exists.
Int x = vis [a];
If (X-vis [v] + 1) & 1) // The Ring length is an odd number. In this case, three colors are required.
Flag = 1;
Return;
}
Vis [v] = vis [a] + 1;
DFS (V, );
}
}
Int main (){
Int n, m;
Scanf ("% d", & N, & M );
Int X, Y;
For (INT I = 1; I <= m; I ++ ){
Scanf ("% d", & X, & Y );
Add (x, y );
Add (Y, X );
}
Vis [1] = 1;
DFS (1, 0 );
If (n = 1)
Printf ("1 ");
Else if (flag = 1)
Printf ("3 ");
Else if (flag = 0)
Printf ("2 ");
Return 0;
}

Wannafly challenge 27B (DFS, linked list head insertion)

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.