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睡着的时候 ...
随机推荐
- Deep Learning 学习随记(四)自学习和非监督特征学习
接着看讲义,接下来这章应该是Self-Taught Learning and Unsupervised Feature Learning. 含义: 从字面上不难理解其意思.这里的self-taught ...
- iOS目录结构
默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件 Documents:苹果建议将程序中建立的或在程序中浏览到的文件 ...
- GCD介绍(三): Dispatch Sources
何为Dispatch Sources 简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一个block放入一个dispatch queue ...
- linux如何开机以命令行形式启动?
在管理员权限下,修改/etc/inittab文件即可.把id:5:initdefault:改为id:3:initdefault:就可以了. 如下图所示: 图1: . 图2:
- 利用sfntly的sfnttool.jar提取中文字体
雨忆博客中提到了sfntly(具体介绍可以看:https://code.google.com/p/sfntly/),利用其中sfnttool.jar就可以提取只包含指定字符的字体,如果想在页面中通过@ ...
- CSS3 box-shadow(阴影使用)
from: http://jingyan.baidu.com/article/03b2f78c4d9fae5ea237aea6.html css3 box-shadow 内阴影与外阴影 1- box- ...
- flask request
请求对象要操作 URL (如 ?key=value )中提交的参数可以使用 args 属性:searchword = request.args.get('key', '')用户可能会改变 URL 导致 ...
- 转 四大Java EE容器(Tomcat、JBoss、Resin、Glassfish)之简单比较
现在流行的Java EE容器有很多:Tomcat.JBoss.Resin.Glassfish等等.下面对这四种Java EE容器进行 ...
- Application路径
根目录:StreamingAssets文件夹 #if UNITY_EDITOR string filepath = Application.dataPath +"/StreamingAsse ...
- nutch,hbase,zookeeper兼容性问题
nutch-2.1使用gora-0.2.1, gora-0.2.1使用hbase-0.90.4,hbase-0.90.4和hadoop-1.1.1不兼容,hbase-0.94.4和gora-0.2.1 ...