Submission #339768

# Submission time Handle Problem Language Result Execution time Memory
339768 2020-12-26T07:37:51 Z Bill_00 Factories (JOI14_factories) C++14
100 / 100
7405 ms 256612 KB
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#define MAX_N          500000
#define MAX_Q          100000
#define MAX_SUM_ST    1000000
#define MAX_VALUE  1000000000
#define ff first
#define ss second
#define pb push_back
#include "factories.h"
#define mp make_pair
typedef long long ll; 
using namespace std;
ll maincent,n,a[1000001],b[1000001],d[1000001],k=0,first[1000001],belong[25][1000001],sz[1000001],centroid,nearest[1000001],o=0,p[1000001];
bool vis[1000001];
vector<ll>child[1000001];
vector<ll>pre[1000001];
vector<int>vec;
vector<pair<ll,ll> >adj[1000001];
ll cnt[1000001],K=0,rr[1000001];
void dfs(int node,int par){
	K++;
	cnt[node]=K;
	for(int i=0;i<child[node].size();i++){
		dfs(child[node][i],node);
	}
	rr[node]=K;
}
void dfs1(ll node,ll par){
  sz[node]=1;
  for(int i=0;i<adj[node].size();i++){
    if(adj[node][i].ff!=par && vis[adj[node][i].ff]==0){
      dfs1(adj[node][i].ff,node);
      sz[node]+=sz[adj[node][i].ff];
    }
  }
}
void dfs3(ll node,ll par,ll dis,ll cent){
	// cout << cnt[node] << ' ' << cnt[cent] << '\n';
  pre[cent][cnt[node]-cnt[cent]]=dis;
  for(int i=0;i<adj[node].size();i++){
    if(adj[node][i].ff!=par && vis[adj[node][i].ff]==0){
      dfs3(adj[node][i].ff,node,dis+adj[node][i].ss,cent);
    }
  }
}
void dfs2(ll node){
	pre[node].resize(rr[node]-cnt[node]+1);
	dfs3(node,-1,0,node);
	vis[node]=1;
	for(int i=0;i<child[node].size();i++){
		dfs2(child[node][i]);
	}
}
void solve(ll node,ll par,ll level,ll parent){
	// cout << node << '\n';
  o++;
  dfs1(node,par);
  ll cent=node;
  while(1){
    bool f=true;
    for(int i=0;i<adj[cent].size();i++){
      if(sz[adj[cent][i].ff]<sz[cent] && vis[adj[cent][i].ff]==0 && sz[adj[cent][i].ff]*2>sz[node]){
        f=false;
        cent=adj[cent][i].ff;
        break;
      }
    }
    if(f) break;
  }
  if(level==1) maincent=cent;
  centroid=cent;
  // cout << cent << ' ' << level << '\n';
  p[cent]=parent;
  child[parent].pb(cent);
  vis[cent]=1;
  for(int i=0;i<adj[cent].size();i++){
    if(vis[adj[cent][i].ff]==0){
      solve(adj[cent][i].ff,-1,level+1,cent);
    }
  }
}
void Init(int N, int A[], int B[], int D[]) {
  n=(ll)N;
  for(int i=0;i<(N-1);i++){
    a[i]=(ll)A[i];
    b[i]=(ll)B[i];
    d[i]=(ll)D[i];
    adj[a[i]].pb(mp(b[i],d[i]));
    adj[b[i]].pb(mp(a[i],d[i]));
  }
  solve(0,-1,1,-1);
  for(int i=0;i<n;i++){
    nearest[i]=1e18;
  }
  dfs(maincent,-1);
  memset(vis,0,sizeof(vis));
  dfs2(maincent);
}
long long Query(int S, int X[], int T, int Y[]) {
  for(int i=0;i<S;i++){
    int XX=X[i];
    while(XX!=-1){
    	nearest[XX]=min(nearest[XX],pre[XX][cnt[X[i]]-cnt[XX]]);
    	// cout << nearest[XX] << ' ' << XX << '\n';
    	vec.pb(XX);
    	XX=p[XX];
    }
  }
  ll ans=2e18;
  for(int i=0;i<T;i++){
    int YY=Y[i];
    while(YY!=-1){
    	ans=min(ans,nearest[YY]+pre[YY][cnt[Y[i]]-cnt[YY]]);
    	YY=p[YY];
    }
  }
  for(int i=0;i<vec.size();i++){
  	nearest[vec[i]]=1e18;
  }
  vec.clear();
  return ans;
}

