博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1308 (Is It A Tree?) 并查集
阅读量:7280 次
发布时间:2019-06-30

本文共 2623 字,大约阅读时间需要 8 分钟。

Is It A Tree?
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39434   Accepted: 12999

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.

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. 分析:这个题跟hdu1272(小希的迷宫)很像,不过这个题的边是有方向的, 小希的迷宫没有方向,合并的时候只要岸边的指向合并即可。
#include
#include
#include
using namespace std;int pre[200000],flag,N=0;int a[200000];void init(){ for(int i=0;i<200000;i++) pre[i]=i;}int find(int x){ if(x!=pre[x]) pre[x]=find(pre[x]); return pre[x];}void unit(int x,int y){ int rootx=find(x),rooty=find(y); if(rootx==rooty) flag=1; if(rootx!=rooty) pre[rootx]=pre[rooty];//x-->y }int main(){ int x,y,cas=0; N=flag=0;init(); while(scanf("%d%d",&x,&y)!=EOF) { if(x==-1&&y==-1) break; if(!x&&!y)//一组数据 { if(flag) printf("Case %d is not a tree.\n",++cas); else { int x=find(a[0]);//找出根 for(int i=1;i
View Code

 

转载于:https://www.cnblogs.com/ACRykl/p/9705597.html

你可能感兴趣的文章
用Go实现Redis之一准备工作
查看>>
简单5步,从0开始搭建你的第一款小程序
查看>>
安装Scrapy库报错 “error: Microsoft Visual C++ 14.0 is required. ”解决方法
查看>>
根据JWT的key和URL决定是否缓存HTTP请求
查看>>
前端AES加密
查看>>
持续更新 mongoose 细节收集
查看>>
Mybatis常见面试题
查看>>
基于VUE构建做题H5组件【附源代码】
查看>>
BFC深入理解
查看>>
Webpack 将代码打包成什么样子?
查看>>
从头实现一个简易版React(三)
查看>>
SVG相关学习(一)SVG基础
查看>>
极致Web性能 —— SPA性能指南
查看>>
关于 webpack 4.0.0 使用 html-webpack-plugin 报错的处理方法
查看>>
PHP-生成二维码(qr-code)
查看>>
git/github使用(入门到中阶)
查看>>
计数排序,桶排序与基数排序
查看>>
Android 项目目录结构
查看>>
一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](九)
查看>>
Facebook Docusaurus 中文文档 添加博客
查看>>