Submission #484920

#TimeUsernameProblemLanguageResultExecution timeMemory
484920wind_reaper경주 (Race) (IOI11_race)C++17
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
#ifndef LOCAL
#include "race.h"
#endif
 
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
*/
 
using namespace std;
// using namespace __gnu_pbds;
using namespace chrono;
 
// mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
/*
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
 
//***************** CONSTANTS *****************
 
const int MXN = 100000;
 
//***************** GLOBAL VARIABLES *****************
 
vector<array<int, 2>> g[MXN];
int mp[1000001];
int ans = INT_MAX, K, sz[MXN], r[MXN];
int64_t depth[MXN];

//***************** AUXILIARY STRUCTS *****************

int dfs(int p, int u){
	sz[u] = 1;

	for(auto& [v, d] : g[u]) if(v != p){
		sz[u] += dfs(u, v);
	}

	return sz[u];
}

void dfs2(int p, int u, int s, int len){
	if(depth[u] > K)
		return;

	mp[s][depth[u]] = min(mp[s][depth[u]], len);

	for(auto& [v, d] : g[u]) if(v != p && !r[v]){
		depth[v] = depth[u] + int64_t(d);
		dfs2(u, v, s, len + 1);
	}
}

void decompose(int u){
	int inv = -1, lim = sz[u] >> 1;

	for(auto& [v, d] : g[u])
		if(!r[v] && sz[v] > lim){
			inv = v;
			break;
		}

	if(inv != -1){
		sz[u] -= sz[inv];
		sz[inv] += sz[u];
		decompose(inv);

		return;
	}

	r[u] = 1;

	depth[u] = 0;

	fill(mp[u], mp[u] + 1000001, MXN + 1);
	for(auto& [v, d] : g[u]) if(!r[v]){
		depth[v] = d;
		fill(mp[v], mp[v] + 1000001, MXN + 1);
		dfs2(u, v, v, 1);
	}

	mp[u][0] = 0;

	for(auto& [v, d] : g[u]) if(!r[v]){
		for(auto& [dep, len] : mp[v]){
			if(dep <= K) ans = min(ans, len + mp[u][K - dep]);
		}

		for(auto& [dep, len] : mp[v]){
			if(dep > K)
				continue;

			mp[u][dep] = min(mp[u][dep], len);
		}
	}

	for(auto& [v, d] : g[u]) if(!r[v]){
		decompose(v);
	}
}

//***************** MAIN BODY *****************

int best_path(int N, int k, int H[][2], int L[]){
	for(int i = 0; i < N - 1; i++){
		g[H[i][0]].push_back({H[i][1], L[i]});
		g[H[i][1]].push_back({H[i][0], L[i]});
	}
 
	K = k;

	dfs(0, 0);

	decompose(0);

	return (ans == INT_MAX ? -1 : ans);
}
 
//***************** *****************
 
#ifdef LOCAL
int32_t main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
 
	#ifdef LOCAL
		auto begin = high_resolution_clock::now();
	#endif
 
	int tc = 1;
	// cin >> tc; 
	for (int t = 0; t < tc; t++){
		int N, k;
		cin >> N >> k;
 
		int H[N-1][2], L[N-1];
		for(int i = 0; i < N-1; i++)
			cin >> H[i][0] >> H[i][1] >> L[i];
 
		cout << best_path(N, k, H, L) << '\n';
	}
 
	#ifdef LOCAL 
		auto end = high_resolution_clock::now();
		cout << fixed << setprecision(4);
		cout << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
	#endif
 
	return 0;
}
#endif
/*
If code gives a WA, check for the following : 
1. I/O format
 
2. Are you clearing all global variables in between tests if multitests are a thing
 
3. Can you definitively prove the logic
 
4. If the code gives an inexplicable TLE, and you are sure you have the best possible complexity,
use faster io
*/

Compilation message (stderr)