Compilation message

factories.cpp: In function 'void dfs(int, int)':
factories.cpp:25:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |  for(int i=0;i<child[node].size();i++){
      |              ~^~~~~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs1(ll, ll)':
factories.cpp:32: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]
   32 |   for(int i=0;i<adj[node].size();i++){
      |               ~^~~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs3(ll, ll, ll, ll)':
factories.cpp:42: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]
   42 |   for(int i=0;i<adj[node].size();i++){
      |               ~^~~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs2(ll)':
factories.cpp:52:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |  for(int i=0;i<child[node].size();i++){
      |              ~^~~~~~~~~~~~~~~~~~~
factories.cpp: In function 'void solve(ll, ll, ll, ll)':
factories.cpp:63: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]
   63 |     for(int i=0;i<adj[cent].size();i++){
      |                 ~^~~~~~~~~~~~~~~~~
factories.cpp:78: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]
   78 |   for(int i=0;i<adj[cent].size();i++){
      |               ~^~~~~~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:119:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |   for(int i=0;i<vec.size();i++){
      |               ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 57 ms 72192 KB Output is correct
2 Correct 442 ms 81004 KB Output is correct
3 Correct 502 ms 81076 KB Output is correct
4 Correct 490 ms 81388 KB Output is correct
5 Correct 547 ms 81516 KB Output is correct
6 Correct 321 ms 80748 KB Output is correct
7 Correct 502 ms 81132 KB Output is correct
8 Correct 506 ms 81132 KB Output is correct
9 Correct 543 ms 81644 KB Output is correct
10 Correct 319 ms 80780 KB Output is correct
11 Correct 502 ms 81004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 72076 KB Output is correct
2 Correct 3463 ms 200936 KB Output is correct
3 Correct 4968 ms 224276 KB Output is correct
4 Correct 1227 ms 163036 KB Output is correct
5 Correct 6006 ms 254860 KB Output is correct
6 Correct 5065 ms 225540 KB Output is correct
7 Correct 1856 ms 108908 KB Output is correct
8 Correct 613 ms 99292 KB Output is correct
9 Correct 2129 ms 114972 KB Output is correct
10 Correct 1892 ms 109804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 72192 KB Output is correct
2 Correct 442 ms 81004 KB Output is correct
3 Correct 502 ms 81076 KB Output is correct
4 Correct 490 ms 81388 KB Output is correct
5 Correct 547 ms 81516 KB Output is correct
6 Correct 321 ms 80748 KB Output is correct
7 Correct 502 ms 81132 KB Output is correct
8 Correct 506 ms 81132 KB Output is correct
9 Correct 543 ms 81644 KB Output is correct
10 Correct 319 ms 80780 KB Output is correct
11 Correct 502 ms 81004 KB Output is correct
12 Correct 50 ms 72076 KB Output is correct
13 Correct 3463 ms 200936 KB Output is correct
14 Correct 4968 ms 224276 KB Output is correct
15 Correct 1227 ms 163036 KB Output is correct
16 Correct 6006 ms 254860 KB Output is correct
17 Correct 5065 ms 225540 KB Output is correct
18 Correct 1856 ms 108908 KB Output is correct
19 Correct 613 ms 99292 KB Output is correct
20 Correct 2129 ms 114972 KB Output is correct
21 Correct 1892 ms 109804 KB Output is correct
22 Correct 4159 ms 203360 KB Output is correct
23 Correct 4326 ms 208932 KB Output is correct
24 Correct 6220 ms 230420 KB Output is correct
25 Correct 6323 ms 232844 KB Output is correct
26 Correct 6253 ms 226608 KB Output is correct
27 Correct 7405 ms 256612 KB Output is correct
28 Correct 1492 ms 168452 KB Output is correct
29 Correct 6292 ms 226776 KB Output is correct
30 Correct 6237 ms 226384 KB Output is correct
31 Correct 6182 ms 226668 KB Output is correct
32 Correct 2171 ms 122456 KB Output is correct
33 Correct 620 ms 104668 KB Output is correct
34 Correct 1337 ms 107500 KB Output is correct
35 Correct 1350 ms 107660 KB Output is correct
36 Correct 1842 ms 111724 KB Output is correct
37 Correct 1830 ms 111724 KB Output is correct