http://poj.org/problem?id=1308
利用并查集看最后是不是都再一个集合内,如果有出现自环也是错的
http://blog.csdn.net/accelerator_/article/details/23930947
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 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 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.
利用并查集看最后是不是都再一个集合内,如果有出现自环也是错的
解题思路:并查集,如果两个点之间先前已经联通则说明不是一棵树。
注意几个坑点:
1,空树是一棵树;2,相同的两个点间存在两条或者多条边也是不行的;3,形成环是不行的;4,森林是不行的。
http://blog.csdn.net/accelerator_/article/details/23930947
- const int N = 100005;
- int a, b, n, m, vis[N], parent[N], flag;
- int find(int x) {
- if (x == parent[x])
- return x;
- return parent[x] = find(parent[x]);
- }
- void Union(int a, int b) {
- if (!vis[a]) n++; vis[a] = 1;
- if (!vis[b]) n++; vis[b] = 1;
- int pa = find(a);
- int pb = find(b);
- if (pa != pb) {
- parent[pa] = pb;
- m++;
- }
- else {
- flag = 1;
- }
- }
- void init() {
- n = m = flag = 0;
- memset(vis, 0, sizeof(vis));
- for (int i = 1; i < N; i++) parent[i] = i;
- }
- int main() {
- int cas = 0;
- init();
- while (~scanf("%d%d", &a, &b) && a != -1) {
- if (a == 0 && b == 0) {
- printf("Case %d %s\n", ++cas, (!flag && (m == n - 1 || n == 0))? "is a tree." : "is not a tree.");
- init();
- continue;
- }
- Union(a, b);
- }
- return 0;
- }
- const int N = 1e6+5;
- struct edge {
- int u, v;
- edge (int u, int v) {
- this->u = u;
- this->v = v;
- }
- friend bool operator < (const edge& a, const edge& b) {
- if (a.u != b.u) return a.u < b.u;
- return a.v < b.v;
- }
- };
- int n, f[N];
- vector<edge> g;
- set<int> vis;
- int getfar (int x) {
- return x == f[x] ? x : f[x] = getfar(f[x]);
- }
- bool init () {
- int a, b;
- n = 0;
- g.clear();
- vis.clear();
- while (scanf("%d%d", &a, &b) == 2 && a + b) {
- if (a == -1 && b == -1) return false;
- n = max(n, max(a, b));
- g.push_back(edge(a, b));
- vis.insert(a);
- vis.insert(b);
- }
- for (int i = 0; i <= n; i++)
- f[i] = i;
- return true;
- }
- bool judge () {
- for (int i = 0; i < g.size(); i++) {
- int a = g[i].u, b = g[i].v;
- int p = getfar(a), q = getfar(b);
- if (p == q) return false;
- f[q] = p;
- }
- int tmp = getfar(n);
- for (set<int>::iterator i = vis.begin(); i != vis.end(); i++)
- if (getfar(*i) != tmp) return false;
- return true;
- }
- int main () {
- int cas = 1;
- while (init()) {
- printf("Case %d is %s\n", cas++, judge()? "a tree." : "not a tree.");
- }
- return 0;
- }