A - Promotions
题目详见http://7xjob4.com1.z0.glb.clouddn.com/3f644de6844d64706eb36baa3a0c27b0
这题是普通的拓扑排序,要把每一层的都保存下来。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define MOD 1000000007
struct edge{
int to,next;
}e[2*20050];
int first[5050],du[5050];
int q[1111111];
int n,m;
int ceng[5050][5050],num[5050],deep[5050],cengshu,sum[5050];
void topu()
{
int i,j;
int front,rear;
front=1;rear=0;
memset(num,0,sizeof(num));
memset(deep,0,sizeof(deep));
for(i=1;i<=n;i++){
if(!du[i]){
rear++;
q[rear]=i;
deep[i]=1;
}
}
int x,y,xx,yy;
cengshu=1;
while(front<=rear){
x=q[front];
front++;
cengshu=max(cengshu,deep[x]);
num[cengshu]++;
ceng[cengshu ][num[cengshu] ]=x;
for(i=first[x];i!=-1;i=e[i].next){
int v=e[i].to;
du[v]--;
if(!du[v]){
rear++;
q[rear]=v;
deep[v]=deep[x]+1;
}
}
}
sum[0]=0;
for(i=1;i<=cengshu;i++){
sum[i]=sum[i-1]+num[i];
}
}
int main()
{
int i,j,l,r,tot,c,d;
while(scanf("%d%d%d%d",&l,&r,&n,&m)!=EOF)
{
memset(first,-1,sizeof(first));
memset(du,0,sizeof(du));
tot=0;
for(i=1;i<=m;i++){
scanf("%d%d",&c,&d);
c++;d++;
du[d]++;
tot++;
e[tot].next=first[c];e[tot].to=d;
first[c]=tot;
}
topu();
int num1,num2,num3;
for(i=1;i<=cengshu;i++){
if(sum[i]>l)break;
}
i--;
num1=sum[i];
for(i=1;i<=cengshu;i++){
if(sum[i]>r)break;
}
i--;
num2=sum[i];
for(i=1;i<=cengshu;i++){
if(sum[i]>=r)break;
}
if(i>=cengshu)num3=0;
else num3=sum[cengshu]-sum[i];
printf("%d\n%d\n%d\n",num1,num2,num3);
}
return 0;
}