180192025-09-25 11:43:29ubormaciK-léptű ősökcpp17Futási hiba 41/100787ms131072 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;
 
#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
//#pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")
 
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
 
/*
 
notes:
 
int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string
 
vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));
 
{if statement} ? {truth value} : {false value}
 
set lower bound/upper bound:
 	// . . . m1 . . . d . . . . m2
    auto m1_it = b.lower_bound(d);
    advance(m1_it, -1);
    m1 = *m1_it;
	m2 = *b.upper_bound(d);
 
#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
 
constexpr auto lcm(auto x, auto... xs)
{
	return ((x = std::lcm(x, xs)), ...);
}
 
std::gcd(int a, int b)
 
cout << setprecision(n);
 
set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);
 
*/
 
typedef int32_t ll;
 
void solve() {
 
    // binary lifting
 
    ll n, k;
    cin >> n >> k;
 
    vector<vector<ll>> v(n+1);
 
    ll lim = log2(n) + 1;
 
    //cerr << "\nlim=" << lim;
 
    vector<ll> twopows(lim+1, 0);
    twopows[0] = 1;
    for(ll i = 1; i <= lim; i++) {
        twopows[i] = twopows[i-1] * 2;
    }
 
    //cerr << "\ntwopows=" << twopows;
 
    vector<vector<ll>> p(n+1, vector<ll>(lim+1, 0));
 
    // root the tree in 0
 
    for(ll i = 0; i < n - 1; i++) {
        ll a, b;
        cin >> a >> b;
        a++;
        b++;
        v[a].push_back(b);
        v[b].push_back(a);
    }
 
    vector<ll> order(n, 0);
    ll orderctr = 0;
    queue<ll> qu;
    qu.push(1);
    while(!qu.empty()) {
        ll curr = qu.front();
        qu.pop();
 
        order[orderctr] = curr;
        orderctr++;
 
        for(const auto & child : v[curr]) {
            if(child != p[curr][0]) {
                p[child][0] = curr;
                qu.push(child);
            }
        }
    }
 
    //cerr << "\norder=" << order;
 
    for(ll ord = 0; ord < n; ord++) {
        ll curr = order[ord];
 
        //cerr << "\ncurr=" << curr;

        //ll tp = 1;
        ll ph = 0;
        while(ph <= lim - 1) {
 
            //cerr << "\nph=" << ph;
            //cerr << "\np[" << curr << "][" << ph << "]=" << p[curr][ph];
            p[curr][ph+1] = p[p[curr][ph]][ph];
            ph++;
            //tp *= 2;
        }
    }
 
    for(ll qi = 1; qi <= n; qi++) {
        
        ll node = qi;

        ll kc = k;
 
        while(kc > 0) {
 
            ll move = log2(kc);
            node = p[node][move];
            kc -= twopows[move];
        }
 
        if(node == 0) {
            cout << "-1 ";
        }else{
            cout << node - 1 << " ";
        }

    }
 
}
 
int main()
{
	std::ios_base::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);
 
	solve();
 
	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva1ms316 KiB
subtask225/25
2Elfogadva1ms316 KiB
3Elfogadva1ms428 KiB
4Elfogadva1ms316 KiB
5Elfogadva1ms316 KiB
6Elfogadva2ms568 KiB
7Elfogadva2ms572 KiB
8Elfogadva2ms564 KiB
9Elfogadva2ms564 KiB
10Elfogadva2ms564 KiB
11Elfogadva3ms564 KiB
subtask316/16
12Elfogadva54ms9212 KiB
13Elfogadva115ms16948 KiB
14Elfogadva114ms17900 KiB
15Elfogadva96ms15464 KiB
16Elfogadva128ms17716 KiB
17Elfogadva112ms17800 KiB
18Elfogadva114ms17844 KiB
subtask40/59
19Elfogadva151ms21300 KiB
20Elfogadva202ms28980 KiB
21Elfogadva317ms32948 KiB
22Elfogadva363ms38716 KiB
23Elfogadva296ms38708 KiB
24Elfogadva279ms38708 KiB
25Elfogadva787ms77372 KiB
26Elfogadva726ms77364 KiB
27Futási hiba367ms131072 KiB
28Futási hiba377ms131072 KiB
29Futási hiba128ms131072 KiB
30Futási hiba126ms131072 KiB
31Futási hiba128ms131072 KiB
32Futási hiba127ms131072 KiB
33Futási hiba149ms131072 KiB