12118 - Inspector's Dilemma
来源:互联网 发布:养树的软件 编辑:程序博客网 时间:2024/05/18 01:03
结题思路: 首先只管需要经过的边,这样得到一个可能具有几个连通块的图,注意题中任意两点之间都有一条边。这样要想走完所有的边,则必须加上一定量的边使得图变成连通图,这个边数就是连通块的个数t-1 ; 然后对于每个连通块,只要加上一定的边使其变成哈密顿图就可以了,在此题中就是统计每个连通块中的边数(这里使用连通块点的度数之和就可以获得),以及奇数点的个数。这样就可以根据哈密顿图的条件,消灭一定量的奇数度的点(消灭的方法就是加边)。 需要加的边数加上原来连通图的边数就是观察员在每个每个连通块需要走的边数。
// main.cpp// uva 12118 Inspector’s Dilemma//// Created by XD on 15/7/19.// Copyright (c) 2015年 XD. All rights reserved.//#include <iostream>#include <string>#include <queue>#include <stack>#include <stdio.h>#include <stdlib.h>#include <math.h>#include<vector>#include <string.h>#include <string>#include <algorithm>#include <set>#include <map>#include <cstdio>using namespace std ;int flag[1010] ;vector<int> g[1010] ;int d[1010] ;set<int> node ;int asn ;int conn ;int v , e,t ;int bfs(int x ){ if (flag[x]==1) { return 0 ; } conn++ ; int d1= 0 ; queue<int > q ; flag[x] = 1 ; if (d[x]%2==1) { d1++ ; } q.push(x) ; int s ,to; ; int d2 = d[x] ; while (!q.empty()) { s = q.front() ; q.pop() ; long len = g[s].size() ; for (int i = 0 ; i < len; i++) { to = g[s][i] ; if (flag[to]== 0) { d2 += d[to] ; flag[to] = 1; q.push(to) ; if (d[to]%2==1) { d1++ ; } } } } if (d1==2 ||d1== 0) { return d2/2 ; ; } else { return d2/2+ (d1 - 2)/2 ; }}int main() { int x , y ; int casenum = 0 ; while (scanf("%d%d%d" ,&v ,&e,&t)==3 && v + e + t != 0 ) { node.clear() ; asn = 0 ; conn = 0 ; memset(flag, 0, sizeof(flag)) ; memset(d, 0, sizeof(d)) ; for (int i = 0; i <= v; i++) { g[i].clear() ; } for (int i = 0 ; i < e; i++) { scanf("%d%d" , &x,&y) ; g[x].push_back(y) ; g[y].push_back(x) ; d[x]++ ; d[y]++ ; node.insert(x) ; node.insert(y) ; } for (set<int >::iterator it = node.begin() ; it!= node.end() ;it++) { if (flag[*it] == 1) { continue ; } asn += bfs(*it) ; } int temp = 0 ; if (conn !=0 ) { temp = 1 ; } printf("Case %d: %d\n" , ++casenum, (asn + conn - temp)*t) ; } return 0;}
0 0
- 12118 - Inspector's Dilemma
- 12118 - Inspector's Dilemma
- UVa 12118 Inspector's Dilemma
- uva 12118 Inspector's Dilemma
- Uva-12118 Inspector's Dilemma
- UVa 12118 Inspector's Dilemma
- UVA 12118(p181)----Inspector's Dilemma
- bnu Inspector's Dilemma
- UVa12118:Inspector's Dilemma
- Inspector's Dilemma UVA
- Inspector's Dilemma UVA
- UVa 12118 - Inspector's Dilemma <欧拉道路+DFS>
- UVa 12118 Inspector's Dilemma (欧拉道路)
- Uva12118——Inspector's Dilemma
- UVa 12118 - Inspector's Dilemma(DFS判连通+欧拉回路)
- 习题 6-14 UVA - 12118 Inspector's Dilemma 检察员的难题 (DFS 构造欧拉通路)
- 习题6-14 检查员的难题(Inspector's Dilemma, ACM/ICPC Dhaka 2007, UVa12118)
- Women's Dilemma
- Web Crawling and Data Miniing with Apache Nutch(翻译+学习心得)_01
- Spring MVC 框架搭建及详解
- decode和encode
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记37 TableView Delegate
- 排座位
- 12118 - Inspector's Dilemma
- LabView随机生成二维数组
- iOS 重写 isequal方法需重写hash方法
- Simulink载入图像并显示
- 证明2D-FFT能够拆分成两个1D-FFT
- SIMULINK:两个1D FFT构建2D-FFT
- 利用一维FFT查看正弦波频谱信息
- XILINX SYSTEM GENERATOR: THE FIRST PROJECT
- MATLAB中统计程序执行时间