hdu1325(Is it a tree)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

const int maxn = 110000;
const int maxm = 110000;
using namespace std;

typedef struct node {
    int to;
    int next;
}node;
node edge[maxm];
int head[maxn];
int ednum = 1;
int s = 0, t = 0;
int que[maxm+maxm];
int flag[maxn];
int indeg[maxn];
vector<int> v;

int init() {
    v.clear();
    memset(head, 0, sizeof(head));
    memset(indeg, 0, sizeof(indeg));
    memset(flag, 0, sizeof(flag));
    ednum = 1;
    while (1) {
        scanf("%d%d", &s, &t);
        if (s<0 && t<0) {
            return -1;
        }
        if (s==0 && t==0) {
            return 1;
        }
        if (!flag[s]) {
            flag[s] = 1;
            v.push_back(s);
        }
        if (!flag[t]) {
            flag[t] = 1;
            v.push_back(t);
        }
        indeg[t]++;
        edge[ednum].to = t;
        edge[ednum].next = head[s];
        head[s] = ednum;
        ednum++;
    }
    return 0;
}

int work() {
    int tot = 0, root = 0;
    for (int i = 0; i < (int)v.size(); i++) {
        if (indeg[v[i]] > 1) {
            return 0;
        }
        if (indeg[v[i]] == 0) {
            root = v[i];
            tot++;
            if (tot > 1) {
                return 0;
            }
        }
    }
    if (tot == 0) {
        return 1;
    }
    memset(flag, 0, sizeof(flag));
    int iq = 0;
    que[iq++] = root;
    flag[root] = 1;
    for (int i = 0; i < iq; i++) {
       int top = que[i];
       for (int k = head[top]; k != 0; k = edge[k].next) {
            int to = edge[k].to;
            if (flag[to] == 1) {
                return 0;
            }
            else {
                flag[to] = 1;
                que[iq++] = to;
            }
       }
    }
    for (int i = 0; i < (int)v.size(); i++) {
        if (!flag[v[i]]) {
            return 0;
        }
    }
    return 1;
}

int main()
{
    int c = 1;
    while (1) {
        int start = init();
        if (start == 1) {
            int ok = work();
            if (ok) {
                printf("Case %d is a tree.\n", c++);
            }
            else {
                printf("Case %d is not a tree.\n", c++);
            }
        }
        if (start == -1) {
            break;
        }
    }
    return 0;
}
poj 1308题目稍微改动,此题用并查集也很优)

你可能感兴趣的:(C++,c,struct,tree,include)