记录编号 259841 评测结果 AAAAAAAAAA
题目名称 [HZOI 2016]奖学金 最终得分 100
用户昵称 Gravatar洛克索耶夫 是否通过 通过
代码语言 C++ 运行时间 0.060 s
提交时间 2016-05-11 18:06:33 内存使用 0.58 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<queue>
  6. using namespace std;
  7.  
  8. int Read()
  9. {
  10. int a=0,minus=1;
  11. char ch=getchar();
  12. while(ch<'0'||ch>'9'){
  13. if(ch=='-') minus=-1;
  14. ch=getchar();
  15. }
  16. while(ch>='0'&&ch<='9'){
  17. a=a*10+ch-'0';
  18. ch=getchar();
  19. }
  20. return a*minus;
  21. }
  22.  
  23. struct EDGE{
  24. int from;
  25. int to;
  26. int next;
  27. EDGE()
  28. {
  29. from=to=next=0;
  30. }
  31. }edges[20010];
  32.  
  33. struct Node{
  34. int money;
  35. int in;
  36. Node()
  37. {
  38. money=100;in=0;
  39. }
  40. }nodes[10010];
  41.  
  42. int GetMax(int a, int b)
  43. {
  44. return a >b ? a : b;
  45. }
  46.  
  47. int head[10010] = {0}, len = 0;
  48.  
  49. queue <int> Q;
  50. //假设nodes,money=100,du
  51.  
  52. void BFS(){
  53. while(!Q.empty()){
  54. int t = Q.front();Q.pop();
  55. for(int i=head[t];i!=-1;i = edges[i].next){
  56. if( nodes[ edges[i].to ].money < nodes[t].money + 1 )
  57. nodes[ edges[i].to ].money = nodes[t].money + 1;
  58. nodes[edges[i].to].in--;
  59. if( nodes[ edges[i].to ].in == 0 )
  60. Q.push(edges[i].to);
  61. }
  62. }
  63. }
  64. void AddEdge(int from, int to)
  65. {
  66. len++;
  67. edges[len].from = from;
  68. edges[len].to = to;
  69. edges[len].next = head[from];
  70. head[from] = len;
  71. nodes[to].in++;
  72. }
  73.  
  74. int main()
  75. {
  76. freopen("reward.in","r",stdin);
  77. freopen("reward.out","w",stdout);
  78. memset(head, -1, sizeof(head));
  79. int n=Read(),m=Read();
  80. for(int i=1;i<=m;i++){
  81. int from=Read();
  82. int to=Read();
  83. AddEdge(to,from);
  84. }
  85. for(int i=1;i<=n;i++){
  86. if(nodes[i].in==0)
  87. Q.push(i);//DFS(i);
  88. //入度为0,说明该同学奖学金最少
  89. }
  90.  
  91. BFS();
  92.  
  93. for(int i=1;i<=n;i++){
  94. if( nodes[i].in != 0 ){
  95. printf("impossible");
  96. return 0;
  97. }
  98. }
  99.  
  100. int ans=0;
  101. for(int i=1;i<=n;i++){
  102. ans+=nodes[i].money;
  103. }
  104. printf("%d",ans);
  105.  
  106. fclose(stdin);
  107. fclose(stdout);
  108. return 0;
  109. }
  110.