Submission #391270

# Submission time Handle Problem Language Result Execution time Memory
391270 2021-04-18T11:08:34 Z MrRobot_28 Sjekira (COCI20_sjekira) C++17
0 / 110
79 ms 15856 KB
#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
#define ld long double
const int N = 3e5 + 100;
int dsu[N];
int val[N];
int rang[N];
vector <pair <int, int> > vec[N];
int pred(int a)
{
    if(a == dsu[a]) return a;
    return dsu[a] = pred(dsu[a]);
}
void unite(int a, int b)
{
    a = pred(a);
    b = pred(b);
    if(rang[b] < rang[a])
    {
        swap(a, b);
    }
    dsu[b] = a;
    rang[a] += rang[b];
    val[a] = max(val[a], val[b]);
}
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector <pair <int, int> > a(n);
    for(int i = 0; i < n; i++)
    {
        cin >> a[i].X;
        val[i] = a[i].X;
        a[i].Y = i;
        dsu[i] = i;
        rang[i] = 1;
    }
    sort(a.begin(), a.end());
    vector <vector <int> > g(n);
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a>> b;
        a--;
        b--;
        vec[a].push_back({a, b});
        vec[b].push_back({b, a});
        g[a].push_back(b);
        g[b].push_back(a);
    }
    ll s = 0;
    for(int i = 0; i < n; i++)
    {
        int minto = -1;
        int v = a[i].Y;
        for(int j = 0; j < sz(g[v]); j++)
        {
            if(pred(g[v][j]) != pred(v))
            {
                if(minto == -1 || val[pred(g[v][j])] < val[pred(minto)])
                {
                    minto = g[v][j];
                }
            }
        }
        if(minto != -1)
        {
        s += val[pred(v)] + val[pred(minto)];
        unite(minto, v);
        }
    }
    cout << s;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Incorrect 5 ms 7372 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 79 ms 15856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Incorrect 5 ms 7372 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Incorrect 5 ms 7372 KB Output isn't correct
4 Halted 0 ms 0 KB -