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,
因为我们是枚举每一个点,最终一定能得到最小环

最新文章

  1. 使用 RequireJS 优化 Web 应用前端
  2. Yii源码阅读笔记(二十八)
  3. c#抽象类相关
  4. 【学习笔记】【C语言】自增自减
  5. JavaScript笔记 第十六章 匿名函数和闭包
  6. Filter过滤器(1)
  7. jQuery图片提示示例
  8. java类加载器行为[笔记]
  9. Nhibernate初入门基本配置(一)
  10. 高频(工作频率为13.56MHz)
  11. Android学习之Service(1)---&gt;Started方式
  12. 卷积神经网络的一些经典网络(Lenet,AlexNet,VGG16,ResNet)
  13. OrmLite-更符合面向对象的数据库操作方式
  14. MySql 8.0 C#连接报错 MySql.Data.MySqlClient.MySqlException (0x80004005): Authentication to host &#39;12.118.224.181&#39; for user &#39;root&#39; using method &#39;caching_sha2_password&#39; failed with message: Reading from t
  15. h5需要的浏览器插件
  16. Hadoop数据分析平台项目实战(基于CDH版本集群部署与安装)
  17. 停车场信息管理系统(C语言)
  18. 常见的压缩文件格式案例tarZ
  19. CSS3一些常用动画
  20. Win10系列:UWP界面布局进阶4

热门文章

  1. 【原创】自己动手写工具----签到器[Beta 1.0]
  2. bzoj1003 物流运输
  3. tyvj1013 找啊找啊找GF
  4. avl树的操作证明
  5. PHP通用的XSS攻击过滤函数,Discuz系统中 防止XSS漏洞攻击,过滤HTML危险标签属性的PHP函数
  6. Spring中的JdbcTemplate使用
  7. 第三次个人作业—“K米”评测
  8. SET基本数据类型
  9. jquery-常用的运行函数
  10. JS date常用代码积累