Submission #72106

# Submission time Handle Problem Language Result Execution time Memory
72106 2018-08-26T05:18:44 Z nvmdava Dreaming (IOI13_dreaming) C++17
Compilation error
0 ms 0 KB
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;

struct Path{
	int to, cost;
	Path(int _to, int _cost){
		to = _to;
		cost = _cost;
	}
};

struct Node{
	int p[20], d;
};
map<int, int> c[100001];
vector<Path> path[100001];
bool in[100001];
Node a[100001];
int mxdist, mxid;

void build(int v, int p){
	in[v] = 1;
	for(auto x : path[v]){
		if(x.to == v) continue;
		a[x.to].d = a[v].d + 1;
		memset(a[x.to], -1, sizeof(a[x.to]));
		a[x.to].p[0] = v;
		for(int i = 1; i < 20; i++){
			int t = a[x.to].p[i - 1];
			if(t == -1) break;
			a[x.to].p[i] = a[t].p[i - 1];
		}
	}
}

int goUp(int v, int u){
	for(int i = 0; i < 20; i++){
		if(u & (1 << i)) v = a[v].p[i];
	}
	return v;
}

int lca(int v, int u){
	int l = 0;
	int r = min(a[v].d, a[u].d) + 1;
	int m;
	while(l + 1 != r){
		m = (l + r) >> 1;
		if(goUp(v, a[v].d - m) == goUp(u, a[u].d - m)) l = m;
		else r = m;
	}
	return goUp(v, a[v].d - l);
}

void dfs(int v, int p, int d){
	if(path[v].size() == 1 && p != -1){
		if(d > mxdist){
			mxdist = d;
			mxid = v;
		}
	}
	for(auto x : path[v]){
		if(x.to == p) continue;
		dfs(x.to, v, d + x.cost);
	}
}

int go(int v){
	if(path[v].size() == 0) return 0;
	if(in[v]) return 0;
	
	memset(a[v].p, -1, sizeof(a[v].p));
	a[v].d = 0;
	build(v, -1);
	
	mxdist = 0;
	mxid = v;
	dfs(v, -1, 0);
	mxdist = 0;
	v = mxid;
	dfs(v, -1, 0);
	int u = mxid;
	int o = lca(v, u);
	int s = (mxdist + 1) >> 1;
	int q = 0;
	while(v != o){
		q += c[v][a[v].p[0]];
		if(q >= s){
			return min(q, mxdist - q + c[u][a[v].p[0]);
		}
	}
	q = 0;
	while(u != o){
		q += c[u][a[u].p[0]];
		if(q >= s){
			return min(q, mxdist - q + c[u][a[u].p[0]);
		}
	}
}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    
    for(int i = 0; i < N; i++){
    	p[i] = i;
	}
    
    for(int i = 0; i < M; i++){
    	path[A[i]].push_back(Path(B[i], T[i]));
    	path[B[i]].push_back(Path(A[i], T[i]));
    	c[A[i]][B[i]] = T[i];
    	c[B[i]][A[i]] = T[i];
	}
	int mx = -1;
	int mx2 = -1;
	int s;
	for(int i = 0; i < N; i++){
		s = go(i);
		if(s > mx){
			mx2 = mx;
			mx = s;
		} else if(s > mx2) mx2 = s;
	}
	return L + mx1 + mx2;
}

Compilation message

dreaming.cpp: In function 'void build(int, int)':
dreaming.cpp:27:38: error: cannot convert 'Node' to 'void*' for argument '1' to 'void* memset(void*, int, size_t)'
   memset(a[x.to], -1, sizeof(a[x.to]));
                                      ^
dreaming.cpp: In function 'int go(int)':
dreaming.cpp:90:45: error: expected ']' before ')' token
    return min(q, mxdist - q + c[u][a[v].p[0]);
                                             ^
dreaming.cpp:97:45: error: expected ']' before ')' token
    return min(q, mxdist - q + c[u][a[u].p[0]);
                                             ^
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:105:6: error: 'p' was not declared in this scope
      p[i] = i;
      ^
dreaming.cpp:124:13: error: 'mx1' was not declared in this scope
  return L + mx1 + mx2;
             ^~~
dreaming.cpp:124:13: note: suggested alternative: 'mx2'
  return L + mx1 + mx2;
             ^~~
             mx2
dreaming.cpp: In function 'int go(int)':
dreaming.cpp:100:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^