zoukankan      html  css  js  c++  java
  • Ant Trip HDU

    题意:

      Ant Tony和他的朋友们想游览蚂蚁国各地。 

    给你蚂蚁国的N个点和M条边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 
    保证这M条边都不同且不会存在同一点的自环边. 

    也就是蚂蚁分组遍历整个无向图,他们试图把所有的人分成几个小组,每个小组可以从不同的城镇开始。 
    Tony想知道最少需要几组。 

    Input输入包含多组测试用例,由多个空行分隔。 
    每个测试用例的第一行是两个整数N(1<=N<=100000)、M(0<=M<=200000),表明蚂蚁国有N个城镇和M条道路。 
    在M条线路之后,每条线路包含两个整数u,v,(1<=u,v<=N,表示有一条道路连接城镇u和城镇v。 
    Output对于每个测试用例,输出需要形成的最少组数来实现它们的目标。Sample Input

    3 3
    1 2
    2 3
    1 3
    
    4 2
    1 2
    3 4

    Sample Output

    1
    2

    解析:
      题中没有保证所有的点都是一个连通块,所以对于每个连通块,都有三种情况
     1、当前连通块每个点的度数都为偶数,即为欧拉回路 所以一笔就行
     2、有 x 个奇点,已知每两个奇点可以组成一条欧拉路径,所以 笔画数 = x / 2;
    3、 单个点成为连通块 那么0笔
    用并查集维护连通块就好了
    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define pd(a) printf("%d
    ", a);
    #define plld(a) printf("%lld
    ", a);
    #define pc(a) printf("%c
    ", a);
    #define ps(a) printf("%s
    ", a);
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 1e6 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
    int deg[maxn], f[maxn], cnt[maxn];
    set<int> g;
    int find(int x)
    {
        return f[x] == x ? x : (f[x] = find(f[x]));
    }
    
    int main()
    {
        int n, m;
        while(cin >> n >> m)
        {
            for(int i = 1; i <= n; i++) f[i] = i;
            mem(deg, 0);
            mem(cnt, 0);
            g.clear();
            int u, v;
            for(int i = 1; i <= m; i++)
            {
                cin >> u >> v;
                int l = find(u);
                int r = find(v);
                if(l != r) f[l] = r;
                deg[u]++;
                deg[v]++;
            }
            for(int i = 1; i <= n; i++)
            {
                int x = find(i);
                if(deg[i] & 1) cnt[x]++;
                g.insert(x);
            }
            int res = 0;
            for(set<int>::iterator it = g.begin(); it != g.end(); it++)
            {
                int x = *it;
                if(deg[x] == 0) continue;
                if(cnt[x] == 0) res++;
                else res += cnt[x] / 2;
            }
            cout << res << endl;
    
        }
    
    
        return 0;
    }





    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    Working with WordprocessingML documents (Open XML SDK)
    How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
    Azure:Manage anonymous read access to containers and blobs
    Convert HTML to PDF with New Plugin
    location.replace() keeps the history under control
    On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API
    HTTP Modules versus ASP.NET MVC Action Filters
    解读ASP.NET 5 & MVC6系列(6):Middleware详解
    Content Negotiation in ASP.NET Web API
    Action Results in Web API 2
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9754583.html
Copyright © 2011-2022 走看看