codevs 1506 传话
http://codevs.cn/problem/1506/
一个朋友网络,如果a认识b,那么如果a第一次收到某个消息,那么会把这个消息传给b,以及所有a认识的人。
如果a认识b,b不一定认识a。
所有人从1到n编号,给出所有“认识”关系,问如果i发布一条新消息,那么会不会经过若干次传话后,这个消息传回给了i,1<=i<=n。
第一行是n和m,表示人数和认识关系数。
接下来的m行,每行两个数a和b,表示a认识b。1<=a, b<=n。认识关系可能会重复给出,但一行的两个数不会相同。
一共n行,每行一个字符T或F。第i行如果是T,表示i发出一条新消息会传回给i;如果是F,表示i发出一条新消息不会传回给i。
4 6
1 2
2 3
4 1
3 1
1 3
2 3
T
T
T
F
n<=1000
1<=a, b<=n
分析:
广搜,因为如果搜索到某层K,如果不是由i层转回到i层的一条回路,而是一条死路,那么以后必然也不用搜索这层了。
AC代码:
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma warning(disable:4786) using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = 1e3 + ;
const double eps = 1e-;
const double PI = acos(-1.0); bool f[MAX][MAX];
bool mark[MAX];
int n,m,a,b,flag,i; void bfs(int start)
{
queue<int >q;
int p;
q.push(start);
while(!q.empty())
{
int temp=q.front(); q.pop();
for(p=;p<=n;p++)
{
if(f[temp][p]&&!mark[p])
{
mark[p]=;
q.push(p);
}
if(mark[i])
{
flag=;
return ;
}
}
}
} int main()
{
cin>>n>>m; memset(f,,sizeof(f));
for(i=;i<m;i++) scanf("%d%d",&a,&b), f[a][b]=; for(i=;i<=n;i++)
{
memset(mark,,sizeof(mark)); flag=;
bfs(i);
if(flag)
printf("T\n");
else
printf("F\n");
}
return ;
}
看了题解,发现有人用Floyd ,主要是数据量小,orz
AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
int f[][];
int n,m,x,y;
using namespace std;
int main()
{
scanf("%d%d",&n,&m);
memset(f , , sizeof(f));
for(int i = ;i <= m;++ i){
scanf("%d%d",&x,&y);
f[x][y] = ;
}
for(int k = ;k <= n;++ k)
for(int i = ;i <= n;++ i)
for(int j = ;j <= n;++ j)
if(f[i][k] == && f[k][j] == )
f[i][j] = ;
for(int i = ;i <= n;++ i)
if(f[i][i] == )
cout<<"T"<<endl;
else
cout<<"F"<<endl;
return ;
}
codevs 1506 传话的更多相关文章
- Codevs 1506 传话(floyd大法好)。
1506 传话 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 一个朋友网络,如果a认识b,那么如果a第 ...
- code vs 1506 传话
codevs 1506 传话(时间限制: 1 s 空间限制: 128000 KB) 题目描述 Description 一个朋友网络,如果a认识b,那么如果a第一次收到某个消息,那么会把这个消息传给b, ...
- AC日记——传话 codevs 1506 (tarjan求环)
1506 传话 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 一个朋友网络,如果a认识b,那么如果a第 ...
- 1506 传话 (暴力DFS或者Tarjan模板题)
题目描述 Description 一个朋友网络,如果a认识b,那么如果a第一次收到某个消息,那么会把这个消息传给b,以及所有a认识的人. 如果a认识b,b不一定认识a. 所有人从1到n编号,给出所有“ ...
- 题解——code[vs] 1506 传话(传递闭包)
裸的传递闭包 直接Floyd暴力即可 #include <cstdio> #include <algorithm> #include <cstring> using ...
- codevs 搜索题汇总(青铜+白银级)
1792 分解质因数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 编写一个把整数N分解为质因数乘积的程序. 输入描 ...
- code vs1506传话(塔尖)+tarjan图文详解
1506 传话 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 一个朋友网络,如果a认识b,那么如果a第一次收到 ...
- codevs 3289 花匠
题目:codevs 3289 花匠 链接:http://codevs.cn/problem/3289/ 这道题有点像最长上升序列,但这里不是上升,是最长"波浪"子序列.用动态规划可 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
随机推荐
- android studio 编程中用到的快捷键
1.Ctrl+Alt+T可以把代码包在一块内,例如try/catch Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:0000000111 EndF ...
- docker nexus oss
docker login/search x.x.x.x:8081 sonatype/docker-nexus Docker images for Sonatype Nexus with the Ora ...
- Python的运行
1.在命令行中运行 2.使用shell(IDLE) 3.新建.py脚本 只要是编辑器都可以 4.脚本在IDLE中运行 5.在windows下的cmd下运行脚本
- C++ 线程类的一个实现
.h #ifndef CTHREAD_H_ #define CTHREAD_H_ #include "plat.h" class CThread { public: enum { ...
- ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'
ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click ...
- Cocos2d-JS切换场景与切换特效
var HelloWorldLayer = cc.Layer.extend({ sprite:null, ctor:function () { //////////////////////////// ...
- android动态调试samli代码(转)
转载自看雪http://bbs.pediy.com/showthread.php?t=189610,非常感谢原作者分享! 跟踪apk一般的做法是在反编译的smali代码中插入log输出,然后重新编译运 ...
- JQuery中国省市区无刷新三级联动查询
之前有写过用<Ajax控件来实现中国的省市区无刷新查询> 今天用JQuery来实现,用Ajax控件和JQuery的优缺点就先不说了. 效果图如下: 下面来结合代码来详细说明一下如何用JQu ...
- MongoDB非正常关闭后修复记录
启动mongodb时出现如下错误: 根据提示可以知道错误原因是mongodb非正常关闭,此时需要对数据库进行修复.修复命令:mongod --repair 或 ./mongod --repair , ...
- C#中数组、ArrayList和List三者的区别
在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. 数组 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. ...