12118 - Inspector‘s Dilemma (UVA)

题目链接如下:

Online Judge

脑雾严重,这道题一开始我想的方向有问题.....后来看了别人的题解才写出来的.....

用的是欧拉路径的充要条件;以及数连通块。需要加的高速路数目 = 连通块个数 - 1 + sum(每个连通块中连成欧拉路径需要加的高速路数目)。

cpp 复制代码
#include <cstdio>
#include <algorithm>
// #define debug

int V, E, T, a, b, tot, odd, kase = 0;
int arc[1001][1001];
bool vis[1001];
int cnt[1001];

void dfs(int k){
    vis[k] = true;
    if (cnt[k] % 2){
        odd++;
    }
    for (int i = 1; i <= V; ++i){
        if (!vis[i] && arc[i][k]){
            dfs(i);
        }
    }
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d %d %d", &V, &E, &T) == 3 && (V || E || T)){
        std::fill(vis, vis + V + 1, true);
        std::fill(cnt, cnt + V + 1, 0);
        tot = E - 1;
        for (int i = 0; i <= V; ++i){
            for (int j = 0; j <= V; ++j){
                arc[i][j] = arc[j][i] = 0;
            }
        }
        for (int i = 0; i < E; ++i){
            scanf("%d %d", &a, &b);
            arc[a][b] = arc[b][a] = 1;
            vis[a] = vis[b] = false;
            cnt[a]++;
            cnt[b]++;
        }
        for (int i = 1; i <= V; ++i){
            if (!vis[i]){
                odd = 0;
                dfs(i);
                if (odd > 2){
                    tot += (odd - 2) / 2;
                }
                tot++;
            }
        }
        printf("Case %d: %d\n", ++kase, E == 0 ? 0 : tot * T);
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
森焱森1 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机
循环过三天1 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid
127_127_1271 小时前
2025 FJCPC 复建 VP
数据结构·图论·模拟·ad-hoc·分治·转化
闪电麦坤951 小时前
数据结构:二维数组(2D Arrays)
数据结构·算法
凌肖战1 小时前
力扣网C语言编程题:快慢指针来解决 “寻找重复数”
c语言·算法·leetcode
埃菲尔铁塔_CV算法2 小时前
基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
人工智能·深度学习·数码相机·算法·目标检测·计算机视觉
NAGNIP3 小时前
一文搞懂FlashAttention怎么提升速度的?
人工智能·算法
Codebee3 小时前
OneCode图生代码技术深度解析:从可视化设计到注解驱动实现的全链路架构
css·人工智能·算法
刘大猫263 小时前
Datax安装及基本使用
java·人工智能·算法