Bees are one of the most industrious insects. Since they collect nectarand pollen from flowers, they
have to rely on the trees in the forest. For simplicity they numbered the n trees from 0 to n − 1. Instead
of roaming around all over the forest, they use a particular list of paths. A path is based on two trees,
and they can move either way i.e. from one tree to another in straight line. They don’t use paths that
are not in their list.
As technology has been improved a lot, they also changed their working strategy. Instead of hovering
over all the trees in the forest, they are targeting particular trees, mainly trees with lots of flowers.
So, they planned that they will build some new hives in some targeted trees. After that they will only
collect their foods from these trees. They will also remove some paths from their list so that they don’t
have to go to a tree with no hive in it.
Now, they want to build the hives such that if one of the paths in their new list go down (some
birds or animals disturbs them in that path) it’s still possible to go from any hive to another using the
existing paths.
They don’t want to choose less than two trees and as hive-building requires a lot of work, they need
to keep the number of hives as low as possible. Now you are given the trees with the paths they use,
your task is to propose a new bee hive colony for them.
Input
Input starts with an integer T (T ≤ 50), denoting the number of test cases.
Each case starts with a blank line. Next line contains two integers n (2 ≤ n ≤ 500) and m
(0 ≤ m ≤ 20000), where n denotes the number of trees and m denotes the number of paths. Each of
the next m lines contains two integers u v (0 ≤ u, v < n, u ̸= v) meaning that there is a path between
tree u and v. Assume that there can be at most one path between tree u to v, and needless to say that
a path will not be given more than once in the input.
Output
For each case, print the case number and the number of beehives in the proposed colony or ‘impossible’
if its impossible to find such a colony.
NOTE: Dataset is huge. Use faster I/O methods.
Sample Input
3
3
0
1
2
3
1
2
0
2 1
0 1
5
0
1
1
2
0
3
6
1
2
3
3
4
4
Sample Output
Case 1: 3
Case 2: impossible
Case 3: 3

#include <cstdio>
#include <cstring>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = ;
const int M = ; vector<int> g[N];
int vis[N], dis[N], pre[N];
queue<int> que; int ans;
void bfs(int st) {
while(!que.empty()) que.pop();
memset(vis, , sizeof vis);
memset(dis, INF, sizeof dis);
memset(pre, -, sizeof pre);
dis[st] = ;
vis[st] = ;
que.push(st); while(!que.empty()) {
int u = que.front(); que.pop();
int sx = g[u].size();
for(int i = ; i < sx; ++i) {
int v = g[u][i];
if(v == pre[u]) continue;
if(!vis[v]) {
vis[v] = ;
dis[v] = dis[u] + ;
que.push(v);
pre[v] = u;
}else {
ans = min(ans, dis[u] + dis[v] + );
}
}
}
}
int main() {
//freopen("in", "r", stdin);
int _, cas = ; scanf("%d", &_);
while(_ --) {
int n, m; scanf("%d%d", &n, &m);
int u, v;
for(int i = ; i <= n; ++i) g[i].clear();
for(int i = ; i < m; ++i) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
ans = INF;
for(int i = ; i < n; ++i) {
bfs(i);
}
printf("Case %d: ", cas++);
if(ans == INF) puts("impossible");
else printf("%d\n", ans);
}
return ;
}

题意:给出一个无向图,n<=500&&m<=20000, 求一个最小环
思路:枚举起点s,bfs出从s到每个点的距离dis, 对于当前边u,v,如果v被访问过了,且上次v被访问不是通过u,即v != pre[u],那么res = dis[u] + dis[v] + 1

但是注意,枚举的点不一定在环里面,且真正形成的环的长度也是小于等于res的,比如4个点,4条边, 0-1,1-2,1-3,2-3,当从0点bfs时,得到的备选res=5,但环的长度是3,
因为我们是枚举每一个点,最终一定能得到最小环

