首页 技术 正文
技术 2022年11月11日
0 收藏 703 点赞 3,431 浏览 1268 个字

题目地址: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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297