12118 - Inspector‘s Dilemma (UVA)

题目链接如下:

Online Judge

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

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

#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. 12118 - Inspector‘s Dilemma (UVA)

    2024-02-06 07:02:10       32 阅读
  2. AcWing 1211. 蚂蚁感冒

    2024-02-06 07:02:10       22 阅读
  3. P1118 [USACO06FEB] Backward Digit Sums G/S

    2024-02-06 07:02:10       17 阅读
  4. leetcode 1218.最长定差子序列

    2024-02-06 07:02:10       16 阅读
  5. 智能AI一键消除原音PTN1118方案推荐

    2024-02-06 07:02:10       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-06 07:02:10       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-06 07:02:10       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-06 07:02:10       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-06 07:02:10       20 阅读

热门阅读

  1. 图论:合适的环

    2024-02-06 07:02:10       33 阅读
  2. 【算法练习】leetcode算法题合集之动态规划篇

    2024-02-06 07:02:10       40 阅读
  3. DNS服务器异常有什么影响,怎么处理

    2024-02-06 07:02:10       28 阅读
  4. Golang gorm 结构体定义使用

    2024-02-06 07:02:10       32 阅读
  5. 2024-02-05

    2024-02-06 07:02:10       33 阅读
  6. 普通人应该如何使用GPT

    2024-02-06 07:02:10       34 阅读
  7. 当前小程序跳转另一个小程序

    2024-02-06 07:02:10       31 阅读
  8. 0205作业

    2024-02-06 07:02:10       31 阅读