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睡着的时候 ...
随机推荐
- 如何利用C#编写网页投票器程序 如何使用代理来投票 代理IP来投票
一.前言看个图,了解下投票的过程.提交投票信息投票页 ――――――――>投票信息处理页反馈投票结果(请求页)<―――――――(响应页)一般情况下,填写投票信息,然后点提交按钮发送到响应 ...
- Java封装的与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前
public class CalendarCal { /** * 与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前 * * @param calendar * ...
- 微信小程序开发之 下拉刷新,上拉加载更多
本文记载了如何在微信小程序里面实现下拉刷新,上拉加载更多 先开看一下界面 大致如此的界面吧. 这个Demo使用了微信的几个Api和事件,我先列出来. 1.wx.request (获取远程服务器的数据, ...
- Template 模式
Template 模式是很简单模式,但是也应用很广的模式.Template 是采用继承的方式实现算法的异构,其关键点就是将通用算法封装在抽象基类中,并将不同的算法细节放到子类中实现.Template ...
- javascript-处理XML
/** * Created by Administrator on 2015/4/4. */ var XmlUtil=(function () { var createDocument= functi ...
- PHP图片操作
<?php $filename="http://pic.nipic.com/2007-12-06/2007126102233577_2.jpg";//图片地址//获取图片信息 ...
- TortoiseSVN显示图标不正常
Windows Explorer Shell支持的Overlay Icon最多15个,除去系统使用,只有11个.如果其他程序占用了,那么乌龟SVN就无法显示了.注册表定位到:HKEY_LOCAL_MA ...
- linux中VI编辑器使用个人记录
VI编辑器有三种编辑模式:命令模式.最后行模式.文本编辑模式 启动VI后进入的第一种模式是”命令模式“.从命令模式可进入最后行模式和编辑模式.而后两种模式之间不能直接切换.必须按ESC键退回到命令模式 ...
- Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?
Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...
- The Suspects(简单的并查集)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...