• A1094 The Largest Generation [dfs/bfs]


    在这里插入图片描述

    这里是按层求最大节点个数及所在的层数,所以要每条路走一遍记录一下。

    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<string>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    const int maxn = 110;
    vector<int>node[maxn];
    int hashtable[maxn];
    int maxLevel = -1;
    void dfs(int index, int level)
    {
    	hashtable[level]++;
    	if (level > maxLevel)
    		maxLevel = level;
        if (node[index].size() == 0)
    		return;
    	for (int i = 0; i < node[index].size(); i++)
    	{
    		dfs(node[index][i], level + 1);
    	}
    }
    int main()
    {
    	int n, m, parent, k, child;
    	cin >> n >> m;
    	for (int i = 0; i < m; i++)
    	{
    		cin >> parent >> k;
    		for (int j = 0; j < k; j++)
    		{
    			cin >> child;
    			node[parent].push_back(child);
    		}
    	}
    	dfs(1, 1);
    	int maxlevel = 0, maxcount = 0;
    	for (int i = 1; i <= maxLevel; i++)
    	{
    		if (hashtable[i] > maxcount)
    		{
    			maxcount = hashtable[i];
    			maxlevel = i;
    		}
    	}
    	cout << maxcount << " " << maxlevel << endl;
    }
    
    
    
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<string>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    const int maxn = 110;
    vector<int>node[maxn];
    int hashtable[maxn];
    int level[maxn];
    int maxLevel = -1;
    void bfs(int index)
    {
    	queue<int>q;
    	q.push(index);
    	level[index] = 1;
    	while (!q.empty())
    	{
    		int temp = q.front();
    		q.pop();
    		hashtable[level[temp]]++;
    		for (int i = 0; i < node[temp].size(); i++)
    		{
    			q.push(node[temp][i]);
    			level[node[temp][i]] = level[temp] + 1;
    		}
    	}
    }
    int main()
    {
    	int n, m, parent, k, child;
    	cin >> n >> m;
    	for (int i = 0; i < m; i++)
    	{
    		cin >> parent >> k;
    		for (int j = 0; j < k; j++)
    		{
    			cin >> child;
    			node[parent].push_back(child);
    		}
    	}
    	bfs(1);
    	int maxlevel = 0, maxcount = 0;
    	for (int i = 1; i <= maxn; i++)
    	{
    		if (hashtable[i] > maxcount)
    		{
    			maxcount = hashtable[i];
    			maxlevel = i;
    		}
    	}
    	cout << maxcount << " " << maxlevel << endl;
    }
    
  • 相关阅读:
    什么是级联
    @GeneratedValue
    Spring Data JPA中CrudRepository与JpaRepository的不同
    Spring Data JPA的方法命名规则
    C/S架构的渗透测试-请求加解密及测试
    各类Fuzz字典
    Nessus8.11破解
    xss实验1-20writeup
    IIS短文件名漏洞
    Linux挂载exfat文件系统的U盘
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13812011.html
Copyright © 2020-2023  润新知