Submission #1063713

# Submission time Handle Problem Language Result Execution time Memory
1063713 2024-08-18T00:04:55 Z vjudge1 Factories (JOI14_factories) C++17
0 / 100
58 ms 90960 KB
#include "factories.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;

int up(int n, int k);
void DFS(int n);
void dijkstra(int n);

ll inf = LONG_LONG_MAX;
int n;
vector<vector<pair<int, ll>>> adj(500005);
vector<bool> vis(500005, 0);
vector<ll> dis(500005, inf), h(500005,0), parent(500005,0);
vector<pair<ll,ll>> outdeg(500005,{0,0});
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
vector<vector<int>> sparseT(500005, vector<int>(19));

void Init(int N, int A[], int B[], int D[])
{
  n = N;
  for (int i = 0; i < N - 1; i++)
  {
    adj[A[i]].pb({B[i], D[i]});
    adj[B[i]].pb({A[i], D[i]});
    outdeg[A[i]].first++;
    outdeg[A[i]].second = A[i];
    outdeg[B[i]].first++;
    outdeg[B[i]].second = B[i];
  }
  sort(outdeg.rbegin(),outdeg.rend());
  //cout<<outdeg[0].second<<endl;
  
  dijkstra(outdeg[0].second);
  parent[outdeg[0].second] = outdeg[0].second;
  DFS(outdeg[0].second);
  for(int i=0; i < N; i++){
    sparseT[i][0] = parent[i];
  }
  for(int j=1; j < 20; j++){
    for(int i=0; i < N; i++){
      sparseT[i][j] = parent[sparseT[i][j-1]];
    }
  }
}

long long Query(int S, int X[], int T, int Y[])
{
  ll ans = inf;
  for (int i = 0; i < S; i++){
    for(int j = 0; j < T; j++){
      for(int k = 1; k <= max(h[X[i]],h[Y[i]]); k++){
        if(up(X[i],k) == up(Y[i],k)){
          if(up(X[i],k) == outdeg[0].second){
            ans = min(ans,dis[X[i]]+dis[Y[i]]);
          }else{
            ans = min(ans,(dis[X[i]]+dis[Y[i]])-dis[up(X[i],k)]);
          }
          break;
        }
      }
    }
  }

  return ans;
}

int up(int n, int k){
  int act=n;
  for(int p=0; p < 20; p++){
    if(k & (1<<p)){
      act = sparseT[act][p];
    }
  }

  return act;
}

void DFS(int n){
  vis[n] = 1;

  for(auto [x,w]: adj[n]){
    if(vis[x])continue;
    h[x] = h[n]+1;
    parent[x] = n;
    DFS(x);
  }
}

void dijkstra(int n)
{
  dis[n] = 0;
  pq.push({0,n});

  while (!pq.empty())
  {
    int a = pq.top().second;
    pq.pop();

    if (vis[a])
      continue;
    vis[a] = 1;

    for (auto [b, w] : adj[a])
    {
      if (dis[b] > dis[a] + w)
      {
        dis[b] = dis[a] + w;
        pq.push({dis[b], b});
      }
    }
  }
}
# Verdict Execution time Memory Grader output
1 Incorrect 58 ms 90960 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 90704 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 58 ms 90960 KB Output isn't correct
2 Halted 0 ms 0 KB -