Hdoj 3018 Ant Trip (non-Tuola Road | | One stroke + and check set)

Source: Internet
Author: User

title Link:http://acm.hdu.edu.cn/showproblem.php?pid=3018

Thinking Analysis: The title can be seen as a stroke of the problem, the minimum number of strokes can be painted all the edges and draw only once;

First, the number of connected graphs in the undirected graph can be obtained, and the number of strokes to be drawn in each undirected graph is calculated and added. In an undirected connected graph, if all points have an even number, there is a Euler loop,

You only need to draw a pen, if you have an odd-numbered point, you need to draw a number of points with an odd degree of number/2; The isolated points needing attention do not need to be drawn;

The code is as follows:

#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Const intMax_n =100000+ -;intFa[max_n], odd[max_n];intLink[max_n], set_count[max_n];voidInit () { for(inti =0; i < max_n; ++i) {Set_count[i]=1; Fa[i]=i; }}intFind (intnum) {    if(Fa[num] = =num)returnnum; Else        returnFa[num] =Find (Fa[num]);}intUnion (intAintb) {    intFa_a =Find (a); intFa_b =Find (b); if(Fa_a = =fa_b)return-1; Else if(Fa_a >fa_b) {Fa[fa_b]=fa_a; SET_COUNT[FA_A]+=Set_count[fa_b]; }    Else{fa[fa_a]=Fa_b; Set_count[fa_b]+=Set_count[fa_a]; }    return 1;}intMain () {intVertex_num, Road_num; intver_1, ver_2;  while(SCANF ("%d%d", &vertex_num, &road_num)! =EOF) {        intAns =0;        Init (); memset (Link,0,sizeof(link)); memset (Odd,0,sizeof(odd));  for(inti =0; i < Road_num; ++i) {scanf ("%d%d", &ver_1, &ver_2); Link[ver_1]++; Link[ver_2]++;        Union (ver_1, ver_2); }         for(inti =1; I <= vertex_num; ++i) {intFA =Find (i); if((link[i) &1) ==1) ODD[FA]++; }         for(inti =1; I <= vertex_num; ++i) {if(Fa[i] = = i && set_count[i]! =1)            {                if(Odd[i] = =0) ans++; Elseans+ = Odd[i]/2; }} printf ("%d\n", ans); }    return 0;}

Hdoj 3018 Ant Trip (non-Tuola Road | | One stroke + and check set)

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.