1094. The Largest Generation (25)

简介: #include #include using namespace std;vector v[100];int maxdepth = 0, level[100] = {0};bool visited[100] = ...
#include <iostream>
#include <vector>
using namespace std;
vector<int> v[100];
int maxdepth = 0, level[100] = {0};
bool visited[100] = {false};

void dfs(int index, int depth){
    if(!visited[index]) level[depth]++;
    visited[index] = true;
    if(v[index].size() == 0){
        if(depth > maxdepth) maxdepth = depth;
        return;
    }
    for (int i = 0; i < v[index].size(); i++)
        dfs(v[index][i], depth + 1);
}

int main(){
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int f, k;
        cin >> f >> k;
        for (int j = 0; j < k; j++) {
            int t;
            cin >> t;
            v[f].push_back(t);
        }
    }
    dfs(1, 0);
    int maxnum = 0, maxg = 0;
    for (int i = 0; i <= maxdepth; i++) {
        if(level[i] > maxnum){ maxnum = level[i]; maxg = i + 1;}
    }
    cout << maxnum << ' ' << maxg << endl;

    return 0;
}
目录
相关文章
|
8天前
|
人工智能
Big Water Problem
Big Water Problem
11 0
|
7月前
|
存储 算法 搜索推荐
Leetcode 347.Top K Frequent Elements
一句话理解题意:输出数组中出现次数对多的k个数。 在如果用C语言来写这个题目,思路就是先按数的大小排序,然后再用一个结构体数组保存每个数的出现次次数。 因为数组已经有序了,所以只需要遍历一次数组就可以获得每个数的出现次数了。
30 3
|
机器学习/深度学习 算法
LeetCode 347. Top K Frequent Elements
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
32 0
LeetCode 347. Top K Frequent Elements
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)
120 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
89 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
95 0
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
PAT (Advanced Level) Practice - 1004 Counting Leaves(30 分)
89 0
【1094】The Largest Generation (25 分)
【1094】The Largest Generation (25 分) 【1094】The Largest Generation (25 分)
59 0
|
SQL 移动开发 算法
New Dynamic Programming Algorithm for the Generation of Optimal Bushy Join Trees
MySQL无疑是现在开源关系型数据库系统的霸主,在DBEngine的最新排名中仍然稳居第2位,与第3位SQL Server的积分差距并不算小,可以说是最受欢迎,使用度最高的数据库系统,这一点看看有多少新型数据库要兼容MySQL的协议和语法就知道了。
269 0
New Dynamic Programming Algorithm for the Generation of Optimal Bushy Join Trees