40222023-03-09 09:45:54gortomiMobilNet (50 pont)cpp17Accepted 50/5061ms16732 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int> p, r;
struct edge
{
    int l, r, val;
};
bool comp(edge a, edge b)
{
    return a.val < b.val;
}
int get(int n)
{
    return p[n] == 0 ? n : p[n] = get(p[n]);
}
void unio(int a, int b)
{
    a = get(a);
    b = get(b);
    if(a != b)
    {
        if(r[b] > r[a]) swap(a, b);
        p[b] = a;
        if(r[b] == r[a]) r[a]++;
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    p.resize(n + 1);
    r.resize(n + 1);
    map<int, vector<pair<int, int> > > x, y;
    for(int i = 1; i <= n; i++)
    {
        int a, b;
        cin >> a >> b;
        x[a].push_back({b, i});
        y[b].push_back({a, i});
    }
    vector<edge> e;
    for(auto a : x)
    {
        vector<pair<int, int> > v = a.second;
        sort(v.begin(), v.end());
        for(int i = 0; i < v.size() - 1; i++)
        {
            edge b = {v[i].second, v[i + 1].second, v[i + 1].first - v[i].first};
            e.push_back(b);
        }
    }
    for(auto a : y)
    {
        vector<pair<int, int> > v = a.second;
        sort(v.begin(), v.end());
        for(int i = 0; i < v.size() - 1; i++)
        {
            edge b = {v[i].second, v[i + 1].second, v[i + 1].first - v[i].first};
            e.push_back(b);
        }
    }
    sort(e.begin(), e.end(), comp);
    int maxi = 0, maxp = 0;
    int k = n;
    for(auto a : e)
    {
        if(a.val > maxi)
        {
            maxp = 0;
            maxi = a.val;
        }
        if(get(a.l) != get(a.r))
        {
            unio(a.l, a.r);
            maxp++;
            k--;
            if(k == 1)
            {
                break;
            }
        }
    }
    cout << maxi << "\n" << maxp;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1892 KiB
2Accepted0/07ms3276 KiB
3Accepted2/23ms2340 KiB
4Accepted2/23ms2668 KiB
5Accepted2/23ms2736 KiB
6Accepted2/23ms3124 KiB
7Accepted2/23ms3368 KiB
8Accepted2/23ms3776 KiB
9Accepted2/24ms4116 KiB
10Accepted2/24ms4516 KiB
11Accepted2/26ms4952 KiB
12Accepted2/29ms5672 KiB
13Accepted3/314ms6612 KiB
14Accepted3/318ms7240 KiB
15Accepted3/327ms9768 KiB
16Accepted3/329ms9852 KiB
17Accepted3/341ms12876 KiB
18Accepted3/328ms10068 KiB
19Accepted3/343ms12688 KiB
20Accepted3/343ms11716 KiB
21Accepted3/361ms16732 KiB
22Accepted3/357ms16584 KiB