zoukankan      html  css  js  c++  java
  • HDU 1285 确定比赛名次

    拓扑排序模板题,判断 当前点出度与入度。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 
     5 struct N
     6 {
     7     int data;
     8     int n;
     9     struct N *next;
    10 }*head[510];
    11 
    12 void init(int n)
    13 {
    14     for(int i = 1;i <= n; i++)
    15     {
    16         head[i] = (struct N *)malloc(sizeof(struct N));
    17         head[i]->next = NULL;
    18         head[i]->n = 0;
    19         head[i]->data = -1;
    20     }
    21 }
    22 
    23 int link(int u,int v)
    24 {
    25     struct N *p1,*p2;
    26     for(p1 = head[u],p2 = p1->next; p2 != NULL; p1 = p1->next,p2 = p2->next)
    27     {
    28         if(v == p2->data) return 0;
    29         if(p1->data < v && p2->data > v)
    30         {
    31             N *p = (struct N *)malloc(sizeof(struct N));
    32             p->data = v;
    33             p->next = p1->next;
    34             p1->next = p;
    35             return 1;
    36         }
    37     }
    38     N *p = (struct N *)malloc(sizeof(struct N));
    39     p->data = v;
    40     p->next = p1->next;
    41     p1->next = p;
    42     return 1;
    43 }
    44 
    45 void check(int i)
    46 {
    47     for(N *p = head[i]->next;p != NULL;p = p->next)
    48     {
    49         head[p->data]->n--;
    50     }
    51 }
    52 
    53 int q[510],j,hash[510];
    54 
    55 int main()
    56 {
    57     int n,m,u,v,i;
    58     while(scanf("%d %d",&n,&m) != EOF)
    59     {
    60         init(n);
    61         memset(hash,0,sizeof(hash));
    62         for(i = 0;i < m; i++)
    63         {
    64             scanf("%d %d",&u,&v);
    65             if(link(u,v))
    66                 head[v]->n++;
    67         }
    68         for(i = 1,j = 0;i <= n && j < n; i++)
    69         {
    70             if(head[i]->n == 0  && !hash[i])
    71             {
    72                 check(i);
    73                 hash[i] = 1;
    74                 q[j++] = i;
    75                 i = 0;
    76             }
    77         }
    78         printf("%d",q[0]);
    79         for(i = 1;i < j; i++)
    80             printf(" %d",q[i]);
    81         printf("\n");
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    设计模式
    显示WiFi密码
    05-变量
    04-杂谈
    03-杂谈
    02-杂谈
    01-linux介绍、命令
    14-python--inner
    13-python--bibao
    11-python-iterator
  • 原文地址:https://www.cnblogs.com/zmx354/p/3031434.html
Copyright © 2011-2022 走看看