码迷,mamicode.com
首页 > 其他好文 > 详细

hdu-1704 Rank

时间:2014-09-20 10:07:07      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:闭包

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

有向图传递闭包。floyed的优化。

#include<cstdio>
#include<cstring>
int map[501][501],n;
void floyed()
{
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
        if(map[i][k])
        {
            for(int j=1;j<=n;j++)
            map[i][j]=map[i][j]||map[i][k]&&map[k][j];
        }
}
int main()
{
    int t,a,b,m,count;
    scanf("%d",&t);
    while(t--)
    {
       count=0;
       memset(map,0,sizeof(map));
       scanf("%d%d",&n,&m);
       while(m--)
       {
           scanf("%d%d",&a,&b);
           map[a][b]=1;
       }
       floyed();
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
            if(!(map[i][j]||map[j][i])) count++;
        printf("%d\n",count);
    }
    return 0;
}


 

 

hdu-1704 Rank

标签:闭包

原文地址:http://blog.csdn.net/u012773338/article/details/39404737

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!