답안 #977138

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
977138 2024-05-07T12:10:19 Z stefanopulos Nestabilnost (COI23_nestabilnost) C++17
12 / 100
1500 ms 105412 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 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
*/
 
 
 
 
 
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 36184 KB Output is correct
2 Correct 18 ms 36188 KB Output is correct
3 Correct 10 ms 36188 KB Output is correct
4 Correct 16 ms 36188 KB Output is correct
5 Correct 15 ms 36552 KB Output is correct
6 Correct 10 ms 36188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1541 ms 105412 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 35160 KB Output is correct
2 Correct 9 ms 35160 KB Output is correct
3 Correct 7 ms 35292 KB Output is correct
4 Correct 7 ms 35164 KB Output is correct
5 Correct 7 ms 35348 KB Output is correct
6 Correct 12 ms 35164 KB Output is correct
7 Incorrect 7 ms 35164 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 36184 KB Output is correct
2 Correct 18 ms 36188 KB Output is correct
3 Correct 10 ms 36188 KB Output is correct
4 Correct 16 ms 36188 KB Output is correct
5 Correct 15 ms 36552 KB Output is correct
6 Correct 10 ms 36188 KB Output is correct
7 Correct 7 ms 35160 KB Output is correct
8 Correct 9 ms 35160 KB Output is correct
9 Correct 7 ms 35292 KB Output is correct
10 Correct 7 ms 35164 KB Output is correct
11 Correct 7 ms 35348 KB Output is correct
12 Correct 12 ms 35164 KB Output is correct
13 Incorrect 7 ms 35164 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 36184 KB Output is correct
2 Correct 18 ms 36188 KB Output is correct
3 Correct 10 ms 36188 KB Output is correct
4 Correct 16 ms 36188 KB Output is correct
5 Correct 15 ms 36552 KB Output is correct
6 Correct 10 ms 36188 KB Output is correct
7 Execution timed out 1541 ms 105412 KB Time limit exceeded
8 Halted 0 ms 0 KB -