제출 #575436

#제출 시각아이디문제언어결과실행 시간메모리
575436timreizinThe Xana coup (BOI21_xanadu)C++17
컴파일 에러
0 ms0 KiB
#include <iostream>
#include <vector>
#include <array>

using namespace std;

const int INF = 1e8;

using ll = long long;

void solve()
{
    int n;
    cin >> n;
    vector<vector<int>> adj(n + 1);
    for (int i = 0; i + 1 < n; ++i)
    {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    vector<int> a(n);
    for (int &i : a) cin >> i;
    a.insert(a.begin(), 0);
    vector<array<ll, 4>> dp(n + 1, {INF, INF, INF, INF});
    //first bit - he, second - father
    auto dfs = [&adj, &a, &dp](auto self, int v, int p) -> void
    {
        if (adj[v].size() == 1 && p != 0)
        {
            dp[v][a[v]] = 0;
            dp[v][3 - a[v]] = 1;
            return;
        }
        for (int u : adj[v])
        {
            if (u != p)
            {
                self(self, u, v);
            }
        }
        //we don't do operation
        //then parity of number of ones
        //let sum = dp[u][0], d_i = dp[u][2] - dp[u][0]
        //depending on parity we add to one or other mask
        vector<ll> deltas;
        ll sum = 0;
        int cnt = 0;
        for (int u : adj[v])
        {
            if (u != p)
            {
                if (dp[u][0] == INF && dp[u][2] == INF)
                {
                    sum = -1;
                    break;
                }
                if (dp[u][0] == INF)
                {
                    sum += dp[u][2];
                    ++cnt;
                    continue;
                }
                sum += dp[u][0];
                if (dp[u][2] != INF) deltas.push_back(dp[u][2] - dp[u][0]);
            }
        }
        if (sum != -1)
        {
            sort(deltas.begin(), deltas.end(), greater<>());
            cnt += a[v];
            for (int i = 0; i <= deltas.size(); ++i)
            {
                dp[v][cnt & 1] = min(dp[v][cnt & 1], sum);
                if (i != deltas.size())
                {
                    ++cnt;
                    sum += deltas[i];
                }
            }
        }
        //we do operation
        //then parity of number of ones
        //let sum = dp[u][1], d_i = dp[u][3] - dp[u][1]
        //depending on parity we add to one or other mask
        deltas.clear();
        sum = 0;
        cnt = 1;
        for (int u : adj[v])
        {
            if (u != p)
            {
                if (dp[u][1] == INF && dp[u][3] == INF)
                {
                    sum = -1;
                    break;
                }
                if (dp[u][1] == INF)
                {
                    sum += dp[u][3];
                    ++cnt;
                    continue;
                }
                sum += dp[u][1];
                if (dp[u][3] != INF) deltas.push_back(dp[u][3] - dp[u][1]);
            }
        }
        if (sum != -1)
        {
            sort(deltas.begin(), deltas.end(), greater<>());
            cnt += a[v];
            for (int i = 0; i <= deltas.size(); ++i)
            {
                dp[v][2 + (cnt & 1)] = min(dp[v][2 + (cnt & 1)], sum + 1);
                if (i != deltas.size())
                {
                    ++cnt;
                    sum += deltas[i];
                }
            }
        }
    };
    dfs(dfs, 1, 0);
    if (min(dp[1][0], dp[1][2]) == INF) cout << "impossible";
    else cout << min(dp[1][0], dp[1][2]);
}
/*
5
1 2
1 3
2 4
2 5
0 1 0 1 1
*/
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    //cin >> t;
    while (t--) solve();
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

xanadu.cpp: In lambda function:
xanadu.cpp:71:13: error: there are no arguments to 'sort' that depend on a template parameter, so a declaration of 'sort' must be available [-fpermissive]
   71 |             sort(deltas.begin(), deltas.end(), greater<>());
      |             ^~~~
xanadu.cpp:71:13: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
xanadu.cpp:73:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |             for (int i = 0; i <= deltas.size(); ++i)
      |                             ~~^~~~~~~~~~~~~~~~
xanadu.cpp:111:13: error: there are no arguments to 'sort' that depend on a template parameter, so a declaration of 'sort' must be available [-fpermissive]
  111 |             sort(deltas.begin(), deltas.end(), greater<>());
      |             ^~~~
xanadu.cpp:113:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |             for (int i = 0; i <= deltas.size(); ++i)
      |                             ~~^~~~~~~~~~~~~~~~
xanadu.cpp: In instantiation of 'solve()::<lambda(auto:1, int, int)> [with auto:1 = solve()::<lambda(auto:1, int, int)>]':
xanadu.cpp:124:18:   required from here
xanadu.cpp:71:17: error: 'sort' was not declared in this scope; did you mean 'qsort'?
   71 |             sort(deltas.begin(), deltas.end(), greater<>());
      |             ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |             qsort
xanadu.cpp:73:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |             for (int i = 0; i <= deltas.size(); ++i)
      |                             ~~^~~~~~~~~~~~~~~~
xanadu.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |                 if (i != deltas.size())
      |                     ~~^~~~~~~~~~~~~~~~
xanadu.cpp:111:17: error: 'sort' was not declared in this scope; did you mean 'qsort'?
  111 |             sort(deltas.begin(), deltas.end(), greater<>());
      |             ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |             qsort
xanadu.cpp:113:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |             for (int i = 0; i <= deltas.size(); ++i)
      |                             ~~^~~~~~~~~~~~~~~~
xanadu.cpp:116:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |                 if (i != deltas.size())
      |                     ~~^~~~~~~~~~~~~~~~