Submission #391273

#TimeUsernameProblemLanguageResultExecution timeMemory
391273MrRobot_28Sjekira (COCI20_sjekira)C++17
110 / 110
161 ms35524 KiB
#include<bits/stdc++.h> using namespace std; #define X first #define Y second #define sz(a) (int)a.size() #define ll long long #define int 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); vector <set <pair <int, int> > > st(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); } for(int i = 0; i < n; i++) { for(auto to : g[i]) { if(val[to] > val[i] || val[to] == val[i] && to > i) { st[i].insert({val[to], to}); } } } ll s = 0; for(int i = 0; i < n; i++) { int minto = -1; int v = a[i].Y; if(sz(st[v]) != 0) { pair <int, int> t = *st[v].begin(); st[v].erase(t); s += t.X + val[v]; // cout << t.X << " " << val[v] << "\n"; if(sz(st[t.Y]) < sz(st[v])) { swap(st[t.Y], st[v]); } for(auto p : st[v]) { st[t.Y].insert(p); } } } cout << s; return 0; }

Compilation message (stderr)

sjekira.cpp: In function 'int main()':
sjekira.cpp:65:54: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   65 |             if(val[to] > val[i] || val[to] == val[i] && to > i)
      |                                    ~~~~~~~~~~~~~~~~~~^~~~~~~~~
sjekira.cpp:74:13: warning: unused variable 'minto' [-Wunused-variable]
   74 |         int minto = -1;
      |             ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...