Submission #977138

#TimeUsernameProblemLanguageResultExecution timeMemory
977138stefanopulosNestabilnost (COI23_nestabilnost)C++17
12 / 100
1541 ms105412 KiB
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ldb; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<ldb,ldb> pdd; #define ff(i,a,b) for(int i = a; i <= b; i++) #define fb(i,b,a) for(int i = b; i >= a; i--) #define trav(a,x) for(auto& a : x) #define sz(a) (int)(a).size() #define fi first #define se second #define pb push_back #define lb lower_bound #define ub upper_bound #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // os.order_of_key(k) the number of elements in the os less than k // *os.find_by_order(k) print the k-th smallest number in os(0-based) const int mod = 1000000007; const int inf = 1e9 + 5; const int mxN = 300005; int n; int A[mxN]; int f[mxN]; vector<int> g[mxN]; int sufi[mxN]; int par[mxN]; vector<int> svi[mxN]; int findpar(int x){ return (x == par[x] ? x : par[x] = findpar(par[x])); } void unite(int x, int y){ x = findpar(x), y = findpar(y); if(x == y)return; if(sz(svi[x]) < sz(svi[y]))swap(x, y); par[y] = x; for(auto c : svi[y])svi[x].pb(c); } void init(){ ff(i,1,n){ par[i] = i; svi[i].pb(i); } } vector<pii> vec[mxN]; vector<int> ord; vector<pii> edge; void dfs(int v, int p){ ord.pb(v); for(auto u : g[v]){ if(u != p){ dfs(u, v); if(A[v] + 1 == A[u]){ unite(u, v); } if(A[u] == 0){ edge.pb({v, u}); } } } } int mx[mxN]; vector<pii> e[mxN]; ll dp[mxN]; bool visited[mxN]; ll get(int v, int p, int X){ ll sum = 0; for(auto c : e[v]){ int u = c.fi; int w = c.se; if(u != p){ if(w == X)sum += get(u, v, X); else sum += dp[u]; } } return sum; } void dfs1(int v, int p){ visited[v] = 1; dp[v] = sufi[mx[v]]; for(auto c : e[v]){ int u = c.fi; if(u != p){ dfs1(u, v); dp[v] += dp[u]; } } ll kol = get(v, p, mx[v]); dp[v] = min(dp[v], kol + f[mx[v]]); } int main(){ cin.tie(0)->sync_with_stdio(0); cin >> n; ff(i,1,n)cin >> A[i]; ff(i,1,n)cin >> f[i]; ff(i,1,n - 1){ int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } sufi[n] = f[n]; fb(i,n - 1,1)sufi[i] = min(sufi[i + 1], f[i]); init(); dfs(1, -1); ff(i,1,n){ if(findpar(i) == i){ for(auto c : svi[i])mx[i] = max(mx[i], A[c] + 1); } } for(auto c : edge){ int u = findpar(c.fi); int v = findpar(c.se); int w = A[c.fi] + 1; if(w >= mx[u] && w >= mx[v]){ e[u].pb({v, w}); e[v].pb({u, w}); } } ll rez = 0; for(auto c : ord){ int a = findpar(c); if(!visited[a]){ dfs1(a, -1); rez += dp[a]; } } cout << rez << '\n'; return 0; } /* 7 2 3 0 3 2 0 0 6 8 2 9 9 9 9 1 2 2 3 1 4 4 5 5 6 5 7 // probati bojenje sahovski */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...