Sicily 1034. Forest
题目地址:1034. Forest
思路:
网上很多说用深搜,很任性.......发现广搜也挺好用的,实验课打的(⊙o⊙)…orz........囧。
先找根结点,根据根结点广搜深度,广搜宽度,不过要开一个数组,同一层的累加宽度。别忘了要判断是否合法。
具体代码如下:
#include <iostream>
#include <cstring>
#include <queue>
using namespace std; bool path[][];
bool visited[];
bool Root[]; int main()
{
int n, m;
while (cin >> n >> m && n)
{
memset(path, false, sizeof(path));
memset(visited, false, sizeof(visited));
memset(Root, true, sizeof(Root)); bool flag = n > m ? true : false;
for (int i = ; i <= m; i++)
{
int node1, node2;
cin >> node1 >> node2;
if (node1 == node2) flag = false;
path[node1][node2] = true;
}
if (flag == false) {
cout << "INVALID\n";
continue;
} for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
if (path[j][i])
Root[i] = false;
int maxwidth = ;
for (int i = ; i <= n; i++)
if (Root[i]) {
maxwidth++;
visited[i] = true;
}
queue<int> store;
int depth, maxdepth;
maxdepth = depth = ;
int width[] = {};
for (int i = ; i <= n; i++)
{
if (Root[i])
{
store.push(i);
depth = ;
while (!store.empty())
{
int size = store.size();
width[depth] += size;
while (size--)
{
for (int j = ; j <= n; j++)
if (path[store.front()][j])
{
if (!visited[j]) {
store.push(j);
visited[j] = true;
}
else
flag = false;
}
store.pop();
}
if (!store.empty())
depth++;
}
maxdepth = depth > maxdepth ? depth : maxdepth;
}
} for (int i = ; i <= n; i++)
if (!visited[i]) {
flag = false;
break;
} for (int i = ; i <= maxdepth; i++)
maxwidth = width[i] > maxwidth ? width[i] : maxwidth; flag == false ? cout << "INVALID" : cout << maxdepth << " " << maxwidth;
cout << endl;
} return ;
}
Sicily 1034. Forest的更多相关文章
- 基于netty轻量的高性能分布式RPC服务框架forest<下篇>
基于netty轻量的高性能分布式RPC服务框架forest<上篇> 文章已经简单介绍了forest的快速入门,本文旨在介绍forest用户指南. 基本介绍 Forest是一套基于java开 ...
- 基于netty轻量的高性能分布式RPC服务框架forest<上篇>
工作几年,用过不不少RPC框架,也算是读过一些RPC源码.之前也撸过几次RPC框架,但是不断的被自己否定,最近终于又撸了一个,希望能够不断迭代出自己喜欢的样子. 顺便也记录一下撸RPC的过程,一来作为 ...
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
- [Machine Learning & Algorithm] 随机森林(Random Forest)
1 什么是随机森林? 作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Forest,简称RF)拥有广泛的应用前景,从市场营销到医疗保健保险,既可以用来做市场营销模拟的建模,统计客户来 ...
- sicily 1934. 移动小球
Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...
- STL : map函数的运用 --- hdu 4941 : Magical Forest
Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 异常检测算法--Isolation Forest
南大周志华老师在2010年提出一个异常检测算法Isolation Forest,在工业界很实用,算法效果好,时间效率高,能有效处理高维数据和海量数据,这里对这个算法进行简要总结. iTree 提到森林 ...
- BZOJ 1034 泡泡堂BNB 贪心+简单博弈
同样是今天做bzoj时做到的,感觉能力范围之内的就做了,也是蛮简单的 1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec Memory Limit: 162 MB Su ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
随机推荐
- HDOJ 1018 Big Number(大数位数公式)
Problem Description In many applications very large integers numbers are required. Some of these app ...
- Linux文件操作学习总结【转载】
本文转载自: http://blog.csdn.net/xiaoweibeibei/article/details/6556951 文件类型:普通文件(文本文件,二进制文件).目录文件.链接文件.设备 ...
- [Java] Collections - 源代码学习笔记
Collection interface 集合接口 1. 在 Collections 体系中,接口 Collection 是根接口 2. 是指一组对象,这些对象被称为 Collection 的元素. ...
- C - Building a Space Station - poj 2031
空间站是有一些球状的房间组成的,现在有一些房间但是没有相互连接,你需要设计一些走廊使他们都相通,当然,有些房间可能会有重合(很神奇的样子,重合距离是0),你需要设计出来最短的走廊使所有的点都连接. 分 ...
- (1.1.9)UVA 10930 A-Sequence(模拟)
/* * UVA_10930_1.cpp * * Created on: 2013年10月7日 * Author: Administrator */ #include <iostream> ...
- JQuery的ready函数与JS的onload的区别详解
JQuery的ready函数与JS的onload的区别:1.执行时间window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行.$(document).ready()是DOM结构绘制 ...
- 史上最全maven pom.xml详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- c# 根据自定义Attribute排序
add a class: public class ExportAttribute : Attribute { public int FieldOrder { get; set; } ...
- os即时通讯客户端开发之-mac上安装MySQL
一.安装 到MySQL官网上http://dev.mysql.com/downloads/mysql/,下载mysql可安装dmg版本 比如:Mac OS X ver. 10.7 (x86, 64-b ...
- XTU1199:Number Game
题目描写叙述 给你一个有N个数的集合S和一个数X,推断是否存在S的一个子集,子集里的数的最小公倍数正好是X. 输入 第一行是数据组数T. 接下来有多组数据,每组数据包括两行: 第一行有2个数N和X,1 ...