跳转至

10.26算法学习打卡

约 26 个字 25 行代码 预计阅读时间不到 1 分钟

10.26打卡 智乃的“K”叉树

今日牛客每日一题 打卡第5天

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n,k,pos;
    cin>>n;
    vector<int>a(n,0);
    int s,m;
    for(int i=0;i<n-1;++i){
        cin>>s>>m;
        a[s-1]++;
        a[m-1]++;
    }
    if(n==2){
        cout<<1<<" "<<1;
        return 0;
    }
    k=*max_element(a.begin(),a.end());
    pos=0;
    while(a[pos]==k)
    pos++;
    cout<<k-1<<" "<<pos+1;
    return 0;
}
// 64 位输出请用 printf("%lld")
希望坚持。

评论