Submission #743681

# Submission time Handle Problem Language Result Execution time Memory
743681 2023-05-17T17:15:15 Z speedyArda Swapping Cities (APIO20_swap) C++14
6 / 100
128 ms 14924 KB
#include "swap.h"
#include "bits/stdc++.h"
#include <vector>
using namespace std;
using ll = long long;
const int MAXN = 1e5+5;
vector < vector< pair<ll, ll> > > adj(MAXN);
int sum_dist = 0;
int dist[MAXN];
int par[MAXN], sz[MAXN], edge_sz[MAXN], max_sz[MAXN], cnt[MAXN];
int max_ed = 0, u_max;
int n, m;
vector < pair<int, pair<int, int> > > edges;

int find(int v)
{
  if(par[v] == v)
    return v;
  return par[v] = find(par[v]);
}

void merge(int a, int b)
{
  int maxi = max(cnt[a], cnt[b]);
  a = find(a), b = find(b);
  if(a == b) {
    edge_sz[a]++;
    max_sz[a] = max(max_sz[a], maxi);
    return;
  }
  if(sz[a] < sz[b])
    swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
  edge_sz[a]++;
  max_sz[a] = max(max_sz[a], max(max_sz[b], maxi));
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {

    m = M;
    n = N;

    for(int i = 0; i < M; i++)
    {
      adj[U[i]].push_back({W[i], V[i]});
      adj[V[i]].push_back({W[i], U[i]});
      dist[V[i]] = W[i]; // for sub 2
      edges.push_back({W[i], {U[i], V[i]}});
      u_max = max(u_max, U[i]);
      sum_dist = max(sum_dist, W[i]);
    }
    sort(edges.begin(), edges.end());
    for(int i = 0; i < N; i++)
    {
      max_ed = max(max_ed, (int)adj[i].size());
    }

}

int getMinimumFuelCapacity(int X, int Y) {
  if(max_ed <= 2) // Sub 1
  {
    if(m == n - 1)
      return -1;
    else
      return sum_dist;
  }

  if(u_max == 0) // Sub 2
  {
    if(X > Y)
      swap(X, Y);
    ll sum = max(dist[X], dist[Y]);
    if(X != 0) {
      
      sum = max(sum, (adj[0][0].second != X && adj[0][0].second != Y) ? adj[0][0].first : ((adj[0][1].second != X && adj[0][1].second != Y) ? adj[0][1].first : adj[0][2].first));
    } else 
    {
      sum = max(sum, (adj[0][0].second != Y ? max(adj[0][0].first, adj[0][1].second != Y ? adj[0][1].first : adj[0][2].first) : max(adj[0][1].first, adj[0][2].first)));
    }
    return sum;
  }

  for(int i = 0; i < n; i++)
  {
    cnt[i] = 0;
    edge_sz[i] = max_sz[i] = 0;
    sz[i] = 1;
    par[i] = i;
  }

  for(pair<int, pair<int, int> > edge : edges)
  {
    cnt[edge.second.first]++;
    cnt[edge.second.second]++;
    merge(edge.second.first, edge.second.second);

    if(find(edge.second.first) == find(edge.second.second))
    {
      int curr = find(edge.second.first);
      if(edge_sz[curr] >= sz[curr] || max_sz[curr] > 2)
        return edge.first;
    }
  }


  return -1;


  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 48 ms 9812 KB Output is correct
10 Correct 57 ms 11392 KB Output is correct
11 Correct 54 ms 11264 KB Output is correct
12 Correct 59 ms 11696 KB Output is correct
13 Correct 58 ms 11776 KB Output is correct
14 Correct 52 ms 10116 KB Output is correct
15 Correct 108 ms 13220 KB Output is correct
16 Correct 121 ms 12964 KB Output is correct
17 Correct 113 ms 13500 KB Output is correct
18 Correct 112 ms 13544 KB Output is correct
19 Correct 48 ms 7552 KB Output is correct
20 Correct 105 ms 14488 KB Output is correct
21 Correct 103 ms 14376 KB Output is correct
22 Correct 108 ms 14924 KB Output is correct
23 Correct 128 ms 14892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Incorrect 95 ms 13776 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Incorrect 1 ms 2644 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 48 ms 9812 KB Output is correct
10 Correct 57 ms 11392 KB Output is correct
11 Correct 54 ms 11264 KB Output is correct
12 Correct 59 ms 11696 KB Output is correct
13 Correct 58 ms 11776 KB Output is correct
14 Correct 52 ms 10116 KB Output is correct
15 Correct 108 ms 13220 KB Output is correct
16 Correct 121 ms 12964 KB Output is correct
17 Correct 113 ms 13500 KB Output is correct
18 Correct 112 ms 13544 KB Output is correct
19 Incorrect 95 ms 13776 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -