zoukankan      html  css  js  c++  java
  • 【codeforces 723E】OneWay Reform

    【题目链接】:http://codeforces.com/contest/723/problem/E

    【题意】

    给你一个无向图;
    让你把这m条边改成有向图;
    然后使得出度数目等于入度数目的点的数目最多;
    输出这个点的数目;
    同时输出更改之后的所有有向边

    【题解】

    需要先明确;
    就是图中度数为奇数的点的个数肯定是偶数个;
    因为每条边都会贡献2度数;
    所以最后的总度数肯定是偶数的;
    则奇度数的点肯定得是偶数个,不然最后不会满足总度数为偶数;
    知道这个之后;
    猜想;
    最后的答案就为原图中度数为偶数的点的个数;
    为了保证方案存在;
    我们新加一个编号为n+1的点;
    然后把这个n+1号点和所有的原图中的度数为奇数的点全都连上边;
    这个时候;
    可以想见;
    这n+1个点都是偶数度数的点了;
    则可以走一个欧拉路径了;
    对于欧拉路径来说;
    每一个点,入度和出度的个数肯定是一样的;
    只要把那些和n+1有关的边省略掉就好;
    其他的直接输出;

    【Number Of WA

    0

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    
    typedef pair<int, int> pii;
    typedef pair<LL, LL> pll;
    
    const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
    const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
    const double pi = acos(-1.0);
    const int N = 210;
    
    int t,n,m,du[N];
    int G[N][N];
    
    void dfs(int x)
    {
        rep1(y,1,n+1)
        if (G[x][y])
        {
            if (x != n + 1 && y != n + 1)
                cout << x << ' ' << y << endl;
            G[x][y] = G[y][x] = 0;
            dfs(y);
        }
    }
    
    int main()
    {
        //freopen("F:\\rush.txt", "r", stdin);
        ios::sync_with_stdio(false), cin.tie(0);//scanf,puts,printf not use
        cin >> t;
        while (t--)
        {
            ms(du, 0),ms(G, 0);
            cin >> n >> m;
            int num1 = n;
            rep1(i, 1, m)
            {
                int x, y;
                cin >> x >> y;
                G[x][y] = G[y][x] = 1;
                du[x]++, du[y]++;
            }
            rep1(i, 1, n)
                if (du[i] & 1)
                    G[n + 1][i] = G[i][n + 1] = 1, num1--;
            cout << num1 << endl;
            rep1(i, 1, n)
                dfs(i);
        }
        return 0;
    }
    
  • 相关阅读:
    GLSL
    c++ 的垃圾收集(garbage collector
    ZZ 红黑树,并非想象中的那么复杂
    【转载】我心目中的android机器档次
    代码优化
    qqww
    solve Ax+By+C=0
    the c10k problem
    标 题: 腾讯面试题目(PHP程序员)
    zz 软件开发流程工具一览
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626337.html
Copyright © 2011-2022 走看看