141002025-01-09 18:45:40ggeerrggoo2Rácsháló gráfcpp17Accepted 50/50456ms760 KiB
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, m, k; cin >> n >> m >> k;
    vector<vector<int>>dist(n*m+1, vector<int>(n*m+1, 1e9)), adj(n*m+1);
    for(int i=1;i<=n*m;i++)
    {
        dist[i][i] = 0;
        if(i%m != 0)
        {
            adj[i].push_back(i+1);
            adj[i+1].push_back(i);
            dist[i][i+1] = 1;
            dist[i+1][i] = 1;
        }
        if(i <= (n-1)*m)
        {
            adj[i].push_back(i+m);
            adj[i+m].push_back(i);
            dist[i][i+m] = 1;
            dist[i+m][i] = 1;
        }
    }
    for(int k=1;k<=n*m;k++)
    for(int i=1;i<=n*m;i++)
    for(int j=1;j<=n*m;j++)
    {
        if(dist[i][k] + dist[k][j] < dist[i][j])
            {
                dist[i][j] = dist[i][k] + dist[k][j];
            }
    }/**/
    /*for(int i=1;i<=n*m;i++)
    for(int j=i+1;j<=n*m;j++)
    {
        dist[i][j] = dist[j][i] = abs((i%m)-(j%m))+abs(i/m-j/m);
    }/**/
    while(k--)
    {
        int u, v;
        cin >> u >> v;
        dist[u][v] = 1;
        dist[v][u] = 1;
        /*for(int i=1;i<=n*m;i++)
        for(int j=i+1;j<=n*m;j++)
        {
            if(dist[i][u] + dist[v][j] +1 < dist[i][j])
            {
                dist[i][j] = dist[i][u] + dist[v][j] + 1;
                dist[j][i] = dist[i][v] + dist[u][j] + 1;
            }
            else if (dist[i][v] + dist[u][j] +1 < dist[i][j])
            {
                dist[i][j] = dist[i][v] + dist[u][j] + 1;
                dist[j][i] = dist[i][u] + dist[v][j] + 1;
            }
        }/**/
        for(int k=1;k<=n*m;k++)
        for(int i=1;i<=n*m;i++)
        for(int j=1;j<=n*m;j++)
        {
            if(dist[i][k] + dist[k][j] < dist[i][j])
            {
                dist[i][j] = dist[i][k] + dist[k][j];
            }
        }
        int ln=0;
        for(int i=1;i<=n*m;i++)
        for(int j=i+1;j<=n*m;j++)
        {
            ln = max(ln, dist[i][j]);
        }
        cout <<ln << endl;
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/0456ms760 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms532 KiB
5Accepted2/21ms316 KiB
6Accepted2/21ms316 KiB
7Accepted2/214ms316 KiB
8Accepted2/214ms448 KiB
9Accepted2/214ms316 KiB
10Accepted2/24ms316 KiB
11Accepted2/214ms316 KiB
12Accepted2/2140ms572 KiB
13Accepted3/3165ms508 KiB
14Accepted3/318ms316 KiB
15Accepted3/3160ms316 KiB
16Accepted3/316ms316 KiB
17Accepted3/3123ms508 KiB
18Accepted3/328ms316 KiB
19Accepted3/31ms316 KiB
20Accepted3/33ms316 KiB
21Accepted3/337ms440 KiB
22Accepted3/3455ms576 KiB