并查集。需要考虑入度。

 #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?的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【BZOJ2843】极地旅行社(Link-Cut Tree)

    [BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...

  3. 【BZOJ4530】大融合(Link-Cut Tree)

    [BZOJ4530]大融合(Link-Cut Tree) 题面 讨厌权限题!!! Loj链接 题目描述 小强要在 N个孤立的星球上建立起一套通信系统.这套通信系统就是连接 N个点的一个树.这个树的边是 ...

  4. 【BZOJ1969】航线规划(Link-Cut Tree)

    [BZOJ1969]航线规划(Link-Cut Tree) 题面 BZOJ 题解 删边操作 套路呀 离线读入倒过来做 变成加边操作 现在考虑怎么确定两点直接的关键路径条数 如果是一棵树,那么每条边都是 ...

  5. 【BZOJ4825】【HNOI2017】单旋(Link-Cut Tree)

    [BZOJ4825][HNOI2017]单旋(Link-Cut Tree) 题面 题面太长,懒得粘过来 题解 既然题目让你写Spaly 那就肯定不是正解 这道题目,让你求的是最大/最小值的深度 如果有 ...

  6. 【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)

    [BZOJ3669][Noi2014]魔法森林(Link-Cut Tree) 题面 题目描述 为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含 n ...

  7. 【BZOJ2049】洞穴勘测(Link-Cut Tree)

    [BZOJ2049]洞穴勘测(Link-Cut Tree) 题面 题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别 ...

  8. 【BZOJ3999】[TJOI2015]旅游(Link-Cut Tree)

    [BZOJ3999][TJOI2015]旅游(Link-Cut Tree) 题面 BZOJ 洛谷 题解 一道不难的\(LCT\)题(用树链剖分不是为难自己吗,这种有方向的东西用\(LCT\)不是方便那 ...

  9. 【BZOJ5212】[ZJOI2018]历史(Link-Cut Tree)

    [BZOJ5212][ZJOI2018]历史(Link-Cut Tree) 题面 洛谷 BZOJ 题解 显然实际上就是给定了一棵树和每个点被\(access\)的次数,求解轻重链切换的最大次数. 先考 ...

随机推荐

  1. mac os 终端提示 you have new mail

    这里的信息可能是由于所做的什么操作触发了发邮件的事件, 系统发送的邮件提醒. 我遇到的原因是由于运行 cron , 由于权限所导致了发邮件的事件提醒. Last login: Tue Apr 26 0 ...

  2. Java动态调用webService,axis2动态调用webService

    Java动态调用webService axis2动态调用webService >>>>>>>>>>>>>>>& ...

  3. sql if

    SELECT a.id, a.EduSiteNo, a.EduSiteName, a.SchoolId, a.LinkMan, a.Tel, a.Mobile, a.Fax, a.Address, C ...

  4. Customize the SharePoint 2013 search experience with a Content Enrichment web service

    Did you ever wish you had more control over how your content is indexed and presented as search resu ...

  5. HTML+CSS基础学习笔记(8)

    一.水平居中设置--行内元素 如果设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置text-align:center来实现的 二.水平居中设置 --定宽块状元素 #当被设置元素为块状元素时 ...

  6. [综合|基础|语法] 最新的皮肤帮助类 UI_Misc_Helper (转载)

    using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace ...

  7. 计算机网络-ip地址聚合后可用的地址数

    (1)59.81.1.128/28=59.81.1.1000-0000(2)59.81.1.144/28=59.81.1.1001-0000(3)59.81.1.160/28=59.81.1.1010 ...

  8. 本地tomcat的start.bat启动时访问不出现小猫图标

    排除端口错误.看看是不是webapps的root文件夹删除了,如果删除了,从tomcat的压缩包中解压一个root文件夹,房里面即可

  9. 清楚form表单数据的便捷jQuery之法

    有时候可能需要实现这样的效果:注册表单或者地址表单等填写多个记录之后,想要清除重新填写,如果一个个删除非常麻烦,因此这时清除按钮非常必须.接下来为您详细介绍两个自己经历的便捷方法,需要了解的朋友参考下 ...

  10. oracle的学习 第二节:创建数据表

    学习内容: A.创建数据库和表空间 B.创建用户和分配权限 C.创建数据表 一.创建数据库和表空间 (一)SQL语言的基本概念 1.概念 高级的结构化查询语言:沟通数据库服务器和客户的重要桥梁. PL ...