Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Maximize the distance in graph problem solution

YASH PAL, 31 July 2024
In this HackerEarth Maximize the distance in graph problem solution Given an undirected complete graph G with n vertices. The weight of the edge between (i,j) is equal to C[i][j] Let D(i,j) be the shortest distance between vertices i,j and W(G) be the sum of all shortest distances between any two vertices. You can remove at most k edges from graph G in order to obtain a graph G’. You have to maximize the ratio of the sum of all shortest distances between any two vertices in new graph G’ (after removing the edges) and sum of all shortest distances between any two vertices in old graph G (before reomoving the edges): W(G’)/W(G). G must remain connected.
HackerEarth Maximize the distance in graph problem solution

HackerEarth Maximize the distance in graph problem solution.

#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
int main(void)
{
int n,k;
cin>>n>>k;
cout << "0n";
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int inf = 1e9 + 1e5;

mt19937 rr;

using Edge = pair<int, int>;

struct DSU {
vector<int> col, rk;

DSU(int n): col(n), rk(n) {
iota(col.begin(), col.end(), 0);
}

int get(int u) {
if (u == col[u]) {
return u;
}
return col[u] = get(col[u]);
}

bool join(int u, int v) {
u = get(u), v = get(v);
if (u == v) {
return false;
}
if (rk[u] > rk[v]) {
swap(u, v);
}
if (rk[u] == rk[v]) {
++rk[v];
}
col[u] = v;
return true;
}
};

const int maxn = 128;
const int C = 1 << 20;
int a[maxn][maxn];
int n, k;
int clique = 1, rem_in_clique = 0;

int b[maxn][maxn];

ll score(int g[maxn][maxn]) {
int t[maxn][maxn];
memcpy(t, g, sizeof(t));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
t[j][k] = min(t[j][k], t[j][i] + t[i][k]);
}
}
}
ll res = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
assert(t[i][j] <= (1 << 20) * (n - 1));
res += t[i][j];
}
}
return res;
}

vector<int> find_path_1(int u) {
vector<int> path{u};
vector<char> used(n);
used[u] = true;
for (int i = 0; i < n - 1; ++i) {
int v = -1;
for (int j = 0; j < n; ++j) {
if (!used[j] && (v == -1 || a[u][j] > a[u][v])) {
v = j;
}
}
assert(v != -1);
u = v;
path.push_back(u);
used[u] = true;
}
reverse(path.begin(), path.end());
return path;
}

vector<int> find_path_2(int u, bool with_two) {
vector<int> path{u};
vector<char> used(n);
used[u] = true;
int L = n / 2 - 1, R = n / 2 + 1;
while (L >= 0 || R < n) {
bool in_front = true;
if (R < n && (L <= clique || L + 1 <= n - R)) {
in_front = false;
}
bool two_vertices = false;
if (in_front) {
if (--L >= 0 && with_two) {
two_vertices = true;
--L;
}
} else {
if (++R < n && with_two) {
two_vertices = true;
++R;
}
}
int u = in_front ? path.front() : path.back();
int v1 = -1, v2 = -1;
if (!two_vertices) {
for (int j = 0; j < n; ++j) {
if (used[j]) {
continue;
}
if (v1 == -1 || (a[u][j] > a[u][v1] && rr())) {
v1 = j;
}
}
assert(v1 != -1);
} else {
for (int j1 = 0; j1 < n; ++j1) {
if (used[j1]) {
continue;
}
for (int j2 = 0; j2 < n; ++j2) {
if (used[j2] || j2 == j1) {
continue;
}
if (v1 == -1 || a[u][j1] + a[j1][j2] > a[u][v1] + a[v1][v2]) {
v1 = j1, v2 = j2;
}
}
}
assert(v1 != -1 && v2 != -1);
}
used[v1] = true;
if (two_vertices) {
used[v2] = true;
}
if (!in_front) {
path.push_back(v1);
if (two_vertices) {
path.push_back(v2);
}
} else {
path.insert(path.begin(), v1);
if (two_vertices) {
path.insert(path.begin(), v2);
}
}
}
assert(L == -1 && R == n);
return path;
}

vector<int> find_path_3() {
vector<vector<int>> g(n);
struct E {
int u, v, w;
};
vector<E> es;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
es.push_back(E{i, j, a[i][j]});
}
}
sort(es.begin(), es.end(), [](E a, E b) {
return a.w > b.w;
});
int iters = n - 1;
DSU dsu(n);
for (auto e : es) {
if ((int) g[e.u].size() <= 1 &&
(int) g[e.v].size() <= 1 &&
dsu.get(e.u) != dsu.get(e.v)) {
--iters;
dsu.join(e.u, e.v);
g[e.u].push_back(e.v);
g[e.v].push_back(e.u);
}
}
int u = 0, prev = -1;
while ((int) g[u].size() == 2) {
++u;
}
vector<int> path{u};
for (int iter = 0; iter < n - 1; ++iter) {
for (auto v : g[u]) {
if (v == prev) {
continue;
}
prev = u;
u = v;
path.push_back(u);
break;
}
}
return path;
}

