10129 - Play on Words (UVA)

题目链接如下:

Online Judge

欧拉道路的题。

有向图满足欧拉道路有两个条件:1,图是连通的(无向边意义上);2,最多只能有两个点的出度不等于入度,而且其中一个点的出度比入度大1,一个点的入度比出度大1.

连通是在无向边意义上连通,所以第16行需要 mat[k][i] || mat[i][k] ,只要单方向有路,就算连通。(一个测试数据:

msm

mac

csd

dsa

asd

如果 mat[k][i] || mat[i][k] 写成 mat[k][i] ,这组数据答案会有问题。

我的代码如下:

cpp 复制代码
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
// #define debug

int T, N;
std::string str;
int degree[26];
int mat[26][26];
int visited[26];

void dfs(int k){
    visited[k] = 0;
    for (int i = 0; i < 26; ++i){
        if (visited[i] && (mat[k][i] || mat[i][k])){
            dfs(i);
        }
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d", &T);
    while (T--){
        std::fill(degree, degree + 26, 0);
        std::fill(mat[0], mat[0] + 26 * 26, 0);
        std::fill(visited, visited + 26, 0);
        scanf("%d", &N);
        while (N--){
            std::cin >> str;
            degree[str[0] - 'a']++;
            degree[str.back() - 'a']--;
            mat[str[0] - 'a'][str.back() - 'a']++;
            visited[str[0] - 'a'] = 1;
            visited[str.back() - 'a'] = 1;
        }
        std::map<int, int> mp;
        bool flag = true;
        for (int i = 0; i < 26; ++i){
            if (degree[i] < -1 || degree[i] > 1){
                flag = false;
                break;
            }
            if (degree[i] == -1){
                mp[-1]++;
            } else if (degree[i] == 1){
                mp[1]++;
            }
        }
        if (!flag || mp[-1] > 1 || mp[1] > 1 || mp[-1] != mp[1]){
            printf("The door cannot be opened.\n");
            continue;
        }
        int cnt = 0;
        for (int i = 0; i < 26; ++i){
            if (visited[i]){
                cnt++;
                dfs(i);
            }
        }
        if (cnt == 1){
            printf("Ordering is possible.\n");
        } else {
            printf("The door cannot be opened.\n");
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
NAGNIP14 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队15 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja20 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下20 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶20 小时前
算法 --- 字符串
算法
博笙困了20 小时前
AcWing学习——差分
c++·算法
NAGNIP20 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP20 小时前
大模型微调框架之LLaMA Factory
算法
echoarts20 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客21 小时前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法