답안 #964314

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
964314 2024-04-16T15:56:29 Z Requiem Parkovi (COCI22_parkovi) C++17
20 / 110
165 ms 41080 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];

    bool check(int mid){
        int minimum = 0;
        queue<int> q;
        q.push(1);
        int tmp = k;
        int cur = mid;
        while(!q.empty()){
            int u = q.front();
            // cout << u << ' ' << cur << endl;
            if (cur < 0){
                cur = mid;
                tmp--;
            }
            q.pop();
            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){
                    tmp--;
                    cur = mid - w;
                }
                else cur = cur - w;
                q.push(v);
            }
        }
        return tmp >= 0;
    }
    void SolveSubtask3(){
        int l = 1;
        int r = 2e14;
        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:136:13: warning: unused variable 'minimum' [-Wunused-variable]
  136 |         int minimum = 0;
      |             ^~~~~~~
Main.cpp: At global scope:
Main.cpp:210:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  210 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:214:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  214 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:215:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  215 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11100 KB Output is correct
2 Correct 8 ms 11100 KB Output is correct
3 Correct 6 ms 11100 KB Output is correct
4 Correct 5 ms 11100 KB Output is correct
5 Correct 6 ms 11096 KB Output is correct
6 Correct 17 ms 11100 KB Output is correct
7 Correct 23 ms 11096 KB Output is correct
8 Correct 10 ms 11096 KB Output is correct
9 Correct 72 ms 11140 KB Output is correct
10 Correct 9 ms 11100 KB Output is correct
11 Correct 41 ms 11100 KB Output is correct
12 Correct 80 ms 11144 KB Output is correct
13 Correct 7 ms 11096 KB Output is correct
14 Correct 39 ms 11096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 38972 KB Output is correct
2 Correct 77 ms 41080 KB Output is correct
3 Correct 71 ms 31028 KB Output is correct
4 Correct 123 ms 29940 KB Output is correct
5 Correct 104 ms 29308 KB Output is correct
6 Correct 129 ms 29544 KB Output is correct
7 Correct 116 ms 27476 KB Output is correct
8 Correct 134 ms 28200 KB Output is correct
9 Correct 144 ms 28524 KB Output is correct
10 Correct 153 ms 29324 KB Output is correct
11 Correct 86 ms 30184 KB Output is correct
12 Correct 91 ms 30292 KB Output is correct
13 Correct 103 ms 31956 KB Output is correct
14 Correct 78 ms 29812 KB Output is correct
15 Correct 82 ms 29204 KB Output is correct
16 Correct 92 ms 29928 KB Output is correct
17 Correct 83 ms 28756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 165 ms 25764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 11100 KB Output is correct
2 Correct 8 ms 11100 KB Output is correct
3 Correct 6 ms 11100 KB Output is correct
4 Correct 5 ms 11100 KB Output is correct
5 Correct 6 ms 11096 KB Output is correct
6 Correct 17 ms 11100 KB Output is correct
7 Correct 23 ms 11096 KB Output is correct
8 Correct 10 ms 11096 KB Output is correct
9 Correct 72 ms 11140 KB Output is correct
10 Correct 9 ms 11100 KB Output is correct
11 Correct 41 ms 11100 KB Output is correct
12 Correct 80 ms 11144 KB Output is correct
13 Correct 7 ms 11096 KB Output is correct
14 Correct 39 ms 11096 KB Output is correct
15 Correct 63 ms 38972 KB Output is correct
16 Correct 77 ms 41080 KB Output is correct
17 Correct 71 ms 31028 KB Output is correct
18 Correct 123 ms 29940 KB Output is correct
19 Correct 104 ms 29308 KB Output is correct
20 Correct 129 ms 29544 KB Output is correct
21 Correct 116 ms 27476 KB Output is correct
22 Correct 134 ms 28200 KB Output is correct
23 Correct 144 ms 28524 KB Output is correct
24 Correct 153 ms 29324 KB Output is correct
25 Correct 86 ms 30184 KB Output is correct
26 Correct 91 ms 30292 KB Output is correct
27 Correct 103 ms 31956 KB Output is correct
28 Correct 78 ms 29812 KB Output is correct
29 Correct 82 ms 29204 KB Output is correct
30 Correct 92 ms 29928 KB Output is correct
31 Correct 83 ms 28756 KB Output is correct
32 Incorrect 165 ms 25764 KB Output isn't correct
33 Halted 0 ms 0 KB -