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; } } }}
- HDU1272&POJ
- hdu1272
- hdu1272
- hdu1272
- hdu1272
- hdu1272
- HDU1272
- hdu1272
- hdu1272
- HDU1272
- hdu1272
- HDU1272
- hdu1272
- hdu1272
- hdu1272
- hdu1272
- HDU1272
- hdu1272
- Node.js结合mongoose操作MongoDB
- 学习笔记: 源码 accuracy_layer.cpp 略明
- Java工程师成神之路~
- Android之okhttp的用法
- oracle 单行函数
- HDU1272&POJ
- 我与python约个会:07程序设计开发基础
- Eclipse创建Spark的WordCount工程
- ubuntu下NFS挂载常见错误的解决方法[转载]
- Oracle 数据库字段乱码 或 sqlplus CMD乱码
- windows防火墙配置与勒索病毒防范-限制445端口访问
- 【运维】vim 编辑器
- 第1节---分别用HttpURLConnection和HttpClient爬取百度首页
- 买糖果