思路:拓补排序查找图中有无环
#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int M=10005;
int c[M],r[M],head[M];
int x,y,n,m,tot,u,v,t;
stack <int> q;
struct edge{
int from,to,val,next;
}e[100000];
void add(int i,int j)
{
e[t].to=j;
e[t].next=head[i];
head[i]=t++;
}
void init()
{
memset(head,-1,sizeof(head));
cin>>n>>m;
while(m--)
{
scanf("%d%d",&x,&y);
c[x]++;r[y]++;
add(x,y);
}
for(int i=1;i<=n;i++) if(r[i]==0) q.push(i);
}
void work()
{
while(!q.empty())
{
u=q.top();
q.pop();tot++;
for (int i=head[u];i!=-1;i=e[i].next)
{
v=e[i].to;r[v]--;
if(!r[v]) q.push(v);
}
}
if(tot<n){
printf("T_T\n");
printf("%d\n",n-tot);
}
else
printf("o(∩_∩)o\n");
}
int main()
{
init();
work();
return 0;
}