Submission #1071017

# Submission time Handle Problem Language Result Execution time Memory
1071017 2024-08-22T23:45:56 Z HorizonWest Closing Time (IOI23_closing) C++17
51 / 100
1000 ms 1889948 KB
//#include "closing.h"
#include <iostream>
#include <complex>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <bitset>
#include <queue>
#include <map>
#include <stack>
#include <cmath>
#include <cstdint>
#include <cassert>
using namespace std;
 
#define ll unsigned long long 
#define fs first 
#define sd second 
 
const ll Inf = 1e18 + 7;

vector <ll> bfs(int n, int x, vector <vector<pair<ll, ll>>> v)
{
    vector <ll> D(n + 1, Inf);
    queue <int> q; q.push(x); D[x] = 0; 
 
    while (!q.empty())
    {
        int x = q.front(); q.pop();
 
        for(auto& u : v[x]) if(D[u.fs] == Inf)
        {   
            D[u.fs] = D[x] + u.sd; 
            q.push(u.fs);
        }
    }
    
    return D;
}

vector <int> line(int n, int x, int y, vector <vector<pair<ll, ll>>> v)
{
    vector <int> p(n, -1), s;  
    queue <int> q; q.push(x); p[x] = x;

    while (!q.empty())
    {
        int x = q.front(); q.pop();
 
        for(auto& u : v[x]) if(p[u.fs] == -1)
        {   
            p[u.fs] = x; 
            q.push(u.fs);
        }
    }

    while (true)
    {
        s.push_back(y);
        if(y == x) break;
        y = p[y];
    }

    reverse(s.begin(), s.end());
    
    return s;
}
 
