并查集
来源:互联网 发布:ubuntu 软件安装目录 编辑:程序博客网 时间:2024/05/14 23:08
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1164 Accepted Submission(s): 358Problem Description
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.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
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.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
Output
For each test case display the line ``Case k is a tree.\\\\\\\" or the line ``Case k is not a tree.\\\\\\\", where k corresponds to the test case number (they are sequentially numbered starting with 1).
Sample Input
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1
Sample Output
Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.
#include <iostream>#include <sstream>#include <string.h>#include <cstdio>#include <string>#include <cctype>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <cmath>#define PI acos(-1)#define ll long long#define INF 0x7fffffff#define _N 100005using namespace std;/// 父亲int parent[_N];/// 树的高度int high[_N];/// 初始化void init(int n) { for (int i = 0; i < n; i++) { parent[i] = i; high[i] = 0; }}/// 查询树的根int _find(int x) { if (parent[x] == x) { return x; } else { return parent[x] = _find(parent[x]); }}/// 合并void unite(int x, int y) { x = _find(x); y = _find(y); if (x == y) { return ; } if (high[x] < high[y]) { parent[x] = y; } else { parent[y] = x; if (high[x] == high[y]) high[x]++; }}/// 是否属于同一个集合bool same(int x, int y) { return _find(x) == _find(y);}/// 是否已经访问bool v[_N];/// 入度int in[_N];/// mergebool _unite(int x, int y) { x = _find(x); y = _find(y); if (x != y) { parent[y] = x; return true; } else { return false; }}int main(){ ///freopen("in.txt", "r", stdin); ///freopen("out.txt", "w", stdout); int t = 1; int x, y; while (~scanf("%d%d", &x, &y)) { /// 注意!!! 不是-1,认真读题 if (x < 0 && y < 0) break; /// empty (null, void, nothing) if (x == 0 && y == 0) { printf("Case %d is a tree.\n", t++); continue; } /// init init(_N); bool error = 0; memset(v, 0, sizeof(v)); memset(in, 0, sizeof(in)); /// op v[x] = 1; v[y] = 1; in[y]++; if (_unite(x, y) == false) error = 1; while (~scanf("%d%d", &x, &y)) { if (x == 0 && y == 0) break; v[x] = 1; v[y] = 1; in[y]++; if (in[y] > 1) error = 1; if (_unite(x, y) == false) error = 1; } if (error) { printf("Case %d is not a tree.\n", t++); } else { int root = 0; for (int i = 0; i < _N; i++) { if (v[i] && _find(i) == i) root++; } if (root > 1) printf("Case %d is not a tree.\n", t++); else printf("Case %d is a tree.\n", t++); } } return 0;}
0 0
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- 并查集
- 2.6.1HTML5表单的创建
- vue+webpack+es6+sass
- rEFI引导Win10+Ubuntu16双系统
- RHCE_1-2_[从命令行管理文件]
- C++ 11 线程调用类的成员函数解决办法
- 并查集
- 指针变量作为函数参数为了改变指针变量指向实参的值
- 八皇后
- Android贝塞尔曲线————波浪效果(大波浪)
- java 中基本数据类型和封装类
- [国内首套] Kotlin从入门到『放弃』系列 视频教程
- Ubuntu14.04安装失败!!很伤心,Why?
- 微信红包知多少
- R 语言编程艺术笔记