Submission #964335

# Submission time Handle Problem Language Result Execution time Memory
964335 2024-04-16T16:27:11 Z Requiem Parkovi (COCI22_parkovi) C++17
50 / 110
1297 ms 37208 KB
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define endl "\n"
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define pi 3.14159265359
#define TASKNAME "parkovi"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res >= val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 3e5 + 9;
vector<int> g[MAXN];
iii edge[MAXN];
int n, k;

namespace sub1{
    bool checkSubtask1(){
        return n <= 20;
    }
    int dist[21];
    vector<int> SetVertice;
    void bfs(){
        priority_queue<ii, vector<ii>, greater<ii> > q;
        fill(dist + 1, dist + 1 + n, INF);
        for(auto x: SetVertice){
            dist[x] = 0;
            q.push({0, x});
        }

        while(!q.empty()){
              int u = q.top().se;
              int du = q.top().fi;
              q.pop();
              if (du > dist[u]) continue;
              for(auto idedge: g[u]){
                  int v = edge[idedge].se.fi + edge[idedge].se.se - u;
                  int w = edge[idedge].fi;
                  if (dist[v] > dist[u] + w){
                      dist[v] = dist[u] + w;
                      q.push({dist[v], v});
                  }
              }
        }
    }

    void solveSubtask1(){
         int res = INF;
         int msk = 0;
         for(int i = 1; i < (1 << n); i++){
             SetVertice.clear();
             if (__builtin_popcount(i) != k) continue;
             for(int j = 0; j < n; j++){
                 if (i & (1 << j)) SetVertice.pb(j + 1);
             }
             bfs();
             if (minimize(res, *max_element(dist + 1, dist + 1 + n))) msk = i;
         }
         cout << res << endl;
         vector<int> park;
         for(int i = 0; i < n; i++){
             if (msk & (1 << i)) park.pb(i + 1);
         }
         for(auto x: park){
             cout << x << " ";
         }
         cout << endl;
    }
}

namespace sub2{
    bool checkSubtask2(){
        return k == 1;
    }

    int par[MAXN], depth[MAXN];
    void dfs(int u,int p){
        for(auto idedge: g[u]){
            int v = edge[idedge].se.fi + edge[idedge].se.se - u;
            int w = edge[idedge].fi;
            if (v == p) continue;
            depth[v] = depth[u] + w;
            par[v] = u;
            dfs(v, u);
        }
    }

    void reset(){
         fill(par + 1, par + 1 + n, -1);
         fill(depth + 1, depth + 1 + n, 0);
    }

    void solveSubtask2(){
         dfs(1, -1);
         int root = max_element(depth + 1, depth + 1 + n) - depth;
         reset();
         dfs(root, -1);
         int furthest = max_element(depth + 1, depth + 1 + n) - depth;

         vector<int> diameter;
         int res = INF, tmp = furthest, chosen = 0;
         while(tmp != root) {
               if (minimize(res, max(depth[tmp], depth[furthest] - depth[tmp]))){
                   chosen = tmp;
               }
               tmp = par[tmp];
         }

         cout << res << endl;
         cout << chosen << endl;


    }
}

namespace sub3{
    bool checkSubtask3(){
        for(int i = 1; i < n; i++){
            if (edge[i].se.fi != i or edge[i].se.se != i + 1) return false;
        }
        return true;
    }

    int chosen[MAXN], pre[MAXN];