int max_score(int n, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W)
{
    vector <vector<pair<ll, ll>>> v(n + 1,  vector <pair<ll, ll>> ());
 
    for(int i = 0; i < n - 1; i++)
    {
        v[U[i]].push_back({ V[i], W[i] });        
        v[V[i]].push_back({ U[i], W[i] });
    }
    
    vector <ll> d1 = bfs(n, X, v), d2 = bfs(n, Y, v);
    vector <int> s = line(n, X, Y, v), sz(n), p(n); 
    int ans = 0, m = s.size(); 

    auto S = [&](auto S, int node, int parent) -> int
    {
        if(sz[node] != 0) return sz[node];
        sz[node] = 2;
        for(auto& u : v[node]) if(u.fs != parent){
            sz[node] += S(S, u.fs, node); 
        }
        return sz[node];
    };

    auto F = [&] (auto F, int node, int parent) -> vector <vector<ll>>
    {     
        vector <vector<ll>> dp1(S(S, node, parent) + 1, vector <ll> (3, Inf));
        dp1[1][0] = d1[node];
        dp1[1][1] = d2[node];
        dp1[2][2] = max(d1[node], d2[node]);

        for(auto& u : v[node]) if(u.fs != parent)
        {
            vector <vector<ll>> dp2 = F(F, u.fs, node);

            for(int j = dp1.size() - 1; j >= 0; j--)
            {
                for(int k = 0; k < dp2.size(); k++) if(j - k >= 0)
                {
                    dp1[j][0] = min(dp1[j][0], dp1[j - k][0] + dp2[k][0]);
                    dp1[j][1] = min(dp1[j][1], dp1[j - k][1] + dp2[k][1]);
                    dp1[j][2] = min(dp1[j][2], dp1[j - k][2] + min(dp2[k][2], 
                                min(dp2[k][0], dp2[k][1])));
                }
            }
        }

        return dp1;
    };

    vector <vector<vector<ll>>> dp(m, vector <vector<ll>> 
        (2 * n + 1, vector <ll> (4, Inf)));

    for(int i = 0; i < m; i++){
        //dp[i][0][0] = dp[i][0][1] = dp[i][0][2] = 0; 
        p[s[i]] = 1; //cerr << d1[s[i]] << " " << d2[s[i]] << " " << s[i] << endl;
    }

    dp[0][1][0] = d1[s[0]]; 
    dp[0][1][1] = d2[s[0]]; 
    dp[0][2][2] = max(d1[s[0]], d2[s[0]]); 

    for(int i = 0; i < m; i++)
    { 
        if(i > 0){
            for(int j = 0; j <= 2 * n; j++){
                if(j > 0) dp[i][j][0] = min(dp[i][j][0], dp[i-1][j-1][0] + d1[s[i]]); // X -> i
                
                if(j > 0) dp[i][j][1] = min(dp[i][j][1], dp[i-1][j-1][1] + d2[s[i]]); // Y -> i
                if(j > 0) dp[i][j][1] = min(dp[i][j][1], dp[i-1][j-1][2] + d2[s[i]]);
                if(j > 0) dp[i][j][1] = min(dp[i][j][1], dp[i-1][j-1][3] + d2[s[i]]);

                if(j > 1) dp[i][j][2] = min(dp[i][j][2], dp[i-1][j-2][0] + max(d1[s[i]], d2[s[i]])); // X, Y -> i
                if(j > 1) dp[i][j][2] = min(dp[i][j][2], dp[i-1][j-2][2] + max(d1[s[i]], d2[s[i]]));
                

                if(j > 0) dp[i][j][3] = min(dp[i][j][3], dp[i-1][j][0]);
                if(j > 0) dp[i][j][3] = min(dp[i][j][3], dp[i-1][j][3]);
            }
        }

        for(auto& u : v[s[i]]) if(p[u.fs] == 0)
        {  
            vector <vector<ll>> dp1 = F(F, u.fs, s[i]);

            /*cerr << "Node: " << s[i] << " -> " << u.fs << endl; 
            for(int j = 0; j < dp1.size(); j ++){
                cerr << j << ": " << (dp1[j][0] == Inf ? -1 : dp1[j][0]) << " " 
                    << (dp1[j][1] == Inf ? -1 : dp1[j][1]) << " " 
                    << (dp1[j][2] == Inf ? -1 : dp1[j][2]) << endl;
            }*/

            for(int j = 2*n; j >= 0; j--)
            {
                for(int k = 0; k < dp1.size(); k++) if(j - k >= 0)
                {
                    dp[i][j][0] = min(dp[i][j][0], dp[i][j - k][0] + dp1[k][0]);
                    dp[i][j][1] = min(dp[i][j][1], dp[i][j - k][1] + dp1[k][1]);
                    dp[i][j][2] = min(dp[i][j][2], dp[i][j - k][2] + min(dp1[k][2], 
                                min(dp1[k][0], dp1[k][1])));
                }
            }
        }

        /*cerr << "Node: " << s[i] << " -> " << i << endl; 
        for(int j = 0; j < dp[i].size(); j ++){
            cerr << j << ": " << (dp[i][j][0] == Inf ? -1 : dp[i][j][0]) << " " 
                 << (dp[i][j][1] == Inf ? -1 : dp[i][j][1]) << " " 
                 << (dp[i][j][2] == Inf ? -1 : dp[i][j][2]) << endl;
        }*/
    }

    for(int i = 0; i <= 2*n; i++)
    { 
        for(int j = 0; j < 3; j++) if(dp[m-1][i][j] <= K){
            ans = i;   // cerr << i << " " << dp[m-1][i][j] << " " << K << endl;
        }
    }

    return ans;
}

Compilation message

closing.cpp: In lambda function:
closing.cpp:107:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long unsigned int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |                 for(int k = 0; k < dp2.size(); k++) if(j - k >= 0)
      |                                ~~^~~~~~~~~~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:164:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long unsigned int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |                 for(int k = 0; k < dp1.size(); k++) if(j - k >= 0)
      |                                ~~^~~~~~~~~~~~
