188352025-11-06 16:25:51ubormaciRácsháló gráfcpp17Elfogadva 50/5010ms748 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
//#pragma GCC optimize("O3","Ofast","unroll-loops")
// #pragma GCC target("sse4.2","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

std::lcm(ll a, ll b), std::gcd(int a, int b)

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int64_t ll;

vector<vector<ll>> v;
ll s;

const ll inf = 1e9;

vector<pair<ll,ll>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

bool ins(ll i, ll j, ll n, ll m) {

	if(i < 0 || i >= n || j < 1 || j > m) {
		return false;
	}
	return true;

}

void solve() {

	ll n, m;
	cin >> n >> m;
	ll k;
	cin >> k;

    ll s = m * n;

	v.resize(s+1, vector<ll>(s+1, 0));

	// floyd-warhsall is v^3 -> 200 ^ 3 still fits
	// and k is <= 100
	// so 10 ^ 2 * 10^6 * 2 = 2 * 10^8
	// . . . it *should* fit
	// maybe

	for(ll from = 1; from <= s; from++) {

        v[from][from] = 0;

        for(ll to = from + 1; to <= s; to++) {

            ll fi = (from-1) / m + 1;
            ll fj = (from-1) % m + 1;

            ll ti = (to-1) / m +1;
            ll tj = (to-1) % m + 1;

            ll dis = abs(fi - ti) + abs(fj - tj);

            v[from][to] = dis;
            v[to][from] = dis;

        }

    }   

	vector<ll> res;

	for(ll i = 0; i < k; i++) {
		ll a, b;
		cin >> a >> b;

		v[a][b] = 1;
		v[b][a] = 1;

        ll ai = (a-1) / m + 1;
        ll aj = (a-1) % m + 1;
        
        ll bi = (b-1) / m + 1;
        ll bj = (b-1) % m + 1;

        ll mx  = 0;

        for(ll from = 1; from <= s; from++) {
            for(ll to = 1; to <= s; to++) {

                ll v1 = v[from][a] + v[b][to] + 1;
                ll v2 = v[from][b] + v[a][to] + 1;

                v[from][to] = min(v[from][to], min(v1, v2));
                v[to][from] = v[from][to];

                mx = max(v[from][to], mx);
            }
        }

		res.push_back(mx);
	}

	for(const auto & x : res) {
		cout << x << "\n";
	}


}

int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	solve();

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/010ms564 KiB
3Elfogadva2/21ms316 KiB
4Elfogadva2/21ms508 KiB
5Elfogadva2/21ms316 KiB
6Elfogadva2/21ms316 KiB
7Elfogadva2/22ms504 KiB
8Elfogadva2/22ms316 KiB
9Elfogadva2/22ms316 KiB
10Elfogadva2/21ms520 KiB
11Elfogadva2/22ms316 KiB
12Elfogadva2/24ms740 KiB
13Elfogadva3/36ms608 KiB
14Elfogadva3/32ms316 KiB
15Elfogadva3/34ms564 KiB
16Elfogadva3/31ms316 KiB
17Elfogadva3/34ms564 KiB
18Elfogadva3/32ms316 KiB
19Elfogadva3/31ms316 KiB
20Elfogadva3/31ms540 KiB
21Elfogadva3/33ms316 KiB
22Elfogadva3/310ms748 KiB