const int M = 10;
vector<int> rebuild(vector<int> path, vector<int> ws) {
pair<ll, int> d[1 << M][M];
memset(d, -1, sizeof(d));

int n = (int) path.size();
assert(n <= M);

auto w = [&](int i, int j) -> ll {
return a[path[i]][path[j]];
};

ll score_old = 0;
for (int i = 0; i < n - 1; ++i) {
score_old += ws[i] * w(i, i + 1);
}

d[1][0] = {0, -1};
for (int mask = 0; mask < (1 << n); ++mask) {
int bits = __builtin_popcount(mask);
for (int u = 0; u < n; ++u) {
if (d[mask][u].first == -1) {
continue;
}
for (int v = 0; v < n; ++v) {
if (mask & (1 << v)) {
continue;
}
int nmask = mask | (1 << v);
ll ncost = d[mask][u].first + ws[bits - 1] * w(u, v);
if (d[nmask][v].first < ncost) {
d[nmask][v] = {ncost, u};
}
}
}
}
int mask = (1 << n) - 1;
int u = n - 1;
ll score_new = d[mask][u].first;
assert(score_new != -1);
vector<int> res;
for (int i = 0; i < n - 1; ++i) {
res.push_back(path[u]);
int v = d[mask][u].second;
mask ^= 1 << u;
u = v;
}
res.push_back(path[u]);
reverse(res.begin(), res.end());
return res;
}

int rnd(int l, int r) {
return rr() % (r - l + 1) + l;
}

void optimize_path(vector<int>& path, int iters) {
if (clique == n - 1) {
return;
}
for (int iter = 0; iter < iters; ++iter) {
int l = rnd(clique + 1, max(clique + 1, n - M));
int r = min(l + M, n);
vector<int> ws;
for (int i = l + 1; i < r; ++i) {
ws.push_back(i * (n - i));
}
vector<int> subpath;
for (int i = l; i < r; ++i) {
subpath.push_back(path[i]);
}
rebuild(subpath, ws);
for (int i = l; i < r; ++i) {
path[i] = subpath[i - l];
}
}
}

vector<int> random_path() {
vector<int> path(n);
iota(path.begin(), path.end(), 0);
shuffle(path.begin(), path.end(), rr);
return path;
}

int attempt = 0;
vector<int> find_path() {
if (attempt++ == 0) {
return find_path_3();
}
if (attempt <= n) {
int u = attempt++ - 1;
return find_path_2(u, true);
}
if (attempt <= 2 * n) {
int u = attempt++ - n - 1;
return find_path_2(u, false);
}
cerr << "donen";
return random_path();
}

vector<Edge> solve(vector<int> path) {
optimize_path(path, 10);
vector<Edge> es;
for (int i = 0; i < n - 1; ++i) {
es.emplace_back(path[i], path[i + 1]);
}
for (int i = 0; i < n && (int) es.size() < k; ++i) {
vector<pair<int, int>> to;
for (int j = 0; j < i - 1; ++j) {
to.emplace_back(a[path[i]][path[j]], j);
}
sort(to.rbegin(), to.rend());
for (auto e : to) {
es.emplace_back(path[i], path[e.second]);
if ((int) es.size() == k) {
break;
}
}
}
return es;
}

void print(vector<Edge> es) {
assert((int) es.size() == k);
int q = n * (n - 1) / 2 - (int) es.size();
bool keep[maxn][maxn];
memset(keep, 0, sizeof(keep));
for (auto e : es) {
int u = e.first, v = e.second;
keep[u][v] = keep[v][u] = true;
}
cout << q << 'n';
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (!keep[i][j]) {
cout << i + 1 << ' ' << j + 1 << 'n';
--q;
}
}
}
assert(q == 0);
}

void build_graph(vector<Edge> es) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
b[i][j] = i == j ? 0 : inf;
}
}
for (auto e : es) {
int u, v;
tie(u, v) = e;
b[u][v] = b[v][u] = a[u][v];
}
}

signed main() {
#ifdef LOCAL
assert(freopen("test.in", "r", stdin));
#endif
assert(cin >> n >> k);
#ifndef LOCAL
assert(n == 128);
#endif
assert(0 <= k && k <= (n - 1) * (n - 1));
k = max(n - 1, n * (n - 1) / 2 - k);

int cc = n - 1;
if (cc < k) {
for (int i = 2; i < n; ++i) {
cc += i - 1;
if (cc >= k) {
rem_in_clique = cc - k;
clique = i;
break;
}
}
}
//cerr << clique << ' ' << rem_in_clique << 'n';

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
assert(cin >> a[i][j]);
if (i == j) {
assert(a[i][j] == 0);
} else {
assert(1 <= a[i][j] && a[i][j] <= (1 << 20));
}
if (i > j) {
assert(a[i][j] == a[j][i]);
}
}
}
string tmp;
assert(!(cin >> tmp));
ll sum_dist = score(a);
cerr << "sum_dist: " << sum_dist << 'n';
ll best = 0;
vector<Edge> ans;
while (clock() < 1.8 * CLOCKS_PER_SEC) {
auto path = find_path();
auto es = solve(path);
build_graph(es);
ll cur = score(b);
if (cur > best) {
optimize_path(path, 1000);
auto es = solve(path);
build_graph(es);
ll cur2 = score(b);
assert(cur2 >= cur);
cur = cur2;

cerr << "new best: " << best << " -> " << cur << 'n';
ans = es;
best = cur;
}
}
print(ans);
cerr << "score: " << best * 1. / sum_dist << 'n';
}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes