답안 #977182

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
977182 2024-05-07T13:06:20 Z stefanopulos Nestabilnost (COI23_nestabilnost) C++17
컴파일 오류
0 ms 0 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];

ll sufi[mxN];

int mx[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);
    mx[x] = max(mx[x], mx[y]);
}
void init(){
    ff(i,1,n){
        par[i] = i;
        svi[i].pb(i);
        mx[i] = A[i] + 1;
    }
}

vector<int> ko[mxN];
 
vector<pii> edge;
void dfs1(int v, int p){
 
    for(auto u : g[v]){
        if(u != p){
            dfs1(u, v);
            
            if(A[v] + 1 == A[u]){
                unite(u, v);
            }
 
            if(A[u] == 0){
                edge.pb({v, u});
            }
 
        }
    }
 
}

ll dp[mxN];
ll get(int v, int p, int X){
    if(X <= A[v])return inf;
    
    ll sum = 0;
    for(auto u : g[v]){
        if(u == p)continue;

        if(A[v] + 1 == A[u]){
            int a = A[u] + 1;
            if(X >= a)sum += get(u, v, X);
            else sum += dp[u];
        }
        else if(A[u] == 0){
            int a = A[v] + 1;
            if(X == a)sum += get(u, v, X);
            else sum += dp[u];
        }
        else sum += dp[u];

    }
 
    return sum;
 
}

pll calc(int v, int p){

    ll najv = A[v] + 1, sum = 0;
    for(auto u : g[v]){
        if(u == p)continue;

        if(A[v] + 1 == A[u]){
            pll X = calc(u, v);
            najv = max(najv, X.fi);
            sum += X.se;
        }
        else sum += dp[u];

    }

    return {najv, sum};

}

void dfs(int v, int p){

    for(auto u : g[v]){
        if(u != p){
            dfs(u, v);
        }
    }

    // ili cu uzeti neko jednako ili cu samo uzeti svako vece povezano sa v

    dp[v] = inf;
    for(auto c : ko[findpar(v)]){
        dp[v] = min(dp[v], get(v, p, c) + 1ll * f[c]);
    }

    pll X = calc(v, p);
    dp[v] = min(dp[v], sufi[X.fi] + X.se);

    // cout << "v: " << v << '\n';
    // for(auto c : ko[findpar(v)])cout << c << " ";
    // cout << '\n';

    // cout << "mx & sum: " << X.fi << " " << X.se << '\n';

    // cout << "dp: " << dp[v] << '\n';

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

}

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(); dfs1(1, -1);

    for(auto c : edge){
        int u = findpar(c.fi);
        int v = findpar(c.se);
        ko[u].pb(A[c.fi] + 1);
    }

    dfs(1, -1);

    cout << dp[1] << '\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


7
2 3 0 3 2 0 0
6 8 2 9 9 9 1
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:181:49: error: no matching function for call to 'min(ll&, int&)'
  181 |     fb(i,n - 1,1)sufi[i] = min(sufi[i + 1], f[i]);
      |                                                 ^
In file included from /usr/include/c++/10/memory:63,
                 from /usr/include/c++/10/ext/pb_ds/detail/standard_policies.hpp:44,
                 from /usr/include/c++/10/ext/pb_ds/assoc_container.hpp:47,
                 from code1.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  230 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:230:5: note:   template argument deduction/substitution failed:
code1.cpp:181:49: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  181 |     fb(i,n - 1,1)sufi[i] = min(sufi[i + 1], f[i]);
      |                                                 ^
In file included from /usr/include/c++/10/memory:63,
                 from /usr/include/c++/10/ext/pb_ds/detail/standard_policies.hpp:44,
                 from /usr/include/c++/10/ext/pb_ds/assoc_container.hpp:47,
                 from code1.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  278 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:278:5: note:   template argument deduction/substitution failed:
code1.cpp:181:49: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  181 |     fb(i,n - 1,1)sufi[i] = min(sufi[i + 1], f[i]);
      |                                                 ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/c++/10/ext/pb_ds/hash_policy.hpp:45,
                 from /usr/include/c++/10/ext/pb_ds/detail/standard_policies.hpp:45,
                 from /usr/include/c++/10/ext/pb_ds/assoc_container.hpp:47,
                 from code1.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(std::initializer_list<_Tp>)'
 3468 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
code1.cpp:181:49: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  181 |     fb(i,n - 1,1)sufi[i] = min(sufi[i + 1], f[i]);
      |                                                 ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/c++/10/ext/pb_ds/hash_policy.hpp:45,
                 from /usr/include/c++/10/ext/pb_ds/detail/standard_policies.hpp:45,
                 from /usr/include/c++/10/ext/pb_ds/assoc_container.hpp:47,
                 from code1.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(std::initializer_list<_Tp>, _Compare)'
 3474 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3474:5: note:   template argument deduction/substitution failed:
code1.cpp:181:49: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  181 |     fb(i,n - 1,1)sufi[i] = min(sufi[i + 1], f[i]);
      |                                                 ^
code1.cpp:187:13: warning: unused variable 'v' [-Wunused-variable]
  187 |         int v = findpar(c.se);
      |             ^