Submission #19541376


Source Code Expand

Copy
/* in the name of Anton */

/*
  Compete against Yourself.
  Author - Aryan Choudhary (@aryanc403)
*/

#ifdef ARYANC403
    #include "/home/aryan/codes/PastCodes/template/header.h"
#else
    #pragma GCC optimize ("Ofast")
    #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
    #pragma GCC optimize ("-ffloat-store")
    #include<iostream>
    #include<bits/stdc++.h>
    #define dbg(args...)
#endif

using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) ((lli)(x).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"

typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;


const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403()
{
#ifdef ARYANC403
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end_time-start_time;
    cerr<<"Time Taken : "<<diff.count()<<"\n";
#endif
}

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || ( a.X==b.X && a.Y <= b.Y ));   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;
// const lli maxN = 1000000007L;

    lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
    lli m;
    string s;
    vector<vi> e;
    lli A,B;
    //priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

lli dfs(lli u,lli p)
{
    vi b;
    lli cnt=0;
    for(auto x:e[u])
    {
        if(x==p)
            continue;
        b.pb(dfs(x,u));
        cnt++;
    }
    dbg(u,p,e[u],b);
    A+=cnt/2;
    if(sz(b)%2==0)
        b.pb(0);
    sort(all(b));

    if(b.back()>B)
        A=INF;
    auto chk=[b,B](const lli m)-> bool {
        lli i=0,j=sz(b)-1;
        if(i==m)
            i++;
        if(j==m)
            j--;
        while(i<j)
        {
            if(b[i]+b[j]>B)
                return false;
            i++;j--;
            if(i==m)
                i++;
            if(j==m)
                j--;
        }
        return true;
    };
    lli l=-1,r=sz(b);
    while(r-l>1)
    {
        const lli m=(r+l)/2;
        if(chk(m))
            r=m;
        else
            l=m;
    }
    if(r==sz(b))
    {
        r--;
        A=INF;
    }
    return b[r]+1;
}

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
// cin>>T;while(T--)
{

    cin>>n;
    e.resize(n);
    fo(i,n-1)
    {
        cin>>u>>v;
        u--;v--;
        e[u].pb(v);
        e[v].pb(u);
    }

    if(n==2)
    {
        cout<<"1 1"<<endl;
        return 0;
    }

    lli rt=0;
    fo(rt,n)
    {
        if(sz(e[rt])!=1)
            continue;
        break;
    }

    B=INF;
    A=1;
    dfs(rt,-1);
    dbg(A,B);
    const lli noA=A;
    lli l=0,r=n;
    while(r-l>1)
    {
        B=(l+r)/2;
        A=1;
        if(dfs(rt,-1)-1>B)
            A=INF;
        dbg(B,A,noA);
        if(A<=noA)
            r=B;
        else
            l=B;
    }
    cout<<noA<<" "<<r<<endl;
}   aryanc403();
    return 0;
}

Submission Info

Submission Time
Task F - Christmas Tree
User aryanc403
Language C++ (GCC 9.2.1)
Score 900
Code Size 4059 Byte
Status AC
Exec Time 277 ms
Memory 22692 KB

Compile Error

./Main.cpp: In function ‘lli dfs(lli, lli)’:
./Main.cpp:108:17: warning: capture of variable ‘B’ with non-automatic storage duration
  108 |     auto chk=[b,B](const lli m)-> bool {
      |                 ^
./Main.cpp:86:11: note: ‘lli B’ declared here
   86 |     lli A,B;
      |           ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 3
AC × 75
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, 65.txt, 66.txt, 67.txt, 68.txt, 69.txt, 70.txt, 71.txt, 72.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 245 ms 9184 KB
02.txt AC 254 ms 9156 KB
03.txt AC 258 ms 9156 KB
04.txt AC 257 ms 9188 KB
05.txt AC 254 ms 22636 KB
06.txt AC 242 ms 22692 KB
07.txt AC 268 ms 12924 KB
08.txt AC 267 ms 15528 KB
09.txt AC 277 ms 15088 KB
10.txt AC 259 ms 16040 KB
11.txt AC 260 ms 10208 KB
12.txt AC 263 ms 10112 KB
13.txt AC 260 ms 10168 KB
14.txt AC 264 ms 10208 KB
15.txt AC 250 ms 9212 KB
16.txt AC 254 ms 9172 KB
17.txt AC 245 ms 8944 KB
18.txt AC 225 ms 8888 KB
19.txt AC 204 ms 8860 KB
20.txt AC 200 ms 8996 KB
21.txt AC 198 ms 9156 KB
22.txt AC 204 ms 10512 KB
23.txt AC 226 ms 15000 KB
24.txt AC 232 ms 17992 KB
25.txt AC 255 ms 9252 KB
26.txt AC 246 ms 9264 KB
27.txt AC 242 ms 8948 KB
28.txt AC 225 ms 8908 KB
29.txt AC 210 ms 8924 KB
30.txt AC 206 ms 8892 KB
31.txt AC 208 ms 9172 KB
32.txt AC 209 ms 10792 KB
33.txt AC 227 ms 21072 KB
34.txt AC 223 ms 20260 KB
35.txt AC 253 ms 9124 KB
36.txt AC 259 ms 9648 KB
37.txt AC 253 ms 9124 KB
38.txt AC 236 ms 8996 KB
39.txt AC 212 ms 8924 KB
40.txt AC 199 ms 8888 KB
41.txt AC 207 ms 9172 KB
42.txt AC 211 ms 10212 KB
43.txt AC 214 ms 16848 KB
44.txt AC 227 ms 16360 KB
45.txt AC 256 ms 9216 KB
46.txt AC 239 ms 9188 KB
47.txt AC 258 ms 9124 KB
48.txt AC 240 ms 8888 KB
49.txt AC 208 ms 8892 KB
50.txt AC 210 ms 8856 KB
51.txt AC 209 ms 8948 KB
52.txt AC 203 ms 10008 KB
53.txt AC 220 ms 15460 KB
54.txt AC 223 ms 15288 KB
55.txt AC 267 ms 11276 KB
56.txt AC 264 ms 11240 KB
57.txt AC 264 ms 11220 KB
58.txt AC 267 ms 11180 KB
59.txt AC 254 ms 12888 KB
60.txt AC 253 ms 11868 KB
61.txt AC 255 ms 14296 KB
62.txt AC 252 ms 12852 KB
63.txt AC 260 ms 13412 KB
64.txt AC 257 ms 16876 KB
65.txt AC 244 ms 15344 KB
66.txt AC 242 ms 19140 KB
67.txt AC 3 ms 3436 KB
68.txt AC 2 ms 3472 KB
69.txt AC 2 ms 3584 KB
70.txt AC 2 ms 3440 KB
71.txt AC 2 ms 3504 KB
72.txt AC 2 ms 3444 KB
s1.txt AC 2 ms 3536 KB
s2.txt AC 2 ms 3476 KB
s3.txt AC 2 ms 3444 KB