• 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;
    }
    
  • 相关阅读:
    hdu5894 hannnnah_j’s Biological Test(组合数取模)
    HDU5883 The Best Path(并查集+欧拉路)
    HDU5881 Tea(简单题)
    组合数取模
    codeforces703D Mishka and Interesting sum(区间偶数异或)
    upcoj2679 Binary Tree(思路题)
    upcoj2673 It Can Be Arranged(isap)
    atom编辑器适用
    python库pandas
    fabric Node SDK进行连接
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13812011.html
Copyright © 2020-2023  润新知