HDU1272&POJ

来源:互联网 发布:莎士比亚别生气 知乎 编辑:程序博客网 时间:2024/06/15 03:11

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.

There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
hdu1272和poj1308的题目是一样的,不过输出结果不同。正赶上今天poj崩了,只在hdu上a了这道题。
这道题的问形成的图是不是一棵树。树的要求有:
1.入度为1(根除外),出度不限。因为我是用并查集的思想做的,所以一发现par所指的节点想要改变就说明入度大于1了。
2.没有环,不形成森林。在进行 并 的过程中,记录边数和节点数,看是否相差1.
3.一个点或者没有点也是树。(没有点也是树?orz)
——————————————
下面的代码是过hdu的,用注释的替换原来的就能a了poj的

#include<iostream>#include<string>#include<algorithm>#include<stdlib.h>#include<stdio.h>#include<queue>#include<vector>using namespace std;#define N 100000+20#define INF 0x3f3f3f3f#define mem(arr,a) memset(arr,a,sizeof(arr))/*********************************/int par[N];int used[N];int n, m;int flag;int e;void unite(int a, int b){    e++;    if (par[b] == a){        flag = 1;        return;    }    par[b] = a;}int main(){    int cnt = 1;    int num = 0;    const char*no = "No";//"is not a tree.";    const char*yes = "Yes";// "is a tree.";    while (cin >> n >> m){        mem(used, 0);        mem(par, -1);        flag = 0;        e = 0;        num = 0;        if (n == -1 && m == -1)return 0;//      printf("Case %d ", cnt++);        if (n == 0 && m == 0){            cout << yes << endl;            continue;        }        num = 2;        par[n] = n;        unite(n, m);        while (cin >> n >> m){            if (n == 0 && m == 0){                if (!flag&&e == num - 1)cout << yes << endl;                else cout << no << endl;                break;            }            if (par[n] == -1){                par[n] = n;                num++;            }            if (par[m] == -1||par[m]==m){                if (par[m]==-1)                num++;                unite(n, m);            }            else{                flag = 1;                continue;            }        }    }}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 兔子生了不喂奶怎么办 兔子出生4天摸了怎么办 兔子吃了纸箱子怎么办 被小兔子抓一下怎么办 兔子吃了蟑螂药怎么办 兔子洗了澡要死怎么办 养的花蔫了怎么办 养的小鸡很大了怎么办 养的小兔子死了怎么办 小狗出现爬跨行为怎么办 养的小狗总做吞咬人的行为怎么办 螃蟹和柿子一起吃了怎么办 指甲上月牙没了怎么办 手指上月牙没了怎么办 指甲上没半月牙怎么办 电动车车牌被偷了怎么办 警察拖车拖坏了怎么办 6岁儿童牙疼怎么办 小白单车不退押金怎么办 光盘放笔记本电脑里读不出来怎么办 cd光盘读不出来怎么办 最近脸干的不行怎么办 脸感觉干的不行怎么办 新生儿睡觉黑白颠倒了怎么办 婴儿吐水和奶花怎么办 刚刚出生的宝宝拉肚子怎么办 刚出生的婴儿拉肚子怎么办 新生儿5天拉稀水怎么办 10个月孩子拉肚子怎么办 不满月的宝宝拉肚子怎么办 一周岁宝宝发烧腹泻呕吐怎么办 6个月宝宝37度怎么办 1岁宝宝发烧37.2怎么办 新生儿发烧37度3怎么办 两个月宝宝抵抗力差怎么办 6月宝宝着凉拉稀怎么办 六个月的宝宝拉肚子怎么办 衣服颜色太深了怎么办 一多半宝宝爱喝水不爱吃饭怎么办 十个月宝宝不爱吃饭怎么办 十个月宝宝突然不爱吃饭怎么办