HDOJ Is It A Tree?

直接通过判断出度和入度确定,这题的结束标志是两个负数(不一定是-1),输入中含有 0 0 这组数据(判为no),最后一组数据后面要留换行,否则PE;

http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=1304&pid=1003&ojid=0

View Code
# include <cstdio>
# include <cstring>

# define N 20

bool ok;
int in[N], out[N];

void init(void)
{
    ok = false;
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
}

int main()
{
    int x, y, icase = 0;
    bool IN = false;

    init();
    while (1)
    {
        scanf("%d%d", &x, &y);
        if (x<0 && y<0) break;
        if (x==0 && y==0)
        {
            ++icase;
                if (IN == false)
                {
                        printf("Case %d is not a tree.\n", icase);
                        continue;
                        }
            int tmp = 0;
            if (ok == false)
            {
                for (int i = 1; i < N; ++i) if (!in[i] && out[i])
                    if (tmp == 0) ++tmp;
                    else {ok = true; break;}
            }
            if (tmp == 0) ok = true;
            if (ok)
                printf("Case %d is not a tree.\n", icase);
            else
                printf("Case %d is a tree.\n", icase);
            init();
            IN = false;
        }
        else if (ok == false)
        {
                IN = true;
            if (x == y) ok = true;
            ++out[x], ++in[y];
            if (in[y]>1) ok = true;
        }
    }

    return 0;
}

posted on 2012-07-23 22:31  getgoing  阅读(149)  评论(0编辑  收藏  举报

导航