N - Is It A Tree?(判断环)
#include <stdio.h>
#include<algorithm>
using namespace std; const int maxn = ; int f[maxn], use[maxn]; void init()
{
for(int i=; i<maxn; i++)
f[i] = i, use[i] = ;
}
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
} int main()
{
int u, v, ok=, t=; init();
while(scanf("%d%d", &u, &v), u!= - || v!=-)
{
if(u+v == )
{
int sum = ; for(int i=; i<maxn; i++)
{
if(use[i] == && f[i] == i)
sum++;
} if(ok && sum < )printf("Case %d is a tree.\n", t++);
else printf("Case %d is not a tree.\n", t++); ok = ;
init();
}
else
{
use[u] = use[v] = ;
u = Find(u), v = Find(v);
if(u != v)
f[u] = v;
else ok = ;
}
} return ;
}
N - Is It A Tree?(判断环)的更多相关文章
- Floyd判断环算法总结
Floyd判断环算法 全名Floyd’s cycle detection Algorithm, 又叫龟兔赛跑算法(Floyd's Tortoise and Hare),常用于链表.数组转化成链表的题目 ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- [bzoj 1064][NOI2008]假面舞会(dfs判断环)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1064 分析: 如果a看到b,则a->b 那么: 1.如果图中有环,则说明这个环的 ...
- FZU-1924+判断环/DFS/BFS
dfs: /* dfs */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include< ...
- LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- HDU - 1272 小希的迷宫(并查集判断环)
https://cn.vjudge.net/problem/HDU-1272 Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardo ...
- POJ1860 Currency Exchange【最短路-判断环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- Codeforces 937 D. Sleepy Game(DFS 判断环)
题目链接: Sleepy Game 题意: Petya and Vasya 在玩移动旗子的游戏, 谁不能移动就输了. Vasya在订移动计划的时候睡着了, 然后Petya 就想趁着Vasya睡着的时候 ...
随机推荐
- java开发规范总结_命名规范
规范需要平时编码过程中注意,是一个慢慢养成的好习惯 1.文件 1.属性文件后缀为properties,并且符合java中i18n的规范: 2.对于各产品模块自己的配置文件必须放置在自己模块的con ...
- 用response输出一个验证码
package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servle ...
- 292. Nim Game(C++)
292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...
- 完整的 dataType=text/plain jquery ajax 登录验证
Html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...
- js作用域与作用域链
一直对Js的作用域有点迷糊,今天偶然读到JavaScript权威指南,立马被吸引住了,写的真不错.我看的是第六版本,相当的厚,大概1000多页,Js博大精深,要熟悉精通需要大毅力大功夫. 一:函数作用 ...
- git学习利器:《Git Pro》中文版
Git书籍有<版本控制之道git>,但是很一般.强烈推荐<Git Pro>中文版! 很多开源软件的教程也是免费开源的在线阅读的. <Git Pro>中文版在线阅读h ...
- input file文件上传样式
<style> .file-group { position: relative; width: 200px; height: 80px; ...
- ecshop--加载初始化文件
define('IN_ECS', true);require(dirname(__FILE__) . '/../../includes/init.php'); 在开头要加入这两句文件才可以访问数据库以 ...
- opencv之图像腐蚀
最近准备好好学习opencv 这博客就是我的学习笔记. #include <cv.h> #include <highgui.h> using namespace std; vo ...
- Python自动化运维之24、JQuery
jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, ...