【HDOJ】1325 Is It A Tree?
并查集。需要考虑入度。
#include <stdio.h>
#include <string.h> #define MAXNUM 10005 int bin[MAXNUM];
int degree[MAXNUM];
int nums[MAXNUM]; int find(int x) {
int r = x; while (bin[r] != r)
r = bin[r]; return r;
} int main() {
int x, y, fx, fy, n, case_n = ;
int i, flg; while () {
scanf("%d %d", &x, &y);
if (x< && y<)
break;
memset(degree, , sizeof(degree));
n = ;
++case_n;
if (x== && y==) {
printf("Case %d is a tree.\n", case_n);
continue;
}
for (i=; i<MAXNUM; ++i)
bin[i] = i;
fx = find(x);
fy = find(y);
bin[fy] = fx;
degree[y]++;
flg = ;
nums[n++] = x;
nums[n++] = y;
while () {
scanf("%d %d", &x, &y);
if (x== && y==)
break;
fx = find(x);
fy = find(y);
if (fx != fy) {
bin[fy] = fx;
degree[y]++;
} else {
bin[fy] = fx;
degree[y]++;
flg = ;
}
nums[n++] = x;
nums[n++] = y;
}
fx = find(nums[]);
for (i=; i<n; ++i) {
fy = find(nums[i]);
if (fx != fy) {
flg = ;
break;
}
}
for (i=; i<n; ++i) {
if (degree[nums[i]] > ) {
flg = ;
break;
}
}
if (flg)
printf("Case %d is a tree.\n", case_n);
else
printf("Case %d is not a tree.\n", case_n);
} return ;
}
【HDOJ】1325 Is It A Tree?的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【BZOJ2843】极地旅行社(Link-Cut Tree)
[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...
- 【BZOJ4530】大融合(Link-Cut Tree)
[BZOJ4530]大融合(Link-Cut Tree) 题面 讨厌权限题!!! Loj链接 题目描述 小强要在 N个孤立的星球上建立起一套通信系统.这套通信系统就是连接 N个点的一个树.这个树的边是 ...
- 【BZOJ1969】航线规划(Link-Cut Tree)
[BZOJ1969]航线规划(Link-Cut Tree) 题面 BZOJ 题解 删边操作 套路呀 离线读入倒过来做 变成加边操作 现在考虑怎么确定两点直接的关键路径条数 如果是一棵树,那么每条边都是 ...
- 【BZOJ4825】【HNOI2017】单旋(Link-Cut Tree)
[BZOJ4825][HNOI2017]单旋(Link-Cut Tree) 题面 题面太长,懒得粘过来 题解 既然题目让你写Spaly 那就肯定不是正解 这道题目,让你求的是最大/最小值的深度 如果有 ...
- 【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)
[BZOJ3669][Noi2014]魔法森林(Link-Cut Tree) 题面 题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n ...
- 【BZOJ2049】洞穴勘测(Link-Cut Tree)
[BZOJ2049]洞穴勘测(Link-Cut Tree) 题面 题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别 ...
- 【BZOJ3999】[TJOI2015]旅游(Link-Cut Tree)
[BZOJ3999][TJOI2015]旅游(Link-Cut Tree) 题面 BZOJ 洛谷 题解 一道不难的\(LCT\)题(用树链剖分不是为难自己吗,这种有方向的东西用\(LCT\)不是方便那 ...
- 【BZOJ5212】[ZJOI2018]历史(Link-Cut Tree)
[BZOJ5212][ZJOI2018]历史(Link-Cut Tree) 题面 洛谷 BZOJ 题解 显然实际上就是给定了一棵树和每个点被\(access\)的次数,求解轻重链切换的最大次数. 先考 ...
随机推荐
- dynamic 和var
dynamic,编译后被转换成带有 dynamicAttribute的object对象,可用在方法参数,返回值活或者局部变量上 执行过程: 运行时绑定首先会检查是否继承IDynamicMetaObje ...
- oracle查询最占用资源的查询
从V$SQLAREA中查询最占用资源的查询 select b.username username,a.disk_reads reads,a.executions exec,a.disk_reads/d ...
- nodejs框架express4.x 学习--安装篇
一.安装建立项目 1.安装nodejs 2.安装express(全局) npm install -g express 默认安装的是4.12.4 3.由于在3.6版本之后项目构建器被单独拆分出来,所以还 ...
- 使用Ubuntu 新建vpn过程
1.更新软件源 sudo apt-get update 2.安装pip sudo apt-get install python-pip 3.安装shadowsocks s ...
- C++学习笔记-1-自增和自减运算符
1. post-increment and pre-increment 的区别 来源:http://www.c4learn.com/c-programming/c-increment-operator ...
- 【随记】SQL Server连接字符串参数说明
废话不多说,请参见 SqlConnection.ConnectionString .
- jQuery 插件写法
一.jQuery插件的类型 1. jQuery方法 很大一部分的jQuery插件都是这种类型,由于此类插件是将对象方法封装起来,在jQuery选择器获取jQuery对象过程中进行操作,从而发挥jQue ...
- sublime text 2代码片段(Snippet)功能的使用
“snippet”在英语里面是“片段”的意思.当我们编码时候,通常想要打几个简略的字符串,就出来一些固定的模板. 例如:使用snippet在新建文件时快速生成HTML头部信息等. 定义很简单,菜单:t ...
- linux 备份日志文件
seo说要备份文件,然后自己搞不定,每天一份文件.写了个shell,加了个crontab -e任务.每天执行一次. crontab: 59 23 * * * /root/sh/dumpApacheLo ...
- Day17 表单验证、滚动菜单、WEB框架
一.表单验证的两种实现方式 1.DOM绑定 <!DOCTYPE html> <html lang="en"> <head> <meta c ...