/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.02 MiB
#2 Accepted 36ms 8.102 MiB
#3 Accepted 38ms 8.066 MiB
#4 Accepted 37ms 8.02 MiB
#5 Accepted 37ms 8.02 MiB
#6 Accepted 37ms 8.105 MiB
#7 Accepted 36ms 8.02 MiB
#8 Accepted 34ms 8.02 MiB
#9 Accepted 35ms 8.02 MiB
#10 Accepted 39ms 8.016 MiB
#11 Accepted 35ms 7.895 MiB
#12 Accepted 38ms 8.066 MiB
#13 Accepted 36ms 8.02 MiB
#14 Accepted 66ms 8.066 MiB
#15 Accepted 35ms 8.062 MiB
#16 Accepted 36ms 8.086 MiB
#17 Accepted 36ms 8.023 MiB
#18 Accepted 38ms 8.02 MiB
#19 Accepted 36ms 8.02 MiB
#20 Accepted 54ms 8.066 MiB
#21 Accepted 3ms 2.02 MiB
#22 Accepted 2ms 2.031 MiB
#23 Accepted 33ms 7.789 MiB
#24 Accepted 30ms 7.77 MiB
#25 Accepted 3ms 2.02 MiB
#26 Accepted 11ms 3.316 MiB

Code

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define vi vector<int>
#define vll vector<long long>
#define N 200001
using namespace std;
const int mod = 1e9+7;
//const int mod = 998244353;

class Int
{
public:
    ll val;
    Int(ll b=0)
    {
        val = b%mod;
        if(val<0) val+=mod;
    }
    Int& operator+=(Int const &b)
    {
        val = (val+b.val)%mod;
        return *this;
    }
    Int& operator-=(Int const &b)
    {
        val = (val-b.val)%mod;
        if(val<0) val+=mod;
        return *this;
    }
    Int& operator*=(Int const &b)
    {
        val = (val*b.val)%mod;
        return *this;
    }
    friend Int Pow(Int a, int b)
    {
        Int res=1;
        while(b)
        {
            if(b&1) res*=a;
            a*=a;
            b>>=1;
        }
        return res;
    }
    friend Int inv(Int a)
    {
        return Pow(a,mod-2);
    }
    friend Int operator+(Int a, Int const b) { return a += b; }
    friend Int operator-(Int a, Int const b) { return a -= b; }
    friend Int operator-(Int const a) { return 0 - a; }
    friend Int operator*(Int a, Int const b) { return a *= b; }
    friend bool operator != (Int a, Int b) {return a.val!=b.val;}
    friend std::ostream& operator<<(std::ostream& os, Int const& a) {return os << a.val;}
};

Int factor[N];
Int C(int m, int n)
{
    if(n>m || n<0) return 0;
    if(n==0 || n==m) return 1;
    return factor[m]*inv(factor[m-n]*factor[n]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;
    if(n==1)
    {
        cout<<1<<' '<<0<<'\n';
        return 0;
    }

    vi g[n+1], deg(n+1,0);
    vector<bool> vis(n,false);
    for(int i=0; i<n-1; i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
        deg[u]++, deg[v]++;
    }

    queue<int> q;
    for(int i=1; i<=n; i++)
        if(g[i].size()==1) q.push(i);

    int remain=n, turn=0;
    while(!q.empty())
    {
        if(remain<=2) break;
        int k=q.size();
        turn++;
        while(k--)
        {
            int u=q.front();
            q.pop();
            vis[u] = true;
            remain--;

            for(auto &v: g[u])
            {
                if(!vis[v] && --deg[v]==1)
                    q.push(v);
            }
        }
    }

    if(remain==1)
        cout<<turn<<' '<<q.front()<<'\n';
    else
    {
        int a=q.front(); q.pop();
        int b=q.front();
        cout<<turn+1<<' '<<max(a,b)<<'\n';
    }
    /*int T;
    cin>>T;

    while(T--)
    {

    }*/
}







Information

Submit By
Type
Submission
Problem
P1069 Vaccination
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-05 05:22:40
Judged At
2024-10-05 05:22:40
Judged By
Score
100
Total Time
66ms
Peak Memory
8.105 MiB