HDU 1285 确定比赛名次

http://acm.hdu.edu.cn/showproblem.php?pid=1285

 

好久好久没编程了。。。。几个月了。。。。感觉很费劲!基本思想很容易。。。。这里对输入的处理比较关键。。。。因为对于其中2位对手来说。。

只有三种情况(通过多次比赛后最终的结果)。。。还有n==1的时候。。。下面的break漏了会WA。。。

 

#include <iostream>

using namespace std;



int g[505][505];

int ingree[505];

int main()

{

    

    int n, m;

    

    while(~scanf("%d%d", &n, &m))

    {

        memset(g, 0, sizeof(g));

        memset(ingree, 0, sizeof(ingree));

        

        

        while (m--)

        {

            int a, b;

            scanf("%d%d", &a, &b);

            if (g[a][b] == 0 && g[b][a] == 0)//处理那3种情况

            {

                g[a][b] = 1;

                ingree[b]++;

            }

            else

            {

                if (g[a][b] == 0 && g[b][a] == 1)

                {

                    g[b][a] = 0;

                    ingree[a]--;

                }

                

            }

        }

        if (n == 1)//n == 1的情况

        {

            puts("1");

        }

        else

        {

            int count = 0;

            for (int cnt = 1; cnt <= n; cnt++)

            {

                for (int i = 1; i <= n; i++)

                {

                    if (ingree[i] == 0)

                    {

                        ingree[i] = -1;

                        printf(count == 0 ? "%d" : " %d", i);

                        count++;

                        for (int j = 1; j <= n; j++)

                        {

                            if (g[i][j] == 1)

                            {

                                ingree[j]--;

                            }

                        }

                        break;//每轮只扫一遍,所以不能漏

                    }

                    

                }

            }

            puts("");

        }

        

    }

    return 0;

}

 

你可能感兴趣的:(HDU)