    bool inter(ii a, ii b){
        return !(a.fi > b.se or a.se < b.fi);
    }
    bool check(int mid){
         for(int i = 1; i < n; i++){
             pre[i] = pre[i - 1] + edge[i].fi;
//             cout << pre[i] << ' ';
         }
//         cout << endl;
         vector<ii> Ranges;
         for(int i = 1; i <= n; i++){
             int lo = 1, hi = i, leftboard = 0;
             while(lo <= hi){
                int m = (lo + hi) / 2;
                if (pre[i - 1] - pre[m - 1] <= mid){
                    leftboard = m;
                    hi = m - 1;
                }
                else lo = m + 1;
             }
             lo = i, hi = n;
             int rightboard = 0;
             while(lo <= hi){
                int m = (lo + hi) / 2;
                if (pre[m - 1] - pre[i - 1] <= mid){
//                    cout << mid << ' ' << pre[mid - 1] - pre[i - 1] << ' ' << mid << endl;
                    rightboard = m;
                    lo = m + 1;
                }
                else hi = m - 1;
             }
//             cout << i << ' ' <<  leftboard << ' ' << rightboard << endl;
             Ranges.pb({leftboard, rightboard});
         }
         sort(Ranges.begin(), Ranges.end());
         int numgroup  = 0;
         for(int l = 0, last = 0; l < Ranges.size(); l = last + 1){
             last = l;
             ii intersect = Ranges[l];
             while(last < Ranges.size() - 1 and inter(intersect, Ranges[last + 1])){
                   last++;
                   intersect = ii(max(intersect.fi, Ranges[last].fi),
                                  min(intersect.se, Ranges[last].se));
             }
             numgroup++;
         }
         return numgroup <= k;
    }
    void SolveSubtask3(){
        int l = 1;
        int r = 1e18;
        int res= 0;
        while(l <= r){
            int mid = (l + r) >> 1;
            if (check(mid)) {
                res = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
         for(int i = 1; i < n; i++){
             pre[i] = pre[i - 1] + edge[i].fi;
//             cout << pre[i] << ' ';
         }
//         cout << endl;
         vector<ii> Ranges;
         for(int i = 1; i <= n; i++){
             int lo = 1, hi = i, leftboard = 0;
             while(lo <= hi){
                int m = (lo + hi) / 2;
                if (pre[i - 1] - pre[m - 1] <= res){
                    leftboard = m;
                    hi = m - 1;
                }
                else lo = m + 1;
             }
             lo = i, hi = n;
             int rightboard = 0;
             while(lo <= hi){
                int m = (lo + hi) / 2;
                if (pre[m - 1] - pre[i - 1] <= res){
//                    cout << mid << ' ' << pre[mid - 1] - pre[i - 1] << ' ' << mid << endl;
                    rightboard = m;
                    lo = m + 1;
                }
                else hi = m - 1;
             }
//             cout << i << ' ' <<  leftboard << ' ' << rightboard << endl;
             Ranges.pb({leftboard, rightboard});
         }
         sort(Ranges.begin(), Ranges.end());
         for(int l = 0, last = 0; l < Ranges.size(); l = last + 1){
             last = l;
             ii intersect = Ranges[l];
             while(last < Ranges.size() - 1 and inter(intersect, Ranges[last + 1])){
                   last++;
                   intersect = ii(max(intersect.fi, Ranges[last].fi),
                                  min(intersect.se, Ranges[last].se));
             }
             chosen[intersect.fi] = 1;
             k--;
         }

        for(int i = 1; i <= n; i++){
            if (!chosen[i] and k > 0) chosen[i] = 1, k--;
        }
        cout << res << endl;
        for(int i = 1; i <= n; i++){
            if (chosen[i]) cout << i << " ";
        }
        cout << endl;
    }
}
main()
{
    fast;
   if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin);
        freopen(TASKNAME".out","w",stdout);
   }
   cin >> n >> k;
   for(int i = 1; i < n; i++){
       int u, v, w;
       cin >> u >> v >> w;
       edge[i] = {w, {u, v}};
       g[u].pb(i);
       g[v].pb(i);
   }

   if (sub1::checkSubtask1()) return sub1::solveSubtask1(), 0;
   if (sub2::checkSubtask2()) return sub2::solveSubtask2(), 0;
   if (sub3::checkSubtask3()) return sub3::SolveSubtask3(), 0;
}

Compilation message