closing.cpp:184:53: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<long long unsigned int>, long long unsigned int>::value_type' {aka 'long long unsigned int'} and 'long long int' [-Wsign-compare]
  184 |         for(int j = 0; j < 3; j++) if(dp[m-1][i][j] <= K){
closing.cpp: In instantiation of 'max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)::<lambda(auto:1, int, int)> [with auto:1 = max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)::<lambda(auto:1, int, int)>]':
closing.cpp:96:34:   required from 'max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)::<lambda(auto:2, int, int)> [with auto:2 = max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)::<lambda(auto:2, int, int)>]'
closing.cpp:153:54:   required from here
closing.cpp:88:40: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
   88 |         for(auto& u : v[node]) if(u.fs != parent){
      |                                   ~~~~~^~~~~~~~~
closing.cpp: In instantiation of 'max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)::<lambda(auto:2, int, int)> [with auto:2 = max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)::<lambda(auto:2, int, int)>]':
closing.cpp:153:54:   required from here
closing.cpp:101:40: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
  101 |         for(auto& u : v[node]) if(u.fs != parent)
      |                                   ~~~~~^~~~~~~~~
closing.cpp:107:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long unsigned int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |                 for(int k = 0; k < dp2.size(); k++) if(j - k >= 0)
      |                                ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1133 ms 1889948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 688 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 3 ms 860 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 2 ms 856 KB Output is correct
16 Correct 1 ms 1628 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 138 ms 8324 KB Output is correct
20 Correct 216 ms 10844 KB Output is correct
21 Correct 27 ms 22108 KB Output is correct
22 Correct 71 ms 20056 KB Output is correct
23 Correct 29 ms 35680 KB Output is correct
24 Correct 29 ms 35676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 688 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 3 ms 860 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 2 ms 856 KB Output is correct
16 Correct 1 ms 1628 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 138 ms 8324 KB Output is correct
20 Correct 216 ms 10844 KB Output is correct
21 Correct 27 ms 22108 KB Output is correct
22 Correct 71 ms 20056 KB Output is correct
23 Correct 29 ms 35680 KB Output is correct
24 Correct 29 ms 35676 KB Output is correct
25 Correct 12 ms 856 KB Output is correct
26 Execution timed out 1108 ms 237508 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 416 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 372 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 688 KB Output is correct
13 Correct 3 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 1 ms 1628 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 0 ms 416 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 372 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 1372 KB Output is correct
44 Correct 1 ms 688 KB Output is correct
45 Correct 1 ms 860 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 688 KB Output is correct
13 Correct 3 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 1 ms 1628 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 138 ms 8324 KB Output is correct
21 Correct 216 ms 10844 KB Output is correct
22 Correct 27 ms 22108 KB Output is correct
23 Correct 71 ms 20056 KB Output is correct
24 Correct 29 ms 35680 KB Output is correct
25 Correct 29 ms 35676 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 416 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 372 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 604 KB Output is correct
50 Correct 1 ms 1372 KB Output is correct
51 Correct 1 ms 688 KB Output is correct
52 Correct 1 ms 860 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 2 ms 572 KB Output is correct
60 Correct 8 ms 1628 KB Output is correct
61 Correct 3 ms 1116 KB Output is correct
62 Correct 3 ms 1116 KB Output is correct
63 Correct 15 ms 1208 KB Output is correct
64 Correct 15 ms 1624 KB Output is correct
65 Correct 7 ms 3424 KB Output is correct
66 Correct 11 ms 860 KB Output is correct
67 Correct 13 ms 1372 KB Output is correct
68 Correct 23 ms 27088 KB Output is correct
69 Correct 24 ms 27736 KB Output is correct
70 Correct 16 ms 9052 KB Output is correct
71 Correct 19 ms 12380 KB Output is correct
72 Correct 5 ms 1368 KB Output is correct
73 Correct 5 ms 744 KB Output is correct
74 Correct 7 ms 604 KB Output is correct
75 Correct 2 ms 576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 688 KB Output is correct
13 Correct 3 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 1 ms 1628 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 138 ms 8324 KB Output is correct
21 Correct 216 ms 10844 KB Output is correct
22 Correct 27 ms 22108 KB Output is correct
23 Correct 71 ms 20056 KB Output is correct
24 Correct 29 ms 35680 KB Output is correct
25 Correct 29 ms 35676 KB Output is correct
26 Correct 12 ms 856 KB Output is correct
27 Execution timed out 1108 ms 237508 KB Time limit exceeded
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 688 KB Output is correct
13 Correct 3 ms 860 KB Output is correct
14 Correct 3 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 1 ms 1628 KB Output is correct
18 Correct 1 ms 1628 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 138 ms 8324 KB Output is correct
21 Correct 216 ms 10844 KB Output is correct
22 Correct 27 ms 22108 KB Output is correct
23 Correct 71 ms 20056 KB Output is correct
24 Correct 29 ms 35680 KB Output is correct
25 Correct 29 ms 35676 KB Output is correct
26 Correct 12 ms 856 KB Output is correct
27 Execution timed out 1108 ms 237508 KB Time limit exceeded
28 Halted 0 ms 0 KB -