답안 #964333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
964333 2024-04-16T16:24:16 Z Requiem Parkovi (COCI22_parkovi) C++17
20 / 110
1140 ms 37320 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;
        }
        queue<int> q;
        q.push(1);
        int cur = res;
        while(!q.empty()){
            int u = q.front();
            q.pop();
            if (cur < 0){
                cur = res;
                k--;
                chosen[u] = 1;
            }
            for(auto idedge: g[u]){
                int v = edge[idedge].se.fi + edge[idedge].se.se - u;
                int w = edge[idedge].fi;
                if (v == u - 1) continue;
                if (cur - w < 0){
                    k--;
                    cur = res - w;
                    chosen[u] = 1;
                }
                else cur -= w;
                q.push(v);
            }
        }

        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: At global scope:
Main.cpp:230:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  230 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:234:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  234 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:235:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  235 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7516 KB Output is correct
2 Correct 6 ms 7516 KB Output is correct
3 Correct 9 ms 7516 KB Output is correct
4 Correct 6 ms 7516 KB Output is correct
5 Correct 10 ms 7516 KB Output is correct
6 Correct 18 ms 7512 KB Output is correct
7 Correct 21 ms 7516 KB Output is correct
8 Correct 10 ms 7520 KB Output is correct
9 Correct 76 ms 7520 KB Output is correct
10 Correct 11 ms 7520 KB Output is correct
11 Correct 44 ms 7260 KB Output is correct
12 Correct 81 ms 7516 KB Output is correct
13 Correct 7 ms 7512 KB Output is correct
14 Correct 41 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 35212 KB Output is correct
2 Correct 74 ms 37320 KB Output is correct
3 Correct 81 ms 27428 KB Output is correct
4 Correct 137 ms 25756 KB Output is correct
5 Correct 156 ms 25384 KB Output is correct
6 Correct 102 ms 25416 KB Output is correct
7 Correct 141 ms 24244 KB Output is correct
8 Correct 112 ms 25088 KB Output is correct
9 Correct 122 ms 25384 KB Output is correct
10 Correct 130 ms 25680 KB Output is correct
11 Correct 102 ms 27648 KB Output is correct
12 Correct 92 ms 26896 KB Output is correct
13 Correct 99 ms 28712 KB Output is correct
14 Correct 93 ms 25516 KB Output is correct
15 Correct 82 ms 25644 KB Output is correct
16 Correct 106 ms 26704 KB Output is correct
17 Correct 85 ms 26192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1140 ms 31468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7516 KB Output is correct
2 Correct 6 ms 7516 KB Output is correct
3 Correct 9 ms 7516 KB Output is correct
4 Correct 6 ms 7516 KB Output is correct
5 Correct 10 ms 7516 KB Output is correct
6 Correct 18 ms 7512 KB Output is correct
7 Correct 21 ms 7516 KB Output is correct
8 Correct 10 ms 7520 KB Output is correct
9 Correct 76 ms 7520 KB Output is correct
10 Correct 11 ms 7520 KB Output is correct
11 Correct 44 ms 7260 KB Output is correct
12 Correct 81 ms 7516 KB Output is correct
13 Correct 7 ms 7512 KB Output is correct
14 Correct 41 ms 7260 KB Output is correct
15 Correct 62 ms 35212 KB Output is correct
16 Correct 74 ms 37320 KB Output is correct
17 Correct 81 ms 27428 KB Output is correct
18 Correct 137 ms 25756 KB Output is correct
19 Correct 156 ms 25384 KB Output is correct
20 Correct 102 ms 25416 KB Output is correct
21 Correct 141 ms 24244 KB Output is correct
22 Correct 112 ms 25088 KB Output is correct
23 Correct 122 ms 25384 KB Output is correct
24 Correct 130 ms 25680 KB Output is correct
25 Correct 102 ms 27648 KB Output is correct
26 Correct 92 ms 26896 KB Output is correct
27 Correct 99 ms 28712 KB Output is correct
28 Correct 93 ms 25516 KB Output is correct
29 Correct 82 ms 25644 KB Output is correct
30 Correct 106 ms 26704 KB Output is correct
31 Correct 85 ms 26192 KB Output is correct
32 Incorrect 1140 ms 31468 KB Output isn't correct
33 Halted 0 ms 0 KB -