Submission #50707

# Submission time Handle Problem Language Result Execution time Memory
50707 2018-06-13T03:02:14 Z wzy Factories (JOI14_factories) C++11
0 / 100
6000 ms 655360 KB
#include"factories.h"
#include <bits/stdc++.h>
using namespace std;

/*
MAGIA DO GATINHO 
PARA FAZER SEU CODE
PASSAR NO JUDGEZINHO 
  ∧_∧ 
(。・ω・。)つ━☆・*。
⊂   ノ    ・゜+.
 しーJ   °。+ *´¨)
         .• ´¸.•*´¨) ¸.•*¨)
          (¸.•´ (¸.•'* ☆
*/


long long dist[500005] , dp[2][500005] , depth[500005]; 
int  st[500005] , ed[500005] , lca[22][500005] , n , T = 0;
vector<pair<int,int> > adj[500005];

void dfs(int x , int y){
	if(x == y) lca[0][x] = -1;
	else lca[0][x] = y;
	st[x] = ++T;
	for(int j = 1 ; j < 22 ; j++){
		if(lca[j-1][x] == -1){
			lca[j][x] = lca[j-1][lca[j-1][x]];
		}
	}
	for(int i = 0 ; i < adj[x].size() ; i++){
		if(adj[x][i].first == y) continue;
		depth[adj[x][i].first] = depth[x] + 1;
		dist[adj[x][i].first] = dist[x] + adj[x][i].second; 
		dfs(adj[x][i].first, x);
	}
	ed[x] = T;
}


bool inside(int x , int y){ // olha se y ta na subtree de x
	return (st[x] <= st[y] && ed[x] >= ed[y]);

}

int lcaQ(int x , int y){
	if(inside(x,y)) return x;
	else if(inside(y,x)) return y;
	for(int j = 21 ; j >= 0 ; j--){
		if(lca[j][x] != - 1 && !inside(lca[j][x] , y)) x = lca[j][x];
	}
	for(int j = 21 ; j >= 0 ; j--){
		if(lca[j][y] != -1 && !inside(lca[j][y] , x)) y = lca[j][y];
	}
	return lca[0][x];
}

long long ansz = 0;

long long get_dist(int x , int y){
	long long ans = dist[x] + dist[y] - 2*dist[lcaQ(x,y)];
	return ans;
}

void Init(int N, int A[], int B[], int D[]){	
	 n = N;
	 for(int i = 0 ; i < n - 1; i ++){
	 	adj[A[i]].push_back(pair<int,int>(B[i] , D[i]));
	 	adj[B[i]].push_back(pair<int,int>(A[i] , D[i]));
	 }
	 for(int i = 0; i < n ; i++)
	 	for(int j = 0 ; j < 22 ; j++) lca[j][i] = -1;
	 dist[0] = 0;
	 dfs(0 , 0 );
}



vector<pair<int, int> > v_tree[500005];

bool cmp(int a , int b){
	return st[a] < st[b];
}


void solve(int x , int y){
	for(auto p : v_tree[x]){
		if(p.first == y) continue;
		solve(p.first, x);
		ansz = min(ansz , min(dp[0][x] + dp[1][p.first] + p.second , dp[1][x] + dp[0][p.first] + p.second));
		dp[0][x] = min(dp[0][x] , dp[0][p.first] + p.second);
		dp[1][x] = min(dp[1][x] , dp[1][p.first] + p.second);
	}
}

long long Query(int S, int X[], int T, int Y[]){
	vector<int> sweep;
	vector<int> loltree;
	for(int i = 0 ; i < S ; i ++){
		sweep.push_back(X[i]);
		loltree.push_back(X[i]);
	}for(int i = 0 ; i < T ; i ++){
		sweep.push_back(Y[i]);
		loltree.push_back(Y[i]);
	}
	sort(sweep.begin() , sweep.end() , cmp);
	sweep.resize(unique(sweep.begin() , sweep.end()) - sweep.begin());
	for(int i = 0 ; i < ((int)sweep.size()) - 1 ; i++){
		loltree.push_back(lcaQ(sweep[i] , sweep[i+1]));
	}
	sort(loltree.begin() , loltree.end() , cmp);
	loltree.resize(unique(loltree.begin() , loltree.end()) - loltree.begin());
	for(int i = 0 ; i < loltree.size() ; i++){
		dp[0][loltree[i]] = (long long) 1e18;
		dp[1][loltree[i]] = (long long) 1e18;
	}
	for(int i = 0 ; i < S ;i  ++){
		dp[0][X[i]] = 0;
	}
	for(int i = 0 ; i < T ; i++){
		dp[1][Y[i]] = 0;
	}
	int root = loltree.front();
	vector<int> sline;
	sline.push_back(root);
	for(int i =  1 ; i < loltree.size() ; i++){
		while(!inside(sline.back() , loltree[i])) sline.pop_back();
		v_tree[sline.back()].push_back(pair<int,int>(loltree[i] , get_dist(loltree[i] , sline.back())));
		v_tree[loltree[i]].push_back(pair<int,int>(sline.back() , get_dist(loltree[i] , sline.back())));
		sline.push_back(loltree[i]);

	}
	ansz = (long long) 1e18;
	solve(root, root);
	for(int i = 0 ; i < loltree.size() ; i++){
		v_tree[loltree[i]].clear();
	}
	return ansz;
}

Compilation message

factories.cpp: In function 'void dfs(int, int)':
factories.cpp:31:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < adj[x].size() ; i++){
                  ~~^~~~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:113:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < loltree.size() ; i++){
                  ~~^~~~~~~~~~~~~~~~
factories.cpp:126:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i =  1 ; i < loltree.size() ; i++){
                   ~~^~~~~~~~~~~~~~~~
factories.cpp:135:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < loltree.size() ; i++){
                  ~~^~~~~~~~~~~~~~~~
factories.cpp: In function 'void dfs(int, int)':
factories.cpp:28:36: warning: array subscript is below array bounds [-Warray-bounds]
    lca[j][x] = lca[j-1][lca[j-1][x]];
                ~~~~~~~~~~~~~~~~~~~~^
# Verdict Execution time Memory Grader output
1 Execution timed out 6154 ms 655360 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6182 ms 655360 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5430 ms 655360 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -