Submission #1012356

# Submission time Handle Problem Language Result Execution time Memory
1012356 2024-07-02T03:51:40 Z DucNguyen2007 Mag (COCI16_mag) C++14
48 / 120
429 ms 262144 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=1e6+5;
const ll inf=2e18;
ll n,a[maxN+1],dp[maxN+1][2],g[maxN+1],mxn=0,g1[maxN+1];
vector<ll> adj[maxN+1],vec[maxN+1];
void dfs(ll u,ll p)
{
    for(auto v:adj[u])
    {
        if(v!=p)
        {
            vec[u].push_back(v);
        }
    }
    vector<vector<ll>> f(vec[u].size()+1,vector<ll>(2));
    vector<ll> vt;
    for(int i=0;i<=vec[u].size();i++)
    {
        f[i][0]=f[i][1]=-inf;
    }
    f[0][0]=f[0][1]=(a[u]==1);
    ll mx=0;
    g1[u]=0;
    for(int i=0;i<vec[u].size();i++)
    {
        ll v=vec[u][i];
        dfs(v,u);
        if(a[u]!=1)
        {
            f[i+1][0]=f[i+1][1]=0;
        }
        else
        {
            f[i+1][0]=max(f[i][0],dp[v][0]+1);
            f[i+1][1]=max(f[i][1],dp[v][0]+mx);
        }
        vt.push_back(dp[v][0]);
        mx=max(mx,f[i+1][0]);
        g1[u]=max(g1[u],dp[v][0]);
    }
    sort(vt.begin(),vt.end());
    if(vt.size()>1)
    {
        g[u]=min(vt.back(),vt[vt.size()-2]);
    }
    else g[u]=0;
    dp[u][0]=f[vec[u].size()][0];
    dp[u][1]=f[vec[u].size()][1];
}
void Dfs(ll u,ll p,ll res)
{
    if(a[u]==2)
    {
        mxn=max(mxn,min(res,g1[u]));
        res=0;
    }
    for(auto v:adj[u])
    {
        if(v!=p)
        {
            Dfs(v,u,res+(a[v]==1));
        }
    }
}
int main()
{
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ld mn=(ld)inf;
    ll sl,val;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        ll u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(mn>a[i])
        {
            mn=a[i];
            val=a[i];
            sl=1;
        }
    }
    dfs(1,0);
    for(int i=1;i<=n;i++)
    {
        ld cur=(ld)1.0*max(dp[i][0],dp[i][1]);
        if(cur==0)
        {
            continue;
        }
        ld tmp=(ld)1.0*1/cur;
        //cout<<i<<" "<<cur<<'\n';
        if(mn>tmp)
        {
            mn=tmp;
            val=1;
            sl=max(dp[i][0],dp[i][1]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(a[i]==2)
        {
            ld cur=(ld)1.0*2*g[i]+1.0;
            ld tmp=(ld)1.0*2/cur;
            if(mn>tmp)
            {
                mn=tmp;
                val=2;
                sl=2*g[i]+1;
            }
        }
    }
    Dfs(1,0,(a[1]==1));
    ld cur=(ld)1.0*2*mxn+1.0;
    ld tmp=(ld)1.0*2/cur;
    if(mn>tmp)
    {
        mn=tmp;
        val=2;
        sl=2*mxn+1;
    }
    cout<<val<<"/"<<sl;
}
/*3
1 2
2 3
1 2 1*/
/*5
1 2
1 3
2 4
3 5
2 1 1 1 1*/

Compilation message

mag.cpp: In function 'void dfs(long long int, long long int)':
mag.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |     for(int i=0;i<=vec[u].size();i++)
      |                 ~^~~~~~~~~~~~~~~
mag.cpp:30:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for(int i=0;i<vec[u].size();i++)
      |                 ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 47448 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Incorrect 17 ms 47448 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 429 ms 253944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 47300 KB Output is correct
2 Runtime error 309 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 320 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 373 ms 148600 KB Output is correct
2 Correct 303 ms 133716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 333 ms 163628 KB Output is correct
2 Correct 73 ms 58304 KB Output is correct
3 Runtime error 314 ms 262144 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 69 ms 56916 KB Output is correct
2 Correct 373 ms 151124 KB Output is correct
3 Correct 293 ms 102472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 146852 KB Output is correct
2 Correct 357 ms 147540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 385 ms 151120 KB Output is correct
2 Correct 304 ms 95052 KB Output is correct