2833 奇怪的梦境

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
题目描述 Description

Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很多按钮,还有一个屏幕,上面显示了一些信息。屏幕上说,要将所有按钮都按下才能出去,而又给出了一些信息,说明了某个按钮只能在另一个按钮按下之后才能按下,而没有被提及的按钮则可以在任何时候按下。可是Aiden发现屏幕上所给信息似乎有矛盾,请你来帮忙判断。

输入描述 Input Description

第一行,两个数N,M,表示有编号为1...N这N个按钮,屏幕上有M条信息。

接下来的M行,每行两个数ai,bi,表示bi按钮要在ai之后按下。所给信息可能有重复,保证ai≠bi。

输出描述 Output Description

若按钮能全部按下,则输出“o(∩_∩)o”。

若不能,第一行输出“T_T”,第二行输出因信息有矛盾而无法确认按下顺序的按钮的个数。输出不包括引号。

样例输入 Sample Input

3 3

1 2

2 3

3 2

样例输出 Sample Output

T_T

2

数据范围及提示 Data Size & Hint

对于30%的数据,保证0<N≤100。

对于50%的数据,保证0<N≤2000。

对于70%的数据,保证0<N≤5000。

对于100%的数据,保证0<N≤10000,0<M≤2.5N。

思路:

  拓扑排序,两种写法

代码一:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int M = ;
int n,m,ans,now;
int ru[M],head[M];
queue<int>q; struct A{
int next,to;
}e[*M]; void add(int u,int v)
{
e[++now].to=v;
e[now].next=head[u];
head[u]=now;
} void topo()
{
for(int i=;i<=n;i++)
if(ru[i]==)
q.push(i);
while(!q.empty())
{
int u=q.front();
q.pop();
ans++;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].to;
ru[v]--;
if(ru[v]==)
q.push(v);
}
}
if(ans<n)
{
printf("T_T\n%d",n-ans);
return;
}
else
{
printf("o(n_n)o");
return;
}
} int main()
{
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
for(int i=,a,b;i<=m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
ru[b]++;
}
topo();
return ;
}

队列版

代码二:

#include <iostream>
#include <cstdio>
#include <queue> using namespace std; const int M = ;
int n,m,k,ans;
bool dad[M][M];
int ru[M]; int main()
{
scanf("%d%d",&n,&m);
for(int i=,a,b;i<=m;i++)
{
///b在a之前按下
scanf("%d%d",&a,&b);
if(!dad[a][b]) ru[b]++;
dad[a][b]=true;
}
for(int i=;i<=n;i++)
{
k=;///记得将k进行初始化,防止漏下找入度为零的点
while(k<=n && ru[k]!=) k++;///找入度为零的点
if(k<=n && ru[k]==)
{
ans++;///统计答案个数
ru[k]=-;///删边
for(int j=;j<=n;j++)
if(dad[k][j])
ru[j]--;///删边
}
}
if(ans==n) printf("o(n_n)o");
else printf("T_T\n%d\n",n-ans);
return ;
}

for循环版

Ps:如果让我进行选择,我会毫不犹豫的选择第一种,因为跑得快而且内存占用小!!!

codevs2833 奇怪的梦境 x的更多相关文章

  1. codevs2833 奇怪的梦境

    2833 奇怪的梦境  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Aiden陷入了一个奇怪的梦境:他被困 ...

  2. 奇怪的梦境(codevs 2833)

    题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很多按钮,还有一个屏幕,上面显示了一些信息.屏幕上说,要将所有按钮都按下才能出去,而又给出了一些信息,说明 ...

  3. Codevs 2833 奇怪的梦境

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很多按钮,还 ...

  4. 2833 奇怪的梦境 未AC

    2833 奇怪的梦境 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold         题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小 ...

  5. CODEVS——T 2833 奇怪的梦境

    http://codevs.cn/problem/2833/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  6. 【CODEVS】2833 奇怪的梦境

    2833 奇怪的梦境 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很 ...

  7. [wikioi]奇怪的梦境

    http://wikioi.com/problem/2833/ 拓扑排序,居然1A,哈哈. #include <cstdio> #include <iostream> #inc ...

  8. 【拓扑排序】CODEVS 2833 奇怪的梦境

    拓扑排序模板. #include<cstdio> #include<vector> #include<stack> using namespace std; #de ...

  9. 1、Codevs 必做:2833、1002、1003、2627、2599

    2833 奇怪的梦境  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Aiden陷入了一个奇怪的梦境:他被困 ...

随机推荐

  1. vue调用组件,组件回调给data中的数组赋值,报错Invalid prop type check failed for prop value. Expecte

    报错信息: 代码信息:调用一个tree组件,选择一些信息 <componentsTree ref="typeTreeComponent" @treeCheck="t ...

  2. 解决reportNG中文乱码(转:http://www.it610.com/article/3626590.htm)

    1.下载reportng源码      git clone  https://github.com/dwdyer/reportng.git 2.修改AbstractReporter.java      ...

  3. javascript 数据类型 undefined 和null

    数据类型 undefind null boolean number string object type of 功能:检测变量类型 语法:type of 变量或 type of (变量) consol ...

  4. 关于Eclipse及JDK安装过程中的一些问题

    一,环境变量的配置 1.配置CLASSPATH系统变量 CLASSPATH系统变量为类查找路径 ①.在使用javac进行编译时遇到import时候就会通过这个变量里面配置的路径去查找.如果配置的是目录 ...

  5. spring boot-4.配置文件

    官方文档的23.4章节介绍了关于配置文件的内容 springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的 ...

  6. 【转】mysql的group_concat函数,默认最大长度是1024

    mysql的group_concat函数,默认最大长度是1024 查询sql: show variables like 'group_concat_max_len'; 设置方式: 修改配置文件my.i ...

  7. sql中循环的存储过程

    ), a2 bigint, a3 bigint) returns void as $$declare ii integer; begin II:; FOR ii IN a2..a3 LOOP INSE ...

  8. PythonDay13

    第十三章 今日内容 匿名函数 内置函数二 闭包 匿名函数 匿名函数就是一行函数,关键字是lambda lambda x:x# lambda 参数:返回值x 是普通函数的形参 可以不定义形参:x 是 普 ...

  9. HTTP请求报文(请求行、请求头、请求体)

    HTTP协议 1.简介 HTTP协议(Hyper Text Transfer Protocol,超文本传输协议),是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...

  10. WebGPU学习(八):学习“texturedCube”示例

    大家好,本文学习Chrome->webgpu-samplers->texturedCube示例. 上一篇博文: WebGPU学习(七):学习"twoCubes"和&qu ...