CodeForces-528C Data Center Drama

题目链接:CodeForces-528C Data Center Drama

题意

给出一个无向图(连通,可能有重边和自环),要求加尽量少的边,并给每条边定向,使每个结点的入度和出度都是偶数。


思路

对于度数为奇数的结点,加边依次连接,例如结点$1,2,3,4$的度数为奇数,则连接$(1,2)$,$(3, 4)$,使所有结点度数都为偶数,则为欧拉图。

如果此时边数为奇数,则对任一结点加个自环,这样可以构造出偶数长度的欧拉回路。沿着欧拉回路每隔一条边反向一次,可令结点每一条入边和出边变成两条入边或两条出边,就满足了题目要求的每个结点的入度和出度都是偶数。


代码实现

#include <cstdio>
#include <cstring>
const int N = 100010, M = 500010;
struct Edge
{
    int to, nex;
} edge[M];
int cnt_e, tot;
int head[N], deg[N], ans[M];
bool vis[M];
void add_edge(int u, int v) {
    edge[++cnt_e].to = v;
    edge[cnt_e].nex = head[u];
    head[u] = cnt_e;
}
void init() {
    cnt_e = 1;
    memset(head, 0, sizeof(head));
    memset(deg, 0, sizeof(deg));
    memset(vis, 0, sizeof(vis));
}
void dfs(int u) {  // 求欧拉回路方案,存在ans中
    for (int i = head[u]; i; i = head[u]) {
        head[u] = edge[i].nex;
        if (!vis[i|1]) {
            vis[i|1] = true;
            int v = edge[i].to;
            dfs(v);
        }
    }
    ans[tot++] = u; // 要后序回溯时记录路径,不能前序记录
}

int main() {
    int n, m;
    while (~scanf("%d %d", &n, &m)) {
        init();
        int ans1 = m;
        for (int i = 0, u, v; i < m; i++) {
            scanf("%d %d", &u, &v);
            add_edge(u, v);
            add_edge(v, u);
            deg[u]++, deg[v]++;
        }
        int pre;
        tot = 0;
        for (int i = 1; i <= n; i++) {
            if (deg[i] & 1) {
                if (tot & 1) {
                    add_edge(pre, i);
                    add_edge(i, pre);
                    ans1++;
                }
                else pre = i;
                tot++;
            }
        }
        if (ans1 & 1) {
            ans1++;
            add_edge(1, 1);
        }
        printf("%d\n", ans1);
        tot = 0;
        dfs(1);
        for (int i = 0; i < tot - 1; i++) {
            if (i & 1) printf("%d %d\n", ans[i], ans[i+1]);
            else printf("%d %d\n", ans[i+1], ans[i]);
        }
        if (!(tot & 1)) puts("1 1");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kangkang-/p/11380634.html

时间: 2024-08-01 05:32:56

CodeForces-528C Data Center Drama的相关文章

Codeforces 527E Data Center Drama(欧拉回路)

题意: 给定一个无向图连通图,把这个的无向边变成有向边,并添加最少的有向边使这个图每个结点的出度为偶数. Solution: 题目很长,并且很多条件说的不太直接,确实不太好懂. 首先先看得到的无向图,是不是可以不加边就满足题目要求. 可以想到对于一个无向图,当所有点的度数为偶数时,图中存在欧拉回路.那么对于一个存在欧拉路的无向图似乎可以以某种方式构造出满足条件的有向边.假设图中有欧拉回路1 2 3 4 1, 可以构造边2->1,2->3,4->3,4->1满足条件. 而对于不存在欧

#296 (div.2) E. Data Center Drama

1.题目描述:点击打开链接 2.解题思路:本题要求每个点发出的两条路的方向都要相同,如果这样的路径不完整,需要添加尽量少的一些边使得满足该条件.实际上本题考查的就是欧拉回路,而且欧拉回路必须是偶数条路径.而欧拉回路存在的一个条件是度数为奇数的结点不能超过两个,但本题肯定不能存在度数为奇数的点,否则不可能满足题意. 因此所有结点的度数必须为偶数,如果发现有两个点的度数均为奇数,那么就把他们连起来.如果所有点的度数均为偶数但总的边数为奇数,那么就要加一条自环,使得路径总数为偶数. 接下来,就从起点出

Codeforces Gym 100513D D. Data Center 前缀和 排序

D. Data Center Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/560/problem/B Description The startup "Booble" has shown explosive growth and now it needs a new data center with the capacity of m petabytes. Booble can b

Data Center Group

Data Center Group||----Sr. Admin Assistant|----Technical Assistant|----Executive Assistant||--Enterprise & HPC platform Group--Cloud Platforms Group--Network Platform Group--Health& Life Sciences--Silicon Photonics Solutions Group--Storage Group||

Data Center手册(4):设计

基础架构 拓扑图 Switching Path L3 routing at aggregation layer L2 switching at access layer L3 switch融合了三种功能: RP, router processor, 处理路由协议 SP, switch processor, 处理L2协议 ASIC, Application-specific integrated circuit专用集成电路,用于重写header的 对于traffic forwarding有几种方法

Data Center手册(2): 安全性

有个安全性有下面几种概念: Threat:威胁 Vulnerability: 安全隐患 Attack: 攻击 有关Threat 常见的威胁有下面几种 DoS(Denial of Service拒绝服务攻击) Breach of confidential information 破解机密信息 Data theft or alteration 数据盗用和篡改 Unauthorized use of compute resources 未授权访问 Identity theft 身份盗用 有关安全隐患

Data Center手册(1):架构

如图是数据中心的一个基本架构 最上层是Internet Edge,也叫Edge Router,也叫Border Router,它提供数据中心与Internet的连接. 连接多个网络供应商来提供冗余可靠的连接 对外通过BGP提供路由服务,使得外部可以访问内部的IP 对内通过iBGP提供路由服务,使得内部可以访问外部IP 提供边界安全控制,使得外部不能随意访问内部 控制内部对外部的访问 为了HA的需要,往往会有两个Border Router Typical enterprise Internet c

Data Center Manager Leveraging OpenStack

这是去年的一个基于OpenStack的数据中心管理软件的想法. Abstract OpenStack facilates users to provision and manage cloud services in a convenient way, including compute instances, storage and network. Meanwhile, data center requires a converged, uniformed management solutio

Hot Topics on Data Center (HotDC) 2018

Keynote Session Accelerate Machine Intelligence: An Edge to Cloud Continuum Hadi Esmaeilzadeh - UCSD Background open source: http://act-lab.org/artifacts Data grows at an unprecedented rate new landscape of computing: personalize and targeted experie