Submission #394858

# Submission time Handle Problem Language Result Execution time Memory
394858 2021-04-27T11:36:05 Z suren Swapping Cities (APIO20_swap) C++14
Compilation error
0 ms 0 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
int n, m, flag = true;
vector < int > u, v, w;
vector < int > vc[200045];
pair < int , pair< int, int > > p[200045];
int parent[200045];
int get( ll x ){
	if( parent[x] == x )
		return x;
	parent[x] = get( parent[x] );
}
void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
		int i;
		n = N;m = M;u = U;v = V;w = W;
		for( i = 0; i < m; i ++ ){
			p[i].second.F=u[i];
			p[i].second.S=v[i];
			p[i].first=w[i];
		}
		sort( p, p + m );
		for( i = 1; i <= n; i ++ ){
			parent[i] = i;
		}
}
int getMinimumFuelCapacity(int X, int Y) {
	map < int, int > mp;
	int i;
	flag = false;
	mp.clear();
	for( i = 0; i < n; i ++ )
		vc[i].clear();
	for( i = 0; i < m; i ++ ){
		int x = get( p[i].second.F );
		int y = get( p[i].second.S );
		if( x != y ){
			if( sz[x] < sz[y] )
				swap( x, y );
			parent[y] = x; 
		}
		vc[ p[i].second.F ].pb( p[i].second.S );
		vc[ p[i].second.S ].pb( p[i].second.F );
		mp[p[i].second.F] = 1;
		mp[ p[i].second.S ] = 1;
		if( vc[ p[i].S.F ].size() == 3 || vc[p[i].S.S].size() == 3 ){
			flag = true;
		}
		if( mp.size() == i+1 && mp[X] == mp[Y] && mp[X] == 1 && parent[x] == parent[y] ){
			return p[i].first;
		}
		if( mp[X] == mp[Y] && mp[X] == 1 && flag == true && parent[x] == parent[y] )
			return p[i].first;
	}
	return -1;
}
int main() {
  int N, M;
  assert(2 == scanf("%d %d", &N, &M));
  
  std::vector<int> U(M), V(M), W(M);
  for (int i = 0; i < M; ++i) {
    assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
  }

  int Q;
  assert(1 == scanf("%d", &Q));

  std::vector<int> X(Q), Y(Q);
  for (int i = 0; i < Q; ++i) {
    assert(2 == scanf("%d %d", &X[i], &Y[i]));
  }

  init(N, M, U, V, W);
  
  std::vector<int> minimum_fuel_capacities(Q);
  for (int i = 0; i < Q; ++i) {
    minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
  }

  for (int i = 0; i < Q; ++i) {
    printf("%d\n", minimum_fuel_capacities[i]);
  }
  
  return 0;
}

Compilation message

swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:43:8: error: 'sz' was not declared in this scope
   43 |    if( sz[x] < sz[y] )
      |        ^~
swap.cpp:54:17: warning: comparison of integer expressions of different signedness: 'std::map<int, int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   54 |   if( mp.size() == i+1 && mp[X] == mp[Y] && mp[X] == 1 && parent[x] == parent[y] ){
      |       ~~~~~~~~~~^~~~~~
swap.cpp: In function 'int get(long long int)':
swap.cpp:16:12: warning: control reaches end of non-void function [-Wreturn-type]
   16 |  parent[x] = get( parent[x] );
      |  ~~~~~~~~~~^~~~~~~~~~~~~~~~~~