Submission #496628

# Submission time Handle Problem Language Result Execution time Memory
496628 2021-12-21T16:57:48 Z dsyz Roadside Advertisements (NOI17_roadsideadverts) C++17
7 / 100
37 ms 30164 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (50005)
ll V;
vector<pair<ll,ll> > v[MAXN];
ll parent[20][MAXN];
ll depth[MAXN];
ll dist[MAXN];
ll visited[MAXN];
void distdfs(ll x){
	visited[x] = 1;
	for(auto u : v[x]){
		if(visited[u.first] == 0){
			dist[u.first] = dist[x] + u.second;
			visited[u.first] = 1;
			distdfs(u.first);
		}
	}
}
void dfs(ll x,ll nodeparent,ll nodedepth){
	parent[0][x] = nodeparent;
	depth[x] = nodedepth;
	for(ll i = 0;i < v[x].size();i++){
		if(v[x][i].first != nodeparent){
			dfs(v[x][i].first,x,nodedepth + 1);
		}
	}
}
void lcainit(){
	for(ll i = 1;i < 20;i++){
		for(ll j = 0;j < V;j++){
			parent[i][j] = parent[i - 1][parent[i - 1][j]];
		}
	}
}
long long lca(ll u,ll v){
	if(depth[u] > depth[v]){
			swap(u,v);
		}
	ll diff = depth[v] - depth[u];
	for(ll i = 0;i < 20;i++){
		if(diff & (1<<i)){
			v = parent[i][v];
		} 
	}
	if(u == v){
	   return u;
	} 
	for(ll i = 19;i >= 0;i--){
		if(parent[i][u] != parent[i][v]){
			u = parent[i][u];
			v = parent[i][v];
	    }
	}
	return parent[0][u];
}
int main() {
  ios_base::sync_with_stdio(false);cin.tie(0);
  cin>>V;
  for(ll i = 0;i < (V - 1);i++){
	  ll a,b,w;
	  cin>>a>>b>>w;
	  v[a].push_back(make_pair(b,w));
	  v[b].push_back(make_pair(a,w));	  
  }
  dist[0] = 0;
  visited[0] = 1;
  distdfs(0);
  dfs(0,0,0);
  lcainit();
  ll Q;
  cin>>Q;
  for(ll i = 0;i < Q;i++){
	  ll arr[5];
	  for(ll i = 0;i < 5;i++){
		  cin>>arr[i];
	  }
	  ll p[5];
	  memset(p,-1,sizeof(p));
	  for(ll j = 0;j < 5;j++){
		  for(ll z = 0;z < 20;z++){
			  if(parent[z][arr[j]] == arr[0]){
				  p[j] = arr[0];
				  break;
			  }else if(parent[z][arr[j]] == arr[1]){
				  p[j] = arr[1];
				  break;
			  }else if(parent[z][arr[j]] == arr[2]){
				  p[j] = arr[2];
				  break;
			  }else if(parent[z][arr[j]] == arr[3]){
				  p[j] = arr[3];
				  break;
			  }else if(parent[z][arr[j]] == arr[4]){
				  p[j] = arr[4];
				  break;
			  } 			  			  
		  }
		  if(p[j] == arr[j]){
			  p[j] = -1;
		  }	  
	  }
	  ll sum = 0;
	  for(ll i = 0;i < 5;i++){
		  for(ll j = 0;j < 5;j++){
			  if(arr[i] != arr[j]){
				if(p[i] == arr[j]){
					ll a = dist[lca(arr[i],arr[j])];
					sum += dist[arr[i]] + dist[arr[j]] - 2 * a;				  				  
				}else if(p[i] == -1 && p[j] == -1 && i > j){
					ll a = dist[lca(arr[i],arr[j])];
					sum += dist[arr[i]] + dist[arr[j]] - 2 * a;				  				  
				}	
			  }		  
		  }
	  }
	  for(ll i = 0;i < V;i++){
		  for(ll j = i + 1;j < V;j++){
			  if(p[i] == p[j] && p[i] != -1){
				  ll converge = lca(arr[i],arr[j]);
				  if(converge != p[i]){
						sum -= dist[converge];
				  }
			  }
		  }
	  }
	  cout<<sum<<'\n';  	  	  
  }      
}

Compilation message

roadsideadverts.cpp: In function 'void dfs(ll, ll, ll)':
roadsideadverts.cpp:24:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |  for(ll i = 0;i < v[x].size();i++){
      |               ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 37 ms 30164 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 36 ms 26972 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Runtime error 37 ms 30164 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -