Submission #61979

# Submission time Handle Problem Language Result Execution time Memory
61979 2018-07-27T07:18:19 Z mahmoudbadawy Factories (JOI14_factories) C++17
0 / 100
8000 ms 166868 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];

void dfs(int node,int pa=0)
{
	if(pa!=-1)
	{
		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;
		dfs(x,node);
	}
}

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 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]});
	}
	dfs(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++)
		for(int j=0;j<T;j++)
			ans=min(ans,getDist(X[i],Y[j]));
	return ans;
}

Compilation message

factories.cpp: In function 'void dfs(int, int)':
factories.cpp:22:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<adj[node].size();i++)
              ~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 209 ms 12408 KB Output is correct
2 Execution timed out 8021 ms 20836 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 20836 KB Output is correct
2 Correct 2601 ms 90848 KB Output is correct
3 Correct 6652 ms 109644 KB Output is correct
4 Correct 1642 ms 127788 KB Output is correct
5 Correct 6087 ms 163216 KB Output is correct
6 Correct 5501 ms 166868 KB Output is correct
7 Execution timed out 8023 ms 166868 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 209 ms 12408 KB Output is correct
2 Execution timed out 8021 ms 20836 KB Time limit exceeded
3 Halted 0 ms 0 KB -