记录编号 259841 评测结果 AAAAAAAAAA
题目名称 [HZOI 2016]奖学金 最终得分 100
用户昵称 Gravatar洛克索耶夫 是否通过 通过
代码语言 C++ 运行时间 0.060 s
提交时间 2016-05-11 18:06:33 内存使用 0.58 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

int Read()
{
	int a=0,minus=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')    minus=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		a=a*10+ch-'0';
		ch=getchar();
	}
	return a*minus;
}

struct EDGE{
	int from;
	int to;
	int next;
	EDGE()
	{
		from=to=next=0;
	}
}edges[20010];

struct Node{
	int money;
	int in;
	Node()
	{
		money=100;in=0;
	}
}nodes[10010];

int GetMax(int a, int b)
{
	return a >b ? a : b;
}

int head[10010] = {0}, len = 0;

queue <int> Q;
//假设nodes,money=100,du

void BFS(){
	while(!Q.empty()){
		int t = Q.front();Q.pop();
		for(int i=head[t];i!=-1;i = edges[i].next){
			if( nodes[ edges[i].to ].money < nodes[t].money + 1 )
				nodes[ edges[i].to ].money = nodes[t].money + 1;
			nodes[edges[i].to].in--;
			if( nodes[ edges[i].to ].in == 0 )
				Q.push(edges[i].to);
		}
	}
}
void AddEdge(int from, int to)
{
	len++;
	edges[len].from = from;
	edges[len].to = to;
	edges[len].next = head[from];
	head[from] = len;
	nodes[to].in++;
}

int main()
{
	freopen("reward.in","r",stdin);
	freopen("reward.out","w",stdout);
	memset(head, -1, sizeof(head));
	int n=Read(),m=Read();
	for(int i=1;i<=m;i++){
		int from=Read();
		int to=Read();
		AddEdge(to,from);
	}
	for(int i=1;i<=n;i++){
		if(nodes[i].in==0)
			Q.push(i);//DFS(i);
		//入度为0,说明该同学奖学金最少
	}

	BFS();

	for(int i=1;i<=n;i++){
		if( nodes[i].in != 0 ){
			printf("impossible");
			return 0;
		}
	}

	int ans=0;
	for(int i=1;i<=n;i++){
		ans+=nodes[i].money;
	}
	printf("%d",ans);

	fclose(stdin);
	fclose(stdout);
	return 0;
}