Ultraviolet A 10596-morning walk, naked Euler's loop

Source: Internet
Author: User
10596-Morning
Walk
3791 37.43% 1073 89.84%

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 105 & page = show_problem & problem = 1537

Question type: Euler loop, query set, DFS

Question:

Kamal isMotashotaGuy.
He has got a new job in Chittagong. So, he has moved to Chittagong from Dinajpur.
He was getting fatter in Dinajpur as he had no work in his hand there.
So, moving to Chittagong has turned to be a blessing for him. Every morning he takes a walk through the hilly roads of Charming City Chittagong.
He is enjoying this city very much. There are so many roads in chittagongand every morning he takes different paths for his walking. But while choosing a path he makes sure
He does not visit a road twice not even in his way back home. an intersection point of a road is not considered as the part of the road. in a sunny morning, he was thinking about how it wocould be if he cocould visit all the roads of the city in a single walk.
Your task is to help Kamal in determining whether it is possible
He or not.


Question:

Kamal goes from home to chittagongand every morning. There are many routes from home to chittagongand,
He needs to select a route every morning from home to chittagongand and go back to home from chittagongand. This route cannot go through the same route repeatedly. There may be multiple routes between two locations. For example, if the route from A to B appears multiple times, it means that each time it appears, it is a different path.


Analysis and Summary:

This is the naked Euler loop. Ultraviolet
10054-The neck.pdf and
Ultraviolet
10129-play on words. No longer cumbersome


1. Euler Loop + DFS

# Include <cstring> # include <iostream> # include <cstdio> using namespace STD; int vis [210], n, m, G [210] [210], indegree [210], outdegree [210]; void DFS (INT v) // depth-first traversal {vis [v] = true; For (INT I = 0; I <N; I ++) {If (! Vis [I] & G [v] [I]) {DFS (I) ;}} int main () {# ifdef local freopen ("input.txt ", "r", stdin); # endif while (~ Scanf ("% d", & N, & M) {memset (G, 0, sizeof (g); int A, B; memset (VIS, 0, sizeof (VIS); For (INT I = 0; I <m; ++ I) {scanf ("% d", & A, & B ); G [a] [B] = G [B] [a] = 1; ++ vis [a]; ++ vis [B];} int CNT = 0; for (INT I = 0; I <n; ++ I) {If (vis [I] % 2 = 1) {++ CNT; break ;}} memset (VIS, 0, sizeof (VIS); If (CNT | M <2) printf ("Not possible \ n"); else {DFS (0 ); bool flag = true; For (INT I = 0; I <n; ++ I) {If (! Vis [I]) Flag = false;} If (FLAG) printf ("Possible \ n"); else printf ("Not possible \ n") ;}} return 0 ;}

2. Euler Loop + and query set

# Include <cstring> # include <iostream> # include <cstdio> using namespace STD; int vis [210], n, m, G [210] [210], indegree [210], outdegree [210], F [210]; void Init () // check whether the set is a connected graph {int I; for (I = 0; I <n; I ++) f [I] = I;} int find (INT X) {int r = x; while (F [R]! = R) r = f [R]; F [x] = r; return r;} void Union (int x, int y) {int FX, FY; FX = find (x); FY = find (y); If (FX! = FY) f [FX] = FY;} int main () {# ifdef local freopen ("input.txt", "r", stdin); # endif while (~ Scanf ("% d", & N, & M) & N) {memset (G, 0, sizeof (g); int A, B; init (); memset (VIS, 0, sizeof (VIS); For (INT I = 0; I <m; ++ I) {scanf ("% d", & A, & B); ++ G [a] [B]; ++ G [B] [a]; ++ vis [a]; ++ vis [B]; Union (a, B);} int ans = 0; For (INT I = 0; I <N; ++ I) if (F [I] = I) ++ ans; If (ANS = 1 & M> = 2) {int CNT = 0; for (INT I = 0; I <n; ++ I) {If (vis [I] % 2 = 1) {++ CNT; break ;}} if (CNT) printf ("Not possible \ n"); else printf ("Possible \ n");} else printf ("Not possible \ n ");} return 0 ;}


-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 ,
D_double





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.