UVaOJ 10054 - The Necklace

——by A Code Rabbit


Description

输入一堆两端有颜色的珠子。
输出能否用一条绳子串起来。


Types

Date Structure :: Graphs


Analysis

求无相图的欧拉回路,先判断有没有欧拉回路,然后递归输出即可。
输出的时候注意头尾相连,只要符合条件即可AC(不一定要像样例输出)。


Solution

// UVaOJ 10054
// The Necklace
// by A Code Rabbit

#include <cstdio>
#include <cstring>

const int LIMITS_COLORS = 100;
const int LIMITS_BEADS = 2000;

int num_case;

struct Beads {
    int u;
    int v;
};  

Beads beads[LIMITS_BEADS];
int graph[LIMITS_COLORS][LIMITS_COLORS];
int n;

int order[LIMITS_COLORS];

bool visit[LIMITS_COLORS];
int sum;

void Process(int u, int v);

bool MatchConditions();
void Search(int pos);

void Print(int x);

int main() {
    scanf("%d", &num_case);
    for (int t = 1; t <= num_case; ++t) {
        // INIT.
        memset(graph, 0, sizeof(graph));
        memset(order, 0, sizeof(order));
        memset(visit, false, sizeof(visit));
        sum = 0;
        // Inputs.
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
            int u, v;
            scanf("%d%d", &beads[i].u, &beads[i].v);
            Process(beads[i].u, beads[i].v);
        }
        // Compete the number of colors.
        int num_colors = 0;
        for (int i = 0; i < LIMITS_COLORS; ++i) {
            if (order[i]) {
                ++num_colors;
            }
        }
        // Judge whether the orders of them match conditions.
        bool exist_euler_circuit = MatchConditions();
        // Judge whether the undircected graph is connected.
        if (exist_euler_circuit) {
            for (int i = 0; i < LIMITS_COLORS; ++i) {
                if (order[i]) {
                    Search(i);
                    if (sum != num_colors) {
                        exist_euler_circuit = false;
                    }
                    break;
                }
            }
        }
        // Outputs.
        if (t > 1) {
            printf("\n");
        }
        printf("Case #%d\n", t);
        if (exist_euler_circuit) {
            Print(beads[0].u);
        } else {
            printf("some beads may be lost\n");
        }
    }

    return 0;
}

void Process(int u, int v) {
    ++graph[u][v];
    ++graph[v][u];
    ++order[u];
    ++order[v];
}
            
bool MatchConditions() {
    for (int i = 0; i < LIMITS_COLORS; ++i) {
        if (order[i] % 2) {
            return false;
        }
    }
    return true;
}

void Search(int pos) {
    visit[pos] = true;
    ++sum;
    for (int i = 0; i < LIMITS_COLORS; ++i) {
        if (graph[pos][i] && !visit[i]) {
            Search(i);
        }
    }
}

void Print(int x) {
    for (int i = 0; i < LIMITS_COLORS; ++i) {
        if (graph[x][i]) {
            --graph[x][i];
            --graph[i][x];
            Print(i);
            printf("%d %d\n", i, x);
        }
    }
}


下载PDF

参考资料:无

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值