Submission #708633

# Submission time Handle Problem Language Result Execution time Memory
708633 2023-03-12T05:36:29 Z ToroTN Factories (JOI14_factories) C++14
15 / 100
6339 ms 226436 KB
#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
//#include "grader.cpp"
#define ll long long
#define pb push_back
#define X first
#define Y second
vector<pair<ll,ll> > v[500005];
ll n,lv[500005],dis[500005][25],par[200005][25];
void dfs(ll u,ll p)
{
    for(int i=0;i<v[u].size();i++)
    {
        ll node=v[u][i].X,cost=v[u][i].Y;
        if(node==p)continue;
        lv[node]=lv[u]+1;
        dis[node][0]=cost;
        par[node][0]=u;
        dfs(node,u);
    }
}
pair<ll,ll> lca(ll a,ll b)
{
    ll sum=0;
    if(lv[a]<lv[b])swap(a,b);
    for(int i=20;i>=0;i--)
    {
        if(lv[par[a][i]]>=lv[b])
        {
            sum+=dis[a][i],a=par[a][i];
        }
    }
    if(a==b)return {a,sum};
    for(int i=20;i>=0;i--)
    {
        if(par[a][i]!=par[b][i])
        {
            sum+=dis[a][i],sum+=dis[b][i];
            a=par[a][i],b=par[b][i];
        }
    }
    sum+=dis[a][0],sum+=dis[b][0];
    return {par[a][0],sum};
}
ll sz[500005],vis[500005],par_cen[500005];
ll dfs_sz(ll u,ll p)
{
    sz[u]=1;
    for(int i=0;i<v[u].size();i++)
    {
        ll node=v[u][i].X;
        if(node==p||vis[node])continue;
        sz[u]+=dfs_sz(node,u);
    }
    return sz[u];
}
ll find_cen(ll u,ll p,ll size)
{
    for(int i=0;i<v[u].size();i++)
    {
        ll node=v[u][i].X;
        if(node==p||vis[node])continue;
        if(sz[node]>size/2)return find_cen(node,u,size);
    }
    return u;
}
void centroid(ll u,ll p)
{
    dfs_sz(u,u);
    ll cen=find_cen(u,u,sz[u]);
    vis[cen]=1;
    par_cen[cen]=p;
    for(int i=0;i<v[cen].size();i++)
    {
        ll node=v[cen][i].X;
        if(node==p||vis[node])continue;
        centroid(node,cen);
    }
}
void Init(int N, int A[], int B[], int D[]) 
{
    n=N;
    for(int i=0;i<n-1;i++)
    {
        v[A[i]+1].pb({B[i]+1,D[i]});
        v[B[i]+1].pb({A[i]+1,D[i]});
    }
    par[1][0]=1;
    dfs(1,1);
    for(int i=1;i<=20;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dis[j][i]=dis[j][i-1]+dis[par[j][i-1]][i-1];
            par[j][i]=par[par[j][i-1]][i-1];
        }
    }
    dfs_sz(1,1);
    centroid(1,0);
}
multiset<ll> ms[500005];
void update(ll num,ll type)
{
    ll u=num;
    while(1)
    {
        if(type==0)
        {
            ms[u].insert(lca(u,num).Y);
        }else
        {
            ms[u].erase(ms[u].find(lca(u,num).Y));
        }
        u=par_cen[u];
        if(u==0)break;
    }
}
ll query(ll num)
{
    ll u=num,mn=1e18;
    while(1)
    {
        if(ms[u].size()>0)
        {
            mn=min(mn,lca(num,u).Y+(*ms[u].begin()));
        }
        u=par_cen[u];
        if(u==0)break;
    }
    return mn;
}
long long Query(int S, int X[], int T, int Y[]) 
{
    for(int i=0;i<S;i++)
    {
        update(X[i]+1,0);
    }
    ll ans=1e18;
    for(int i=0;i<T;i++)
    {
        ans=min(ans,query(Y[i]+1));
    }
    for(int i=0;i<S;i++)
    {
        update(X[i]+1,1);
    }
    return ans;
}

Compilation message

factories.cpp: In function 'void dfs(long long int, long long int)':
factories.cpp:13:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for(int i=0;i<v[u].size();i++)
      |                 ~^~~~~~~~~~~~
factories.cpp: In function 'long long int dfs_sz(long long int, long long int)':
factories.cpp:50:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |     for(int i=0;i<v[u].size();i++)
      |                 ~^~~~~~~~~~~~
factories.cpp: In function 'long long int find_cen(long long int, long long int, long long int)':
factories.cpp:60:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for(int i=0;i<v[u].size();i++)
      |                 ~^~~~~~~~~~~~
factories.cpp: In function 'void centroid(long long int, long long int)':
factories.cpp:74:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for(int i=0;i<v[cen].size();i++)
      |                 ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 64 ms 36192 KB Output is correct
2 Correct 2680 ms 52232 KB Output is correct
3 Correct 4235 ms 51848 KB Output is correct
4 Correct 4855 ms 53536 KB Output is correct
5 Correct 6324 ms 52660 KB Output is correct
6 Correct 690 ms 48116 KB Output is correct
7 Correct 4152 ms 48240 KB Output is correct
8 Correct 4529 ms 48412 KB Output is correct
9 Correct 6339 ms 49168 KB Output is correct
10 Correct 781 ms 48232 KB Output is correct
11 Correct 4132 ms 48472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 36044 KB Output is correct
2 Incorrect 3760 ms 226436 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 64 ms 36192 KB Output is correct
2 Correct 2680 ms 52232 KB Output is correct
3 Correct 4235 ms 51848 KB Output is correct
4 Correct 4855 ms 53536 KB Output is correct
5 Correct 6324 ms 52660 KB Output is correct
6 Correct 690 ms 48116 KB Output is correct
7 Correct 4152 ms 48240 KB Output is correct
8 Correct 4529 ms 48412 KB Output is correct
9 Correct 6339 ms 49168 KB Output is correct
10 Correct 781 ms 48232 KB Output is correct
11 Correct 4132 ms 48472 KB Output is correct
12 Correct 22 ms 36044 KB Output is correct
13 Incorrect 3760 ms 226436 KB Output isn't correct
14 Halted 0 ms 0 KB -