Chieh's Blog

Because Of Coding

HDU 5154 Harry and Magical Computer

Chieh posted @ 2015年1月03日 22:17 in BestCoder , 265 阅读

飞机票: http://acm.hdu.edu.cn/showproblem.php?pid=5154

很好理解的题。。。

题目分析:最多100个点 跑一边floyd 看是否有环 复杂度 0(n^3)。一个trick就是自己到自己有环也是NO

/*
Author:Chieh
Grow up happy
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define LL long long
using namespace std;
const int maxn=123;
int ai[maxn][maxn];
int n,m;
void init()
{
    int u,v;
    memset(ai,0,sizeof(ai));
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d",&u,&v);
        ai[u][v]=1;
    }
}
void play()
{
    for(int k=1; k<=n; k++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(ai[i][k]&&ai[k][j])
                {
                    ai[i][j]=1;
                }
            }
        }
    }   
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(ai[i][j]&&ai[j][i])
            {
                printf("NO\n");
                return;
            }
        }
    }

    printf("YES\n");
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        play();
    }
    return 0;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter