真尴尬1180

来源:互联网 发布:兴安得力计价软件 编辑:程序博客网 时间:2024/04/30 03:23

结束的标志是两个小于0的数……
被套路了……..

#include <set>#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 10005;const int maxm = 10005;struct Edge{    int from,to,next;};Edge e[maxm];int head[maxn],edgeNum = 0,uset[maxn],du[maxn];void addEdge(int from,int to){    e[edgeNum].from = from; e[edgeNum].to = to; e[edgeNum].next = head[from];    head[from] = edgeNum; edgeNum++;}int Find(int x){    if(x == uset[x]) return x;    else return uset[x] = Find(uset[x]);}int bfs(int rt){    queue<int> q;    q.push(rt);    int num = 1;    while(!q.empty())    {        int fa = q.front(); q.pop();        for(int j = head[fa]; j != 0; j = e[j].next)        {            q.push(e[j].to);            num++;        }    }    return num;}int main(){    //freopen("input.txt","r",stdin);    int from,to,cas = 1;    while(scanf("%d%d" ,&from,&to) != EOF)    {        if(from <= 0 || to <= 0) break;        if(from == 0 && to == 0)        {            printf("Case %d is a tree.\n",cas++); continue;        }        memset(head,0,sizeof(head));        memset(du,0,sizeof(du));        edgeNum = 1;        set<int> myset;        myset.insert(from);        myset.insert(to);        addEdge(from,to);        du[to]++;        while(scanf("%d%d" ,&from,&to))        {            if(from == 0 && to == 0) break;            myset.insert(from);            myset.insert(to);            addEdge(from,to);            du[to]++;        }        for(int i = 0; i < maxn; i++) uset[i] = i;        bool OK = true; //认为不存在重边,环        for(int i = 1; i < edgeNum; i++)        {            int x = Find(e[i].from);            int y = Find(e[i].to);            if(x != y) uset[x] = y;            else            {                OK = false; break;            }        }        if(!OK) //存在重边,环        {            printf("Case %d is not a tree.\n",cas++); continue;        }        int num = 0; //连通分量个数        for(set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr)        {            int x = *itr;            if(uset[x] == x) num++;            if(num >= 2) break;        }        if(num != 1)        {            printf("Case %d is not a tree.\n",cas++); continue;        }        else //只有一个连通分量        {            int rt = -1;            for(set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr)            {                int x = *itr;                if(du[x] == 0)                {                    rt = x; break;                }            }            if(rt == -1) //没能找到根            {                printf("Case %d is not a tree.\n",cas++); continue;            }            else            {                int cnt = bfs(rt);                if(cnt != myset.size())                {                    printf("Case %d is not a tree.\n",cas++);                }                else                {                    printf("Case %d is a tree.\n",cas++);                }            }        }    }    return 0;}
0 0
原创粉丝点击