UVA 12544 - Beehives O(nm) 无向图最小环的更多相关文章

  1. POJ 1734 无向图最小环/有向图最小环

    给定一张图,求图中一个至少包含三个点的环,环上的点不重复,并且环上的边的长度之和最小. 点数不超过100个 输出方案 无向图: /*Huyyt*/ #include<bits/stdc++.h& ...

  2. uva 12544 无向图最小环

    思路:这题的N有500,直接floyd肯定超时. 我的做法是每次枚举一个点,求出包含这个点的最小环. 对所有最小环取最小值.求包含某个点的最小环我用的是启发式搜索,先以该点求一次spfa,然后dfs解 ...

  3. FZU 2090 旅行社的烦恼 floyd 求无向图最小环

    题目链接:旅行社的烦恼 题意是求无向图的最小环,如果有的话,输出个数,并且输出权值. 刚刚补了一发floyd 动态规划原理,用了滑动数组的思想.所以,这个题就是floyd思想的变形.在k从1到n的过程 ...

  4. 【POJ1734】Sightseeing Trip 无向图最小环

    题目大意:给定一个 N 个顶点的无向图,边有边权,如果存在,求出该无向图的最小环,即:边权和最小的环,并输出路径. 题解:由于无向图,且节点数较少,考虑 Floyd 算法,在最外层刚开始遍历到第 K ...

  5. 图论:Floyd-多源最短路、无向图最小环

    在最短路问题中,如果我们面对的是稠密图(十分稠密的那种,比如说全连接图),计算多源最短路的时候,Floyd算法才能充分发挥它的优势,彻彻底底打败SPFA和Dijkstra 在别的最短路问题中都不推荐使 ...

  6. 「LOJ#10072」「一本通 3.2 例 1」Sightseeing Trip(无向图最小环问题)(Floyd

    题目描述 原题来自:CEOI 1999 给定一张无向图,求图中一个至少包含 333 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方 ...

  7. POJ 1734 Sightseeing trip(无向图最小环+输出路径)

    题目链接 #include <cstdio> #include <string> #include <cstring> #include <queue> ...

  8. uva 796 Critical Links(无向图求桥)

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

  9. USACO4.13Fence Loops(无向图最小环)

    最近脑子有点乱 老是不想清楚就啪啪的敲 敲完之后一看 咦..样例都过不去 仔细一想 这样不对啊 刚开始就写了一SPFA 最后发现边跟点的关系没处理好 删了..写dfs..还是没转化好 开始搜解题方法 ...

随机推荐

  1. (转)为什么所有浏览器的userAgent都带Mozilla

    转自:http://www.eamonning.com/blog/view/289 以下是全文 最早的时候有一个浏览器叫NCSA Mosaic,把自己标称为NCSA_Mosaic/2.0 (Windo ...

  2. php中session锁--如何防止阻塞请求(译)

    现代浏览器限制到一个host并发连接的数量一般为4或6.这意味着,如果您的web页面加载几十个来自同一个host的assert file(js.图像.css)时,由于并发数的限制,会产生排队.同样甚至 ...

  3. 将javascript函数写在Html标签里

    有些时候不想把函数写在script标签里了,想直接在html标签里直接加上js代码,可以这样写: <body onload="javascript:{window.location.h ...

  4. MySQL 调优/优化的 100 个建议

    MySQL 调优/优化的 100 个建议   MySQL是一个强大的开源数据库.随着MySQL上的应用越来越多,MySQL逐渐遇到了瓶颈.这里提供 101 条优化 MySQL 的建议.有些技巧适合特定 ...

  5. windows7下修改hosts文件无效解决办法(转)

    通常会为了开发方便.或者屏蔽掉一些恶意网站,我们会在hosts(c:\windows\system32\drivers\etc\hosts)文件中进行相应的域名指向,例:

  6. [NHibernate]并发控制

    目录 写在前面 文档与系列文章 并发控制 乐观并发控制(Optimistic Concurrency) 一个例子 悲观并发控制(Pessimistic Concurrency) 总结 写在前面 上篇文 ...

  7. javascript parseJSON

    解析json: 前台和后台做ajax交互,后台返回的json字符串,我之前都是通过eval来解析,后来慢慢的知道eval这货是魔鬼,eval要尽量避免,是出于安全考虑,因为eval过于强大,他可以把s ...

  8. Digital calculation

    No1=1 No2=2 1. let result=No1+No2   let No1++    let No1+=3 2. result=$[No1+No2] 3. result=$((No1+No ...

  9. (原)android的alertdialog中加入edittext但是不弹出软键盘等问题的解决与原因

    摘要:alertdialog中加入edittext但是不弹出软键盘等问题网上有很多不管用的解决方案, 本文意在给出更有效的解决办法,并初步探究其原因 正文 在对话框中插入文本框是十分常见的需求 通常我 ...

  10. SQL Server服务器上需要导入Excel数据的必要条件

    SQL Server服务器上需要导入Excel数据,必须安装2007 Office system 驱动程序:数据连接组件,或者Access2010的数据库引擎可再发行程序包,这样就不必在服务器上装Ex ...