【每日一题】8月27日题目精讲 Is It A Tree?
鏈接:https://ac.nowcoder.com/acm/problem/105905
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 10000K,其他語言20000K
64bit IO Format: %lld
題目描述
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.
輸入描述:
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.
輸出描述:
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).
示例1
輸入
復制
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
輸出
復制
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree
題解:
題意:判斷給的是不是樹
我們想想樹的性質,每個點都有一個父親節點,除了根節點
前者我們可以用并查集維護,后者可以用入度出度來判斷,因為根節點只有出度,入度為0,而其他點的入度為1
為了避免是森林,我們還應該要求所有點必須聯通
對了,空樹也為樹
代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <math.h> #include <string> #include <list> #include <set> #include <queue> #include <stack> #include <algorithm> #include <stdlib.h> #include <vector> #define maxn 10010 const int MaxN = 0x3f3f3f3f; const int MinN = 0xc0c0c00c;typedef long long ll; ll mod = 998244353; using namespace std; int f[maxn]; int in[maxn]; bool visited[maxn]; int ifind(int x){if(x==f[x]) return x;return f[x]=ifind(f[x]); } void imerge(int x,int y){int dx=ifind(x);int dy=ifind(y);if(dx!=dy) f[dx]=dy;return ; }int main() {int n,m;int z=1;while(cin>>n>>m&&n!=-1){for(int i=0;i<maxn;i++) f[i]=i;memset(visited,false,sizeof(visited));memset(in,0,sizeof(in));bool flag=true;if(n==0&&m==0){printf("Case %d is a tree.\n",z++);continue;}while(n||m){visited[n]=true;visited[m]=true;in[m]++;imerge(n,m);scanf("%d%d",&n,&m);}int ans=0;for(int i=1;i<maxn;i++){if(visited[i]&&in[i]==0){ans++;}if(in[i]>=2) flag=false;}if(ans!=1) flag=false;if(flag==false) printf("Case %d is not a tree.\n",z++);else printf("Case %d is a tree.\n",z++);}return 0; }總結
以上是生活随笔為你收集整理的【每日一题】8月27日题目精讲 Is It A Tree?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 星巴克和阿里“结婚”,这后面真的不简单
- 下一篇: 四时田园杂兴其三十一的诗意