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
  • 相关阅读:
    luogu P4284 [SHOI2014]概率充电器 期望 概率 树形dp
    luogu P5161 WD与数列 SAM 线段树合并 启发式合并
    5.5 省选模拟赛 B Permutation 构造 贪心
    luogu P3761 [TJOI2017]城市 树的直径 bfs
    一本通 1783 矩阵填数 状压dp 容斥 计数
    CF R638 div2 F Phoenix and Memory 贪心 线段树 构造 Hall定理
    BSOJ 5445 -- 【2018雅礼】树 prufer序列 dp
    CF1037H Security 线段树合并 SAM
    c++11の顺序容器
    c++11の关联容器
  • 原文地址:https://www.cnblogs.com/ERKE/p/3257384.html
Copyright © 2011-2022 走看看