题意:

分析:

欧拉通路:图连通;图中只有0个或2个度为奇数的结点

这题我们只需要判断选择的边构成多少个联通块, 再记录全部联通块一共有多少个奇度顶点。

然后我们在联通块中连线, 每次连接两个联通块就减少2个奇度顶点, 然后再数一下剩下的奇度顶点odd(肯定是剩下偶数个), 因为存在两个奇度顶点的图也是欧拉通路, 我们只需要在(odd - 2)个顶点中连线使其变为偶度顶点即可。

如果本身就没有奇度顶点就不需要除了。

所以答案就是 T * (E  +(联通块 - 1) + (odd - 2)/ 2))

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int V, E, T;
vector<int> G[maxn];
set<int> pick;
int deg[maxn];
bool vis[maxn];
void dfs(int u){
vis[u] = ;
for(int i = ; i < G[u].size(); i++){
if(!vis[G[u][i]]){
dfs(G[u][i]);
}
}
}
int main(){ int kase = ;
while(scanf("%d %d %d", &V, &E, &T) && V){ memset(deg,, sizeof(deg));
memset(vis, , sizeof(vis));
pick.clear(); for(int i = ; i < E; i++){
int u, v; scanf("%d %d", &u, &v);
deg[u]++, deg[v]++;
G[u].push_back(v);
G[v].push_back(u);
pick.insert(v);
pick.insert(u);
} int part = ;
int odd = , even = ;
for(auto it = pick.begin(); it != pick.end(); it++){
if(!vis[*it]){
dfs(*it);
part++;
}
if(deg[*it] % == ){
even++;
}
else odd++;
G[*it].clear();
}
odd -= (part - ) * ; int ans = E + part - ; if(odd > ){
ans += (odd -)/;
} if(E == ) ans = ;//特判一下没有边的情况
printf("Case %d: %d\n",kase++, ans * T); }
return ;
}

UVa 12118 检查员的难题 (dfs判连通, 构造欧拉通路)的更多相关文章

  1. UVa 12118 检查员的难题(dfs+欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

    判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...

  3. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  4. CF Gym 100187J Deck Shuffling (dfs判连通)

    题意:给你一堆牌,和一些洗牌机,可以改变牌的顺序,问你能不能通过洗牌机把数字为x的牌洗到第一个位置. 题解:反向建边,dfs判断连通性 #include<cstdio> #include& ...

  5. Uva 1103 古代象形符号(dfs求连通块, floodfill, 进制转换)

    题意: 给定一个H行W列的字符矩阵(H<200, W < 50), 输入的是一个十六进制字符, 代表一行四个相邻的二进制, 1代表像素, 0代表没有像素. 然后要求判断输入的是以下哪些图形 ...

  6. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  7. UVa 12118 nspector's Dilemma (构造+DFS+欧拉回路)

    题意:给定n个点,e条边和每条边的长度t,每两个点之间都有路相连,让你求一条最短的路经过这e条边. 析:刚开始想到要判连通,然后把相应的几块加起来,但是,第二个样例就不过,后来一想,那么有欧拉回路的还 ...

  8. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

  9. 【dfs判负环】BZOJ1489: [HNOI2009]最小圈

    Description 找出一个平均边权最小的圈. Solution 经典问题,二分答案判断有无负环. 但数据范围大,普通spfa会超时,于是用dfs判负环(快多了). 思路是dis设为0,枚举每个点 ...

随机推荐

  1. hdu1856 More is better 基础并查集

    #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> ...

  2. :not 选择器

  3. PHP函数技巧篇

    可变参数 Php提供3个函数用于检索在函数中所传递的参数. $array = func_get_args(); //返回一个提供给函数的所有参数的数组 $count = func_num_args() ...

  4. SpringCloud开发学习总结(七)—— 声明式服务调用Feign(二)

    参数绑定 在上一章的示例中,我们使用Spring Cloud Feign实现的是一个不带参数的REST服务绑定.然而现实系统中的各种业务接口要比它复杂得多,我们有时会在HTTP的各个位置传入各种不同类 ...

  5. 480 Sliding Window Median 滑动窗口中位数

    详见:https://leetcode.com/problems/sliding-window-median/description/ C++: class Solution { public: ve ...

  6. 单机版solr6.3和分布式solr6.3的安装部署

    一.单机版的solr部署 我的是在windows下安装的,linux同理 1. 安装JDK8,并配置好环境变量,一般我们经常开发的电脑上应该都有JDk了,所以这一步可以忽略. 2. 解压solr6.3 ...

  7. re正则表达式讲解—初步认识

    # f = open(r"C:\Users\LENOVO\Desktop\模特.txt",'r') # 1.常规提取文档内容方法 # contacts = [] # for i i ...

  8. poj2377 Bad Cowtractors

    思路: 最大生成树. 实现: #include <iostream> #include <cstdio> #include <vector> #include &l ...

  9. KendoUI Grid Pager部分 Nan-Nan of x Items

    相关问题: http://stackoverflow.com/questions/23941065/pager-error-in-kendo-gridnan-nan-of-1-items http:/ ...

  10. swift的static和class修饰符---What is the difference between static func and class func in Swift?

    Special Kinds of Methods Methods associated with a type rather than an instance of a type must be ma ...