Submission #386844

# Submission time Handle Problem Language Result Execution time Memory
386844 2021-04-07T13:27:05 Z uacoder123 Factories (JOI14_factories) C++14
0 / 100
6959 ms 204512 KB
#include "factories.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) lli(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef long long int lli;
typedef pair <lli,lli> ii;
typedef pair <ii,lli> iii;
typedef vector <lli> vi;
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int n;
vector<ii> al[500001];
lli dp[500001],par[500001][25],par1[500001],lev[500001],din[500001],dout[500001],t=0,sz[500001],vis[500001],vis1[500001],pre[500001],r;
void dfs(int node)
{
  sz[node]=1;
  vis1[node]=1;
  for(int i=0;i<al[node].size();++i)
  {
    if(vis1[al[node][i].F]+vis[al[node][i].F]==0)
    {
      dfs(al[node][i].F);
      sz[node]+=sz[al[node][i].F];
    }
  }
}
int tell(int node,int p,int to)
{
    for(int i=0;i<al[node].size();++i)
    {
      if(al[node][i].F!=p&&sz[al[node][i].F]>to/2&&vis[al[node][i].F]==0)
        return tell(al[node][i].F,node,to);
    }
    return node;
}
void dfs1(int node,int p,int cen)
{
  pre[node]=cen;
  for(int i=0;i<al[node].size();++i)
    if(al[node][i].F!=p&&vis[node]==0)
      dfs1(al[node][i].F,node,cen);
}
void dfs2(int node,int p,int l)
{
  par[node][0]=p;
  for(int i=1;i<25;++i)
    par[node][i]=par[par[node][i-1]][i-1];
  lev[node]=l;
  din[node]=t++;
  for(int i=0;i<al[node].size();++i)
  {
    if(al[node][i].F!=p)
      dfs2(al[node][i].F,node,l+al[node][i].S);
  }
  dout[node]=t++;
}
bool isa(int f,int s)
{
  return (din[f]<=din[s])&&(dout[f]>=dout[s]);
}
int lca(int f,int s)
{
  if(isa(f,s))
    return f;
  if(isa(s,f))
    return s;
  for(int i=24;i>=0;--i)
    if(!isa(par[f][i],s))
      f=par[f][i];
  return par[f][0];
}
void Init(int N, int A[], int B[], int D[]) 
{
  n=N;
  for(int i=0;i<n-1;++i)
  {
    al[A[i]].pb(mp(B[i],D[i]));
    al[B[i]].pb(mp(A[i],D[i]));
  }
  dfs2(0,0,0);
  int c=0;
  while(c!=n)
  {
    for(int i=0;i<n;++i)
    {
      if(vis[i]+vis1[i]==0)
      {
        dfs(i);
        int cen=tell(i,i,sz[i]);
        par1[cen]=pre[i];
        dfs1(i,i,cen);
        c++;
        vis[cen]=1;
        if(c==1)
          r=cen;
      }
    }
    memset(vis1,0,sizeof(vis1));
  }
  for(int i=0;i<n;++i)
    dp[i]=1000000000ll*5000000;
}

long long Query(int S, int X[], int T, int Y[]) 
{
  vi v;
  for(lli i=0;i<S;++i)
  {
  lli cur=X[i];
      while(1)
      {
        lli l=lca(X[i],cur);
        dp[cur]=min(dp[cur],lev[X[i]]+lev[cur]-2*lev[l]);
        v.pb(cur);
        if(cur==r)
          break;
        cur=par1[cur];
      }
    }
    lli ans=1000000000ll*5000000;
    for(lli i=0;i<T;++i)
    {

     lli cur=Y[i];
      while(1)
      {
        lli l=lca(Y[i],cur);
        ans=min(ans,dp[cur]+lev[Y[i]]+lev[cur]-2*lev[l]);
        if(cur==r)
          break;
        cur=par1[cur];
      }
    }
    while(v.size())
    {
      dp[v.back()]=1000000000ll*5000000;
      v.pop_back();
    }
  return ans;
}

Compilation message

factories.cpp: In function 'void dfs(int)':
factories.cpp:27:16: 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]
   27 |   for(int i=0;i<al[node].size();++i)
      |               ~^~~~~~~~~~~~~~~~
factories.cpp: In function 'int tell(int, int, int)':
factories.cpp:38: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]
   38 |     for(int i=0;i<al[node].size();++i)
      |                 ~^~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs1(int, int, int)':
factories.cpp:48:16: 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]
   48 |   for(int i=0;i<al[node].size();++i)
      |               ~^~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs2(int, int, int)':
factories.cpp:59:16: 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]
   59 |   for(int i=0;i<al[node].size();++i)
      |               ~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 16492 KB Output is correct
2 Correct 586 ms 25956 KB Output is correct
3 Incorrect 1044 ms 25828 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 16364 KB Output is correct
2 Correct 4569 ms 202320 KB Output is correct
3 Incorrect 6959 ms 204512 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 16492 KB Output is correct
2 Correct 586 ms 25956 KB Output is correct
3 Incorrect 1044 ms 25828 KB Output isn't correct
4 Halted 0 ms 0 KB -