题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26898

思路:我们可以给定有直接边相连的两点的距离为1,那么就是求源点出发能够走偶数步的所有的点的个数。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAXN 111 int n,m;
vector<vector<int> >g;
bool even[MAXN],odd[MAXN]; void bfs()
{
memset(even,false,sizeof(even));
memset(odd,false,sizeof(odd));
queue<int>que;
que.push();
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=;i<g[u].size();i++){
int v=g[u][i];
int flag=;
if(!odd[v]&&(u==||even[u])){
odd[v]=true;
flag=;
}
if(odd[u]&&!even[v]){
even[v]=true;
flag=;
}
if(flag)que.push(v);
}
}
} int main()
{
int _case,u,v,ans,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
g.clear();
g.resize(n+);
while(m--){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
bfs();
ans=;
for(int i=;i<=n;i++){
if(even[i])ans++;
}
printf("Case %d: %d\n",t++,ans);
}
return ;
}

loj 1185(bfs)的更多相关文章

  1. loj 1377 (bfs)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1377 思路:这道题只要处理好遇到"*"这种情况就可以搞定了.我们可 ...

  2. loj 1165(bfs+康托展开)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...

  3. loj 1055(bfs)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26772 思路:注意判重就行,开个6维数组记录3个robots的位置 ...

  4. loj 1426(dfs + bfs)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1426 思路:首先我们预处理出每一个"*"在某一方向上最终能到达的位 ...

  5. loj 1046(bfs)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26766 思路:由于数据不是很大,我们可以枚举骑士最后聚集的位置,然 ...

  6. LOJ 3057 「HNOI2019」校园旅行——BFS+图等价转化

    题目:https://loj.ac/problem/3057 想令 b[ i ][ j ] 表示两点是否可行,从可行的点对扩展.但不知道顺序,所以写了卡时间做数次 m2 迭代的算法,就是每次遍历所有不 ...

  7. 「LOJ#6121」「网络流 24 题」孤岛营救问题(BFS

    题目描述 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克得到了迷宫的地形图.迷宫的外形是一个长方形 ...

  8. LOJ #2877. 「JOISC 2014 Day2」交朋友 并查集+BFS

    这种图论问题都挺考验小思维的. 首先,我们把从 $x$ 连出去两条边的都合并了. 然后再去合并从 $x$ 连出去一条原有边与一条新边的情况. 第一种情况直接枚举就行,第二种情况来一个多源 bfs 即可 ...

  9. LOJ #2876. 「JOISC 2014 Day2」水壶 BFS+最小生成树+倍增LCA

    非常好的一道图论问题. 显然,我们要求城市间的最小生成树,然后查询路径最大值. 然后我们有一个非常神的处理方法:进行多源 BFS,处理出每一个城市的管辖范围. 显然,如果两个城市的管辖范围没有交集的话 ...

随机推荐

  1. IIS 7.5 配置10W高并发

    原文:    http://www.myhack58.com/Article/sort099/sort0100/2012/35585.htm 原文:   http://www.myhack58.com ...

  2. Java&.Net虚拟机精简(GreenJVM&GreenDotNet发布) .

    精简JRE体积的小工具:http://blog.csdn.net/cping1982/archive/2008/09/02/2865198.aspx 项目地址:http://code.google.c ...

  3. iOS-UIView category

    UIView+Extension.h #import <UIKit/UIKit.h> @interface UIView (Extension) @property (nonatomic, ...

  4. POP3,全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”

    POP3 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . POP3,全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”.是TCP/IP ...

  5. malloc 函数到底做了什么?

    请看下面的代码. 猜测结果是什么?编译通过吗? #include <stdio.h> #include <stdlib.h> int main() { ; char *ptr ...

  6. Array和ArrayList互相转换

    class Order{ public string orderId; public string orderName; public decimal orderPrice; } public cla ...

  7. PHP - 获取和设置include_path .

    PHP - 获取和设置include_path 分类:             PHP              2011-02-16 13:19     2818人阅读     评论(1)     ...

  8. petri网学习心得

    本文转载自duxw,如给您带来不便之处,请联系博主. 1.Petri网书籍:<petri网导论>,吴哲辉 非常适合初学者.概念清晰,容易理解. 2.工作流书籍:<Workflow M ...

  9. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...