Main.cpp: In function 'bool sub3::check(long long int)':
Main.cpp:171:37: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |          for(int l = 0, last = 0; l < Ranges.size(); l = last + 1){
      |                                   ~~^~~~~~~~~~~~~~~
Main.cpp:174:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  174 |              while(last < Ranges.size() - 1 and inter(intersect, Ranges[last + 1])){
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void sub3::SolveSubtask3()':
Main.cpp:226:37: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  226 |          for(int l = 0, last = 0; l < Ranges.size(); l = last + 1){
      |                                   ~~^~~~~~~~~~~~~~~
Main.cpp:229:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  229 |              while(last < Ranges.size() - 1 and inter(intersect, Ranges[last + 1])){
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp: At global scope:
Main.cpp:248:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  248 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:252:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  252 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:253:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  253 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7520 KB Output is correct
2 Correct 6 ms 7512 KB Output is correct
3 Correct 7 ms 7516 KB Output is correct
4 Correct 6 ms 7516 KB Output is correct
5 Correct 6 ms 7496 KB Output is correct
6 Correct 19 ms 7500 KB Output is correct
7 Correct 20 ms 7516 KB Output is correct
8 Correct 10 ms 7512 KB Output is correct
9 Correct 76 ms 7516 KB Output is correct
10 Correct 11 ms 7520 KB Output is correct
11 Correct 44 ms 7512 KB Output is correct
12 Correct 88 ms 7516 KB Output is correct
13 Correct 10 ms 7512 KB Output is correct
14 Correct 39 ms 7260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 35076 KB Output is correct
2 Correct 76 ms 37208 KB Output is correct
3 Correct 73 ms 27372 KB Output is correct
4 Correct 147 ms 25984 KB Output is correct
5 Correct 111 ms 25372 KB Output is correct
6 Correct 125 ms 25336 KB Output is correct
7 Correct 141 ms 24148 KB Output is correct
8 Correct 109 ms 24916 KB Output is correct
9 Correct 119 ms 25168 KB Output is correct
10 Correct 144 ms 25684 KB Output is correct
11 Correct 102 ms 27800 KB Output is correct
12 Correct 104 ms 26932 KB Output is correct
13 Correct 117 ms 28832 KB Output is correct
14 Correct 93 ms 25656 KB Output is correct
15 Correct 87 ms 25700 KB Output is correct
16 Correct 109 ms 26540 KB Output is correct
17 Correct 96 ms 26316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1237 ms 31576 KB Output is correct
2 Correct 1226 ms 30836 KB Output is correct
3 Correct 1171 ms 29952 KB Output is correct
4 Correct 68 ms 35512 KB Output is correct
5 Correct 992 ms 33080 KB Output is correct
6 Correct 1008 ms 32544 KB Output is correct
7 Correct 1035 ms 33460 KB Output is correct
8 Correct 1297 ms 30928 KB Output is correct
9 Correct 1274 ms 30704 KB Output is correct
10 Correct 1212 ms 30248 KB Output is correct
11 Correct 80 ms 35276 KB Output is correct
12 Correct 1020 ms 33208 KB Output is correct
13 Correct 1071 ms 33312 KB Output is correct
14 Correct 1155 ms 32524 KB Output is correct
15 Correct 955 ms 29524 KB Output is correct
16 Correct 919 ms 28832 KB Output is correct
17 Correct 971 ms 28756 KB Output is correct
18 Correct 68 ms 35920 KB Output is correct
19 Correct 849 ms 31120 KB Output is correct
20 Correct 928 ms 31348 KB Output is correct
21 Correct 940 ms 30896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7520 KB Output is correct
2 Correct 6 ms 7512 KB Output is correct
3 Correct 7 ms 7516 KB Output is correct
4 Correct 6 ms 7516 KB Output is correct
5 Correct 6 ms 7496 KB Output is correct
6 Correct 19 ms 7500 KB Output is correct
7 Correct 20 ms 7516 KB Output is correct
8 Correct 10 ms 7512 KB Output is correct
9 Correct 76 ms 7516 KB Output is correct
10 Correct 11 ms 7520 KB Output is correct
11 Correct 44 ms 7512 KB Output is correct
12 Correct 88 ms 7516 KB Output is correct
13 Correct 10 ms 7512 KB Output is correct
14 Correct 39 ms 7260 KB Output is correct
15 Correct 70 ms 35076 KB Output is correct
16 Correct 76 ms 37208 KB Output is correct
17 Correct 73 ms 27372 KB Output is correct
18 Correct 147 ms 25984 KB Output is correct
19 Correct 111 ms 25372 KB Output is correct
20 Correct 125 ms 25336 KB Output is correct
21 Correct 141 ms 24148 KB Output is correct
22 Correct 109 ms 24916 KB Output is correct
23 Correct 119 ms 25168 KB Output is correct
24 Correct 144 ms 25684 KB Output is correct
25 Correct 102 ms 27800 KB Output is correct
26 Correct 104 ms 26932 KB Output is correct
27 Correct 117 ms 28832 KB Output is correct
28 Correct 93 ms 25656 KB Output is correct
29 Correct 87 ms 25700 KB Output is correct
30 Correct 109 ms 26540 KB Output is correct
31 Correct 96 ms 26316 KB Output is correct
32 Correct 1237 ms 31576 KB Output is correct
33 Correct 1226 ms 30836 KB Output is correct
34 Correct 1171 ms 29952 KB Output is correct
35 Correct 68 ms 35512 KB Output is correct
36 Correct 992 ms 33080 KB Output is correct
37 Correct 1008 ms 32544 KB Output is correct
38 Correct 1035 ms 33460 KB Output is correct
39 Correct 1297 ms 30928 KB Output is correct
40 Correct 1274 ms 30704 KB Output is correct
41 Correct 1212 ms 30248 KB Output is correct
42 Correct 80 ms 35276 KB Output is correct
43 Correct 1020 ms 33208 KB Output is correct
44 Correct 1071 ms 33312 KB Output is correct
45 Correct 1155 ms 32524 KB Output is correct
46 Correct 955 ms 29524 KB Output is correct
47 Correct 919 ms 28832 KB Output is correct
48 Correct 971 ms 28756 KB Output is correct
49 Correct 68 ms 35920 KB Output is correct
50 Correct 849 ms 31120 KB Output is correct
51 Correct 928 ms 31348 KB Output is correct
52 Correct 940 ms 30896 KB Output is correct
53 Incorrect 83 ms 22892 KB Unexpected end of file - int64 expected
54 Halted 0 ms 0 KB -