Submission #542438

#TimeUsernameProblemLanguageResultExecution timeMemory
542438inventiontimeTraffic (IOI10_traffic)C++17
0 / 100
13 ms23796 KiB
#include "traffic.h"
#include <bits/stdc++.h>
using namespace std;

//#define int ll
//#define endl '\n'

#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl

typedef __int128 ll;
typedef vector<int> vi;
typedef array<ll, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef priority_queue<int> pq;

template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }

const ll inf = 1e17;
const int maxn = 1e6;

vi adj[maxn];

ll c_sub[maxn];
ll c_par[maxn];

int LocateCentre(int n, int pp[], int s[], int d[]) {

   loop(i, n-1) {
      adj[s[i]].pb(d[i]);
      adj[d[i]].pb(s[i]);
   }
   
   auto dfs = [&](int u, int p, auto&& dfs) -> void {
	   c_sub[u] = 0;
      for (int v : adj[u]) if (v != p) {
			dfs(v, u, dfs);
         c_sub[u] += 2 * c_sub[v];
         c_sub[u] += pp[v];
      }
   };

   dfs(0, 0, dfs);

   auto dfs2 = [&](int u, int p, auto&& dfs2) -> void {
      c_par[u] = 0;
      if(p != u) c_par[u] += pp[p] +
         2 * c_par[p] +
         2 * (c_sub[p] - 2 * c_sub[u] - pp[u]);
	   for (int v : adj[u]) if (v != p) {
			dfs2(v, u, dfs2);
      }
   };

   dfs2(0, 0, dfs2);

   auto dfs3 = [&](int u, int p, auto&& dfs3) -> ii {
      ii res = {c_sub[u] + c_par[u], u};

	   for (int v : adj[u]) if (v != p)
			ckmin(res, dfs3(v, u, dfs3));
      
      return res;
   };

   return dfs3(0, 0, dfs3)[1];

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...