답안 #151247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
151247 2019-09-02T10:15:08 Z TadijaSebez 공장들 (JOI14_factories) C++11
100 / 100
4295 ms 442172 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const ll inf=7e18;
const int N=1000050;
const int L=21;
int n;
vector<pair<int,int>> E[N],B[N];
void AddEdge(int u, int v, int w){ B[u].pb({v,w});B[v].pb({u,w});}
void Build(int u, int p)
{
	vector<pair<int,int>> es;
	for(auto e:E[u]) if(e.first!=p) es.pb(e);
	int last=u;
	for(int i=0;i<es.size();i++)
	{
		AddEdge(last,es[i].first,es[i].second);
		if(i+2<es.size())
		{
			n++;
			AddEdge(last,n,0);
			last=n;
		}
	}
	for(auto e:es) Build(e.first,u);
}
int go[N][L];
ll dep[N][L];
bool was[N];
void DFS(int u, int p, int l, int g, ll d)
{
	go[u][l]=g;
	dep[u][l]=d;
	for(auto e:B[u]) if(e.first!=p && !was[e.first]) DFS(e.first,u,l,g,d+e.second);
}
int sz[N];
void DFSSZ(int u, int p){ sz[u]=1;for(auto e:B[u]) if(e.first!=p && !was[e.first]) DFSSZ(e.first,u),sz[u]+=sz[e.first];}
int Find(int u, int p, int n){ for(auto e:B[u]) if(e.first!=p && !was[e.first] && sz[e.first]*2>n) return Find(e.first,u,n);return u;}
int Find(int u){ DFSSZ(u,u);return Find(u,u,sz[u]);}
int cen_dep[N];
void Decompose(int u, int l)
{
	was[u=Find(u)]=1;
	cen_dep[u]=l;
	go[u][l]=3;
	for(int i=0;i<B[u].size();i++) if(!was[B[u][i].first])
	{
		DFS(B[u][i].first,u,l,i,B[u][i].second);
	}
	for(auto e:B[u]) if(!was[e.first]) Decompose(e.first,l+1);
}
void Init(int N, int A[], int B[], int D[])
{
	n=N;
	for(int i=0;i<N-1;i++) A[i]++,B[i]++,E[A[i]].pb({B[i],D[i]}),E[B[i]].pb({A[i],D[i]});
	Build(1,0);
	Decompose(1,0);
}
void ckmn(ll &a, ll b){ a=min(a,b);}
int nxt[N][3],root,tsz;
ll mn[N][4];
void Clear(){ tsz=root=0;}
void cl(int c){ nxt[c][0]=nxt[c][1]=nxt[c][2]=0;mn[c][0]=mn[c][1]=mn[c][2]=mn[c][3]=inf;}
void Set(int &c, int u, int l=0)
{
    if(!c) c=++tsz,cl(c);
	ckmn(mn[c][go[u][l]],dep[u][l]);
	if(cen_dep[u]==l) return;
	Set(nxt[c][go[u][l]],u,l+1);
}
ll Get(int c, int u, int l=0)
{
	if(!c) return inf;
	ll ans=inf;
	for(int i=0;i<4;i++) if(i!=go[u][l]) ckmn(ans,dep[u][l]+mn[c][i]);
	if(cen_dep[u]!=l) ckmn(ans,Get(nxt[c][go[u][l]],u,l+1));
	return ans;
}
ll Query(int S, int X[], int T, int Y[])
{
	Clear();
	for(int i=0;i<S;i++) Set(root,X[i]+1);
	ll ans=inf;
	for(int i=0;i<T;i++) ans=min(ans,Get(root,Y[i]+1));
	return ans;
}

Compilation message

factories.cpp: In function 'void Build(int, int)':
factories.cpp:17:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<es.size();i++)
              ~^~~~~~~~~~
factories.cpp:20:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i+2<es.size())
      ~~~^~~~~~~~~~
factories.cpp: In function 'void Decompose(int, int)':
factories.cpp:48:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<B[u].size();i++) if(!was[B[u][i].first])
              ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 47864 KB Output is correct
2 Correct 560 ms 67088 KB Output is correct
3 Correct 559 ms 66980 KB Output is correct
4 Correct 469 ms 66972 KB Output is correct
5 Correct 583 ms 67192 KB Output is correct
6 Correct 597 ms 68560 KB Output is correct
7 Correct 554 ms 66808 KB Output is correct
8 Correct 552 ms 66928 KB Output is correct
9 Correct 584 ms 67192 KB Output is correct
10 Correct 608 ms 68728 KB Output is correct
11 Correct 564 ms 66808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 47740 KB Output is correct
2 Correct 2703 ms 280744 KB Output is correct
3 Correct 3159 ms 259220 KB Output is correct
4 Correct 3552 ms 423856 KB Output is correct
5 Correct 3578 ms 292096 KB Output is correct
6 Correct 3249 ms 261268 KB Output is correct
7 Correct 1141 ms 108048 KB Output is correct
8 Correct 1255 ms 142040 KB Output is correct
9 Correct 1245 ms 111688 KB Output is correct
10 Correct 1185 ms 109412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 47864 KB Output is correct
2 Correct 560 ms 67088 KB Output is correct
3 Correct 559 ms 66980 KB Output is correct
4 Correct 469 ms 66972 KB Output is correct
5 Correct 583 ms 67192 KB Output is correct
6 Correct 597 ms 68560 KB Output is correct
7 Correct 554 ms 66808 KB Output is correct
8 Correct 552 ms 66928 KB Output is correct
9 Correct 584 ms 67192 KB Output is correct
10 Correct 608 ms 68728 KB Output is correct
11 Correct 564 ms 66808 KB Output is correct
12 Correct 47 ms 47740 KB Output is correct
13 Correct 2703 ms 280744 KB Output is correct
14 Correct 3159 ms 259220 KB Output is correct
15 Correct 3552 ms 423856 KB Output is correct
16 Correct 3578 ms 292096 KB Output is correct
17 Correct 3249 ms 261268 KB Output is correct
18 Correct 1141 ms 108048 KB Output is correct
19 Correct 1255 ms 142040 KB Output is correct
20 Correct 1245 ms 111688 KB Output is correct
21 Correct 1185 ms 109412 KB Output is correct
22 Correct 3399 ms 293900 KB Output is correct
23 Correct 3285 ms 299772 KB Output is correct
24 Correct 3796 ms 272968 KB Output is correct
25 Correct 3763 ms 276876 KB Output is correct
26 Correct 3630 ms 267496 KB Output is correct
27 Correct 4295 ms 291496 KB Output is correct
28 Correct 4161 ms 442172 KB Output is correct
29 Correct 3622 ms 266848 KB Output is correct
30 Correct 4008 ms 266108 KB Output is correct
31 Correct 3587 ms 267292 KB Output is correct
32 Correct 1429 ms 115500 KB Output is correct
33 Correct 1441 ms 147804 KB Output is correct
34 Correct 1137 ms 110748 KB Output is correct
35 Correct 1106 ms 110632 KB Output is correct
36 Correct 1176 ms 106648 KB Output is correct
37 Correct 1171 ms 106624 KB Output is correct