Problem 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.
![](https://i-blog.csdnimg.cn/blog_migrate/30ef78e36a20744a5f03113e493b37bd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ea4cdfb5d2f2f534399ffe7a50055bc.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9d5304508ccb31ccef0e0cc34205bd20.gif)
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.只有一个根结点,否则就是森林了(入度为0的结点只有一个)2.不能有环,我们只要用一个数组记录每个点的入度是否为1就行了(根结点入度为0)所以,关键的一步就是入度的记录。除此之外,还需注意:1.程序结束的条件是两个负数而不是两个-1啊!!害我超时了。2.这题并不是默认从1-n的点都有,比如第一个例子就没有1.代码:#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <set> #include <vector> #define MAX 1500 using namespace std; int Father[MAX];// int Indegree[MAX]; int a[MAX][2]; int b[MAX];//用来记录数据是否已存入vector中 int p,q; int cnt = 0,ct = 0,flag1 = 0,flag2 = 0; vector<int> s;//用来存储涉及到的点 int Find(int x) { if(Father[x]!= x) { return Find(Father[x]); } return x; } void Merge(int x, int y)//起点 终点 { int ix = Find(x); int iy = Find(y); if(ix != iy) { Father[iy] = ix; Indegree[y]++;//终点入度+1 } } void solve(int a[MAX][2],int len) { int n = 0; int len2 = s.size(); for (int i = 0; i< len; i++) { Merge(a[i][0],a[i][1]); } for (int i = 0; i < len2; i++) { if(Indegree[s[i]] > 1)//入度大于1 { flag2 = 1; return; } if(Indegree[s[i]] == 0) { n++;//根结点个数 } } if(n > 1) { flag1 = 1; } } int main() { memset(Indegree,0,sizeof(Indegree)); memset(b,0,sizeof(b)); while (1) { scanf("%d%d",&q,&p); if(p < 0 && q < 0) { return 0; } if(p == 0 && q == 0) { solve(a,cnt); ct++; if(flag1 == 1 || flag2 == 1) { cout << "Case "<< ct <<" is not a tree."<< endl; } else { cout << "Case "<< ct <<" is a tree."<< endl; }
//为下一例做初始化工作 cnt = 0; memset(Indegree,0,sizeof(Indegree)); memset(b,0,sizeof(b)); flag1 = flag2 = 0; s.clear(); continue; } if(b[p] == 0) { s.push_back(p); Father[p] = p; b[p] = 1; } if(b[q] == 0) { s.push_back(q); Father[q] = q;//初始化父节点 b[q] = 1; } a[cnt][0] = q; a[cnt][1] = p; cnt++; } return 0; }