답안 #976617

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
976617 2024-05-06T21:08:45 Z stefanopulos Nestabilnost (COI23_nestabilnost) C++17
0 / 100
1500 ms 95172 KB
#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 ll inf = 1e15 + 5;
const int mxN = 300005; 

int n;
int A[mxN];
int f[mxN];

vector<int> g[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<pii> edge;
void dfs(int v, int p){

    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 br[mxN];
void dfs1(int v, int p, int r){
    br[r] += (A[v] == 0);
    for(auto u : g[v]){
        if(u == p || A[u] != 0)continue;
        dfs1(u, v, r);
    }
}

int sufi[mxN];
ll calc(int X){
    return min(1ll * X * sufi[1], 1ll * f[1]);
}

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);

    // cout << "edge: " << '\n';
    // for(auto c : edge)cout << c.fi << " " << c.se << '\n';
    // cout << '\n';

    for(auto c : edge)vec[findpar(c.fi)].pb({c.se, A[c.fi] + 1});

    ll rez = 0;
    ff(i,1,n){
        if(A[i] == 0 || findpar(i) != i)continue;

        int mx = 0;
        for(auto c : svi[i])mx = max(mx, A[c] + 1);

        // cout << "i: " << i << '\n';
        // for(auto c : svi[i])cout << c << " ";
        // cout << '\n';

        for(auto c : vec[i]){
            int v = c.fi;
            int val = c.se;

            // A[v] = 0
            br[v] = 0; dfs1(v, -1, v);
            // cout << "v & val & br[v]: " << v << " " << val << " " << br[v] << '\n';

        }

        // cout << "mx: " << mx << '\n';

        ll najm = inf;
        ff(k,mx,n){

            ll sum = 0;
            for(auto c : vec[i]){
                int v = c.fi;
                int val = c.se;

                if(val == k){
                    sum += calc(br[v] - 1);
                }
                else{
                    sum += calc(br[v]);
                }

            }

            sum += f[k];
            najm = min(najm, sum);

        }

        rez += najm;

        // cout << "najm: " << najm << '\n';

        // cout << "-------------------------------" << '\n';
        // cout << '\n';

    }

    cout << (rez == 0 ? calc(n) : 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
*/
 
 
 
 
 

Compilation message

code1.cpp: In function 'int main()':
code1.cpp:138:17: warning: unused variable 'val' [-Wunused-variable]
  138 |             int val = c.se;
      |                 ^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 27992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 95172 KB Output is correct
2 Execution timed out 1598 ms 84048 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 26968 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 27992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 27992 KB Output isn't correct
2 Halted 0 ms 0 KB -