博客
关于我
POJ——1308 HDU——Is It A Tree?(并查集模板题)
阅读量:180 次
发布时间:2019-02-28

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

原题链接

在这里插入图片描述

测试样例

Sample Input6 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 -1Sample OutputCase 1 is a tree.Case 2 is a tree.Case 3 is not a tree.

题意:给你一些点,判断是不是树。

解题思路:和杭电的1272题一模一样,只不过表达方式不一样而已,且这道题也是有向图,需要判断子结点是根结点。还有此题数据也比较刁钻,需要进行很多优化,具体看代码。指路HDU——1272题解blog:。这道题使用并查集模板即可,若对并查集还不是很熟的话,这里指路一篇blog:

AC代码

/**邮箱:unique_powerhouse@qq.com*blog:https://me.csdn.net/hzf0701*注:文章若有任何问题请私信我或评论区留言,谢谢支持。**/#include
//POJ不支持#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。#define pb push_back#define IOS ios::sync_with_stdio(false)#define fi first#define se second#define mp make_pairusing namespace std;const int inf = 0x3f3f3f3f;//无穷大const int maxn = 1e4+10;//最大值。typedef long long ll;typedef long double ld;typedef pair
pll;typedef pair
pii;//*******************************分割线,以上为自定义代码模板***************************************//int father[maxn],vis[maxn],flag,maxx;void init(){ for(int i=0;i
=0&&v>=0) { if(u==0&&v==0) { int cnt=0; for(int i=1;i<=maxn;i++) if(vis[i]&&i==father[i]) cnt++; if(cnt<=1&&!flag) printf("Case %d is a tree.\n",++kase); else printf("Case %d is not a tree.\n",++kase); init(); continue; } maxx=max(maxx,max(u,v)); //统计最大顶点编号,减少不必要的计算。 vis[u]=vis[v]=1; Merge(u,v); } return 0;}

转载地址:http://yusn.baihongyu.com/

你可能感兴趣的文章
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>