zoukankan      html  css  js  c++  java
  • hdu3018欧拉回路题

    Ant Trip

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1107    Accepted Submission(s): 404

    Problem Description
    Ant Country consist of N towns.There are M roads connecting the towns.
    Ant Tony,together with his friends,wants to go through every part of the country. 
    They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are
    trying to divide all the people into several groups,and each may start at different town.Now tony wants to know what is the least groups of ants that needs to form to
    achieve their goal.
     
    Input
    Input contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating
    that there are N towns and M roads in Ant Country.Followed by M lines,each line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town
    a and town b.No two roads will be the same,and there is no road connecting the same town.
     
    Output
    For each test case ,output the least groups that needs to form to achieve their goal.
     
    Sample Input
    3 3
    1 2
    2 3
    1 3
     
    4 2
    1 2
    3 4
     
    Sample Output
    1
    2

     欧拉回路简单题:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #include <string.h>
     5 #include <math.h>
     6 #include <vector>
     7 #include <stack>
     8 using namespace std;
     9 #define ll long long int
    10 int a[110000];
    11 int aa[100005];
    12 int find(int x)
    13 {
    14     while(x!=a[x])x=a[x];
    15     return x;
    16 }
    17 int main()
    18 {
    19     int n,m;
    20     while(scanf("%d%d",&n,&m)!=EOF)
    21     {
    22         memset(aa,0,sizeof(aa));
    23         int i,x,y;
    24         int b[n+1];
    25         for(i=1; i<=n; i++)
    26             a[i]=i;
    27         memset(b,0,sizeof(b));
    28         for(i=0; i<m; i++)
    29         {
    30             scanf("%d%d",&x,&y);
    31             b[x]++;
    32             b[y]++;
    33             int fx=find(x);
    34             int fy=find(y);
    35             if(fy!=fx)
    36                 a[fy]=fx;
    37         }
    38         int sum=0;
    39         for(i=1; i<=n; i++)
    40         {
    41             if(b[i]&1)
    42                 aa[find(a[i])]++;
    43         }
    44         for(i=1; i<=n; i++)
    45         {
    46             int tt=find(a[i]);
    47             if(b[i]&&tt==i)
    48             {
    49                 if(aa[tt])
    50                     sum+=aa[tt]/2;
    51                 else sum++;
    52             }
    53         }
    54         cout<<sum<<endl;
    55     }
    56 
    57 }
    View Code
  • 相关阅读:
    python-函数作用域
    python-yield
    python-内置函数
    python-迭代和递归
    Command /usr/bin/codesign failed with exit code 1
    vue-infinite-scroll 自动加载
    git登陆迁移 SourceTree 不能自动识别
    Xcode不自动提示代码
    NSTimer的循环引用
    iOS autolayout 代码,自定义间距
  • 原文地址:https://www.cnblogs.com/ERKE/p/3257384.html
Copyright © 2011-2022 走看看