답안 #770143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770143 2023-06-30T19:59:58 Z vjudge1 Paths (RMI21_paths) C++17
20 / 100
135 ms 12008 KB
#include<bits/stdc++.h>
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<int, int>, null_type, less<pair<int, int>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;*/
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd  pair<double,double>
#define vdd  vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////

void usaco_problem(){
    freopen("hopscotch.in", "r", stdin);
    freopen("hopscotch.out", "w", stdout);
}

void init(){
    #ifndef ONLINE_JUDGE
 
freopen("input.txt", "r", stdin);
 
freopen("output.txt", "w", stdout);
 
#endif // ONLINE_JUDGE
}

const int mx = 2e5+5;
const int LOG = 25;
const int inf = 1e9+5;
const ll mod = 1e9+7;

vii tree[mx];
 
bool vis[mx];

ll dis1[mx], dis2[mx];

int bfs1(int x){
    memset(vis, 0, sizeof vis);
    priority_queue <pll, vector<pll>, greater<pll>> q;
    q.push({0, x});
    int ans = 0;
    while(!q.empty()){
        ll node = q.top().ss, dis = q.top().ff;
        q.pop();
        ans = node;
        if(vis[node])continue;
        vis[node] = 1;
        for(pii adj : tree[node]){
            if(!vis[adj.ff]){
                q.push({dis + adj.ss, adj.ff});
            }
        }
    }
    return ans;
}

void bfs(int x){
    memset(vis, 0, sizeof vis);
    queue<int> q;
    dis1[x] = 0;
    q.push(x);
    int ans = 0;
    while(!q.empty()){
        ll node = q.front();
        q.pop();
        ans = node;
        if(vis[node])continue;
        vis[node] = 1;
        for(pii adj : tree[node]){
            if(!vis[adj.ff]){
                dis1[adj.ff] = dis1[node] + adj.ss;
                q.push(adj.ff);
            }
        }
    }
}

void bfs2(int x){
    memset(vis, 0, sizeof vis);
    queue<int> q;
    dis2[x] = 0;
    q.push(x);
    int ans = 0;
    while(!q.empty()){
        ll node = q.front();
        q.pop();
        ans = node;
        if(vis[node])continue;
        vis[node] = 1;
        for(pii adj : tree[node]){
            if(!vis[adj.ff]){
                dis2[adj.ff] = dis2[node] + adj.ss;
                q.push(adj.ff);
            }
        }
    }
}

ll ans = 0, cur = 0;

bool done[mx];

bool dfs(int node, int p){
    bool o = 0;
    if(done[node]){
        ans += cur;
        cur = 0;
        o = 1;
    }
    for(pii adj : tree[node]){
        if(adj.ff == p)continue;
        cur += adj.ss;
        bool k = dfs(adj.ff, node);
        if(k){
            o = 1;
        }
        else cur -= adj.ss;
    }
    return o;
}

void run_case(){
   int n, k;cin >> n >> k;
    for(int i = 0; i < n-1;i++){
        int x, y, w;cin >> x >> y >> w;
        tree[x].pb({y, w});
        tree[y].pb({x, w});
    }
    if(k == 1){
        int a = bfs1(1);
        int b = bfs1(a);
        //cout << a << " " << b << endl;
        bfs(a);
        bfs2(b);
        for(int i = 1; i <= n;i++){
            cout << max(dis1[i], dis2[i]) << endl;
        }
        return;
    }
    for(int i = 1; i <= n;i++){
        ll res = 0;
        for(int mask = 0; mask < (1 << n);mask++){
            int cnt = 0;
            for(int j = 0;j < n;j++){
                if((1 << j) & mask){
                    cnt ++;
                }
            }
            if(cnt != k)continue;
            for(int j = 0;j < n;j++){
                if((1 << j) & mask){
                    done[j+1] = 1;
                }
            }
                ans = 0, cur = 0;
                dfs(i, -1);
                res = max(res, ans);
            for(int j = 0;j < n;j++){
                if((1 << j) & mask){
                    done[j+1] = 0;
                    cnt ++;
                }
            }            
        }
        cout << res << endl;
    }
}

int main(){
    speed;
    int t;
    //cin >> t;
    t = 1;
    while(t--){
        run_case();
    }
}

/*
    NEVER GIVE UP!
    DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
    Your Guide when stuck:
    - Continue keyword only after reading the whole input
    - Don't use memset with testcases
    - Check for corner cases(n=1, n=0)
    - Check where you declare n(Be careful of declaring it globally and in main)
*/

Compilation message

Main.cpp: In function 'void bfs(int)':
Main.cpp:83:9: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
   83 |     int ans = 0;
      |         ^~~
Main.cpp: In function 'void bfs2(int)':
Main.cpp:104:9: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
  104 |     int ans = 0;
      |         ^~~
Main.cpp: In function 'void usaco_problem()':
Main.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen("hopscotch.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |     freopen("hopscotch.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void init()':
Main.cpp:40:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:42:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 | freopen("output.txt", "w", stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 4948 KB Output is correct
2 Correct 70 ms 5004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 4948 KB Output is correct
2 Correct 70 ms 5004 KB Output is correct
3 Incorrect 10 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 4948 KB Output is correct
2 Correct 70 ms 5004 KB Output is correct
3 Incorrect 10 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 4948 KB Output is correct
2 Correct 70 ms 5004 KB Output is correct
3 Incorrect 10 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 11800 KB Output is correct
2 Correct 54 ms 11880 KB Output is correct
3 Correct 58 ms 11444 KB Output is correct
4 Correct 66 ms 11752 KB Output is correct
5 Correct 57 ms 12008 KB Output is correct
6 Correct 64 ms 11744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 4948 KB Output is correct
2 Correct 70 ms 5004 KB Output is correct
3 Incorrect 10 ms 4948 KB Output isn't correct
4 Halted 0 ms 0 KB -