HDU 3018Ant Trip (a stroke problem, using and checking the connected components of undirected graphs)

Source: Internet
Author: User

1. Test instructions: Give a simple picture without a direction, ask at least a few strokes to finish drawing all the sides.

2. Idea:① first Use and check set to find a few connected components;② If there is only one node in the connected component, then it is 0 strokes;③ in a simple undirected graph, if there is no Euler loop, at least use N/2 pen to paint all sides, N is the number of singularities.

3AC Code One (93ms):

#include <cstdio> #include <cstring> #include <set> #include <vector>using namespace Std;int n,m int Father[100005];int deg[100005];//Each node's degree int vis[100005];int odd_cnt[100005];vector<int> vec;int Find (int a)    {int r=a;    while (Father[a]!=a) {a=father[a];    } father[r]=a; return A;}    inline void Union (int a,int b) {a=find (a); B=find (b);//Don't write father[b] Ah ah!!!!!!!!!!!!!!!!    1 if (a!=b) {father[b]=a;    }}int Main () {int ans;        while (scanf ("%d%d", &n,&m) ==2) {ans=0;        Vec.clear ();            for (int i=1; i<=n; i++) {father[i]=i;            deg[i]=0;            vis[i]=0;        odd_cnt[i]=0;            } for (int i=0; i<m; i++) {int A, B;            scanf ("%d%d", &a,&b);            deg[a]++;            deg[b]++;        Union (A, b);            } for (int i=1; i<=n; i++) {father[i]=find (i);       if (vis[father[i]]==0)     {Vec.push_back (father[i]);            Vis[father[i]]=1;        } if (deg[i]%2) odd_cnt[father[i]]++;            } for (int i=0;i<vec.size (); i++) {int f=vec[i];            if (deg[f]==0) continue;            if (odd_cnt[f]==0) ans=ans+1;    Else ans=ans+odd_cnt[f]/2;//in a simple undirected connectivity graph, if there is no Euler loop, at least use N/2 pen to paint all sides, N is the number of singularities} printf ("%d\n", ans); } return 0;}

AC Code Two (156MS):

#include <cstdio> #include <cstring> #include <set>using namespace std;struct node{int num;    Set<int> St;        void Init () {num=0;    St.clear (); }};int N,m;int father[100005];    Node son[100005];//the number of Sons of node I son[i].num, then loaded in the set St int deg[100005];//each node's degree int Find (int a) {int r=a;    while (Father[a]!=a) {a=father[a];    } father[r]=a; return A;}    void Union (int a,int b) {a=find (a);    B=find (b);    if (a!=b) {father[b]=a;    }}int Main () {int ans;        while (scanf ("%d%d", &n,&m) ==2) {ans=0;            for (int i=1; i<=n; i++) {father[i]=i;            son[i]=0;            deg[i]=0;        Son[i].init ();            } for (int i=0; i<m; i++) {int A, B;            scanf ("%d%d", &a,&b);            deg[a]++;            deg[b]++;        Union (A, b);            } for (int i=1; i<=n; i++) {father[i]=find (i); Son[father[i]].num++;        Son[father[i]].st.insert (i); } for (int i=1; i<=n; i++) {if (son[i].num>=2) {set<int>::i                Terator it;                int cnt=0;                        For (It=son[i].st.begin (); It!=son[i].st.end (); it++) {if (deg[*it]%2)                cnt++;                } if (!cnt) ans+=1;     Else ans=ans+cnt/2;//in a simple undirected graph, at least the N/2 pen is used to paint all sides, N is the number of singularities} printf ("%d\n", ans); } return 0;}

AC Code Three (78MS):

#include <cstdio> #include <cstring> #include <set>using namespace std;struct node{int son_num;    int odd_son_num;        void Init () {son_num=0;    odd_son_num=0; }};int N,m;int father[100005];    Node son[100005];//node i's singularity son number Son[i]int deg[100005];//each node's degree int Find (int a) {int r=a;    while (Father[a]!=a) {a=father[a];    } father[r]=a; return A;}    inline void Union (int a,int b) {a=find (a);    B=find (b);    if (a!=b) {father[b]=a;    }}int Main () {int ans;        while (scanf ("%d%d", &n,&m) ==2) {ans=0;            for (int i=1; i<=n; i++) {father[i]=i;            son[i]=0;            deg[i]=0;        Son[i].init ();            } for (int i=0; i<m; i++) {int A, B;            scanf ("%d%d", &a,&b);            deg[a]++;            deg[b]++;        Union (A, b);            } for (int i=1; i<=n; i++) {father[i]=find (i); son[father[i]].son_num++;        if (deg[i]%2) son[father[i]].odd_son_num++; } for (int i=1;i<=n;i++) {if (son[i].son_num>=2) {if (!son[i].odd_                Son_num) Ans+=1; Else ans=ans+son[i].odd_son_num/2;//in a simple undirected graph, at least use the N/2 pen to paint all sides, N is the number of singularities}}/*FO                 R (int i=1; i<=n; i++) {if (son[i].num>=2) {//printf ("%d\n", son[i].num);                Set<int>::iterator it;                int cnt=0;                    For (It=son[i].st.begin (); It!=son[i].st.end (); it++) {//printf ("%d\n", *it);                if (deg[*it]%2) cnt++;                } if (!cnt) ans+=1; Else ans=ans+cnt/2;//in a simple undirected graph, at least the N/2 pen is used to paint all sides, N is the number of singularities}}*/printf ("%d\n", ans    ); } return 0;}


The above code is only a different way to calculate the number of strokes for each connected component

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3018Ant Trip (a stroke problem, using and checking the connected components of undirected graphs)

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.