race.cpp: In function 'void dfs2(int, int, int, int)':
race.cpp:47:7: error: invalid types 'int[int64_t {aka long int}]' for array subscript
   47 |  mp[s][depth[u]] = min(mp[s][depth[u]], len);
      |       ^
race.cpp:47:29: error: invalid types 'int[int64_t {aka long int}]' for array subscript
   47 |  mp[s][depth[u]] = min(mp[s][depth[u]], len);
      |                             ^
race.cpp: In function 'void decompose(int)':
race.cpp:83:7: error: invalid types 'int[int]' for array subscript
   83 |  mp[u][0] = 0;
      |       ^
race.cpp:86:30: error: 'begin' was not declared in this scope
   86 |   for(auto& [dep, len] : mp[v]){
      |                              ^
race.cpp:86:30: note: suggested alternatives:
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:95,
                 from race.cpp:1:
/usr/include/c++/10/valarray:1224:5: note:   'std::begin'
 1224 |     begin(const valarray<_Tp>& __va)
      |     ^~~~~
In file included from /usr/include/c++/10/filesystem:46,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:129,
                 from race.cpp:1:
/usr/include/c++/10/bits/fs_dir.h:549:3: note:   'std::filesystem::__cxx11::begin'
  549 |   begin(recursive_directory_iterator __iter) noexcept
      |   ^~~~~
race.cpp:86:30: error: 'end' was not declared in this scope
   86 |   for(auto& [dep, len] : mp[v]){
      |                              ^
race.cpp:86:30: note: suggested alternatives:
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:95,
                 from race.cpp:1:
/usr/include/c++/10/valarray:1244:5: note:   'std::end'
 1244 |     end(const valarray<_Tp>& __va)
      |     ^~~
In file included from /usr/include/c++/10/filesystem:46,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:129,
                 from race.cpp:1:
/usr/include/c++/10/bits/fs_dir.h:554:3: note:   'std::filesystem::__cxx11::end'
  554 |   end(recursive_directory_iterator) noexcept
      |   ^~~
race.cpp:90:30: error: 'begin' was not declared in this scope
   90 |   for(auto& [dep, len] : mp[v]){
      |                              ^
race.cpp:90:30: note: suggested alternatives:
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:95,
                 from race.cpp:1:
/usr/include/c++/10/valarray:1224:5: note:   'std::begin'
 1224 |     begin(const valarray<_Tp>& __va)
      |     ^~~~~
In file included from /usr/include/c++/10/filesystem:46,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:129,
                 from race.cpp:1:
/usr/include/c++/10/bits/fs_dir.h:549:3: note:   'std::filesystem::__cxx11::begin'
  549 |   begin(recursive_directory_iterator __iter) noexcept
      |   ^~~~~
race.cpp:90:30: error: 'end' was not declared in this scope
   90 |   for(auto& [dep, len] : mp[v]){
      |                              ^
race.cpp:90:30: note: suggested alternatives:
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:95,
                 from race.cpp:1:
/usr/include/c++/10/valarray:1244:5: note:   'std::end'
 1244 |     end(const valarray<_Tp>& __va)
      |     ^~~
In file included from /usr/include/c++/10/filesystem:46,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:129,
                 from race.cpp:1:
/usr/include/c++/10/bits/fs_dir.h:554:3: note:   'std::filesystem::__cxx11::end'
  554 |   end(recursive_directory_iterator) noexcept
      |   ^~~
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from race.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h: In instantiation of 'typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type std::__fill_a1(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = int; _Tp = int; typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type = void]':
/usr/include/c++/10/bits/stl_algobase.h:914:21:   required from 'void std::__fill_a(_FIte, _FIte, const _Tp&) [with _FIte = int; _Tp = int]'
/usr/include/c++/10/bits/stl_algobase.h:944:20:   required from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = int; _Tp = int]'
race.cpp:76:38:   required from here
/usr/include/c++/10/bits/stl_algobase.h:873:2: error: invalid type argument of unary '*' (have 'int')
  873 |  *__first = __tmp;
      |  ^~~~~~~~