codevs 2833 奇怪的梦境

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

裸拓扑排序

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int map[10000][10000];
int stack[100000],indgr[100000],outdgr[100000];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        map[x][y] = 1;
        indgr[y] ++;
        outdgr[x] ++;
    }
    int top=0,temp;
    for(int i=1;i<=n;i++)
        if(indgr[i] == 0)
            stack[++top] = i;
    while(top > 0)
    {
        temp=stack[top--];
        for(int j=1;j<=n;j++)
            if(map[temp][j])
            {
                indgr[j] --;
                if(indgr[j] == 0)
                    stack[++top] = j;
            }
    }
    int tot=0;
    for(int i=1;i<=n;i++)
        if(indgr[i] != 0)
            tot++;
    if(tot == 0)printf("o(∩_∩)o");
    else    printf("T_T
%d",tot);
    return 0;
}
/*
3 3
1 2
2 3
3 2

*/
原文地址:https://www.cnblogs.com/kuaileyongheng/p/6723159.html