답안 #61988

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
61988 2018-07-27T07:57:40 Z mahmoudbadawy 공장들 (JOI14_factories) C++17
0 / 100
8000 ms 121232 KB
#include "factories.h"
#include <bits/stdc++.h>
#define F first
#define S second

using namespace std;

const int N=500005;

long long dist[N],hi[N];
int dp[N][20];
vector<pair<int,int> > adj[N];

set< pair<long long,int> > ss[N];
int par[N],sz[N],del[N];
int nc;

void dfs0(int node,int pa=0)
{
	dp[node][0]=pa;
	for(int i=1;i<20;i++)
		dp[node][i]=dp[dp[node][i-1]][i-1];
	for(int i=0;i<adj[node].size();i++)
	{
		int x=adj[node][i].F,l=adj[node][i].S;
		if(x==pa) continue;
		dist[x]=dist[node]+l;
		hi[x]=hi[node]+1;
		dfs0(x,node);
	}
}

void dfs1(int node,int pa=0)
{
	sz[node]=1;
	nc++;
	for(int i=0;i<adj[node].size();i++)
	{
		int x=adj[node][i].F,l=adj[node][i].S;
		if(x==pa) continue;
		if(del[x]) continue;
		dfs1(x,node);
		sz[node]+=sz[x];
	}
}

int getCentroid(int node,int pa=0)
{
	for(int i=0;i<adj[node].size();i++)
	{
		int x=adj[node][i].F,l=adj[node][i].S;
		if(x==pa) continue;
		if(del[x]) continue;
		if(sz[x]>nc/2)
			return getCentroid(x,node);
	}
	return node;
}

void decompose(int node,int pa=-1)
{
	nc=0;
	dfs1(node,node);
	int centroid=getCentroid(node,node);
	if(pa==-1) pa=centroid;
	par[centroid]=pa;
	del[centroid]=1;
	for(int i=0;i<adj[centroid].size();i++)
	{
		int x=adj[centroid][i].F;
		if(!del[x]) decompose(x,centroid);
	}
}

int lca(int x,int y)
{
	if(hi[x]<hi[y]) swap(x,y);
	for(int i=19;i>=0;i--)
	{
		if(hi[x]-(1<<i)>=hi[y])
			x=dp[x][i];
	}
	if(x==y) return x;
	for(int i=19;i>=0;i--)
	{
		if(dp[x][i]!=dp[y][i])
		{
			x=dp[x][i]; y=dp[y][i];
		}
	}
	return dp[x][0];
}

long long getDist(int x,int y)
{
	return dist[x]+dist[y]-2*dist[lca(x,y)];
}

void add(int x)
{
	int lev=0;
	int z=x;
	while(true)
	{
		ss[z].insert({getDist(z,x),x});
		lev++;
		assert(lev<=20);
		if(par[z]==z) break;
		z=par[z];
	}
}

void delet(int x)
{
	int z=x;
	int lev=0;
	while(true)
	{
		ss[z].erase({getDist(z,x),x});
		lev++;
		assert(lev<=20);
		if(par[z]==z) break;
		z=par[z];
	}
}

long long query(int x)
{
	long long ans=(1LL<<60);
	int z=x;
	int lev=0;
	while(true)
	{
		if(ss[z].size()){
			pair<long long,int> pp=(*ss[z].begin());
			ans=min(ans,getDist(x,pp.S));
		}
		lev++;
		assert(lev<=20);
		if(par[z]==z) break;
		z=par[z];
	}
	return ans;
}

void Init(int N, int A[], int B[], int D[]) {
	
	for(int i=0;i<N-1;i++)
	{
		A[i]++; B[i]++;
		adj[A[i]].push_back({B[i],D[i]});
		adj[B[i]].push_back({A[i],D[i]});
	}
	dfs0(1);
	decompose(1);
}

long long Query(int S, int X[], int T, int Y[]) {
	long long ans=(1LL<<60);
	for(int i=0;i<S;i++) X[i]++;
	for(int i=0;i<T;i++) Y[i]++;
	for(int i=0;i<S;i++) add(X[i]);
	for(int i=0;i<T;i++) ans=min(ans,query(Y[i]));
	for(int i=0;i<S;i++) delet(X[i]);
	return ans;
}

Compilation message

factories.cpp: In function 'void dfs0(int, int)':
factories.cpp:23:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<adj[node].size();i++)
              ~^~~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs1(int, int)':
factories.cpp:37:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<adj[node].size();i++)
              ~^~~~~~~~~~~~~~~~~
factories.cpp:39:24: warning: unused variable 'l' [-Wunused-variable]
   int x=adj[node][i].F,l=adj[node][i].S;
                        ^
factories.cpp: In function 'int getCentroid(int, int)':
factories.cpp:49:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<adj[node].size();i++)
              ~^~~~~~~~~~~~~~~~~
factories.cpp:51:24: warning: unused variable 'l' [-Wunused-variable]
   int x=adj[node][i].F,l=adj[node][i].S;
                        ^
factories.cpp: In function 'void decompose(int, int)':
factories.cpp:68:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<adj[centroid].size();i++)
              ~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 36000 KB Output is correct
2 Correct 4615 ms 45164 KB Output is correct
3 Correct 6695 ms 45164 KB Output is correct
4 Execution timed out 8016 ms 47064 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 47064 KB Output is correct
2 Correct 6163 ms 120320 KB Output is correct
3 Execution timed out 8045 ms 121232 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 36000 KB Output is correct
2 Correct 4615 ms 45164 KB Output is correct
3 Correct 6695 ms 45164 KB Output is correct
4 Execution timed out 8016 ms 47064 KB Time limit exceeded
5 Halted 0 ms 0 KB -