#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 20010;
//ip表示第几条边
//indeg表示入度
int head[maxn], ip, indeg[maxn];
int n, m, seq[maxn];//seg表示要干的事
struct note
{
int v,next;
} edge[maxn];
void Init()
{
memset(head, -1, sizeof(head));
memset(indeg, 0, sizeof(indeg));
ip = 0;
}
void addedge(int u, int v)//增加边,u是初始点,v是终点
{
edge[ip].v = v;
edge[ip].next = head[u];
head[u] = ip++;
}
int topo()//拓扑排序
{
queue<int> q;
for(int i=1; i<=n; i++)
if(indeg[i] == 0)
q.push(i);
int k = 0;
bool res = false;
while(!q.empty())
{
if(q.size() != 1)
res = true;
int u = q.front();
q.pop();
k++;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].v;
indeg[v]--;
if(indeg[v] == 0)
{
//seq[v] = seq[u]+1;
q.push(v);
}
}
}
if(k < n) return -1;//No
if(res) return 0;//Only
return 1;//正常
}
时间: 2024-11-15 23:39:44