提出 #52221621
ソースコード 拡げる
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii tuple<int, int, int>
#define pll pair<long long, long long>
#define L(n) (n << 1)
#define R(n) (n << 1 | 1)
#define print_vector(n, delimiter) for(auto a0 : n) cout << a0 << delimiter; cout << endl;
#define vector_sort(n) sort(n.begin(), n.end())
#define print_array(n, l, r, delimiter) for(int a0 = l; a0 <= r; a0++) cout << n[a0] << delimiter; cout << '\n';
#define REP(i, l, r) for (int i = l; i <= r; i++)
#define VREP(i, l, r) for (int i = l; i >= r; i--)
#define MIN(a, b) (a < b ? a : b)
#define MAX(a, b) (a > b ? a : b)
#define ABS(a) ((a) > 0 ? (a) : -(a))
#define LOG2(n) (31 - __builtin_clz(n))
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
template<class T>
istream & operator >> (istream &in, pair<T, T> &p) {
in >> p.first >> p.second;
return in;
}
template<class T>
ostream & operator <<(ostream &out, pair<T, T> &p) {
out << p.first << ' ' << p.second;
return out;
}
template<class Tuple, std::size_t N>
struct TuplePrinter {
static void print(ostream &out, const Tuple& t) {
TuplePrinter<Tuple, N-1>::print(out, t);
out << ' ' << get<N-1>(t);
}
};
template<class Tuple>
struct TuplePrinter<Tuple, 1> {
static void print(ostream &out, const Tuple& t) {
out << get<0>(t);
}
};
template<class... Args>
ostream & operator <<(ostream &out, const tuple<Args...> &p) {
TuplePrinter<decltype(p), sizeof...(Args)>::print(out, p);
return out;
}
const int MAX_N = 2e5;
int N;
long long K;
vector<int> adj[MAX_N + 5];
vector<int> adjTree[MAX_N + 5];
long long mxAns = 0, mnAns = 0;
long long subSz[MAX_N + 5];
long long depth[MAX_N + 5];
void DFS0(int u, int p) {
subSz[u] = 1;
depth[u] = depth[p] + 1;
for (int v : adj[u]) {
if (v == p) continue;
adjTree[u].emplace_back(v);
DFS0(v, u);
subSz[u] += subSz[v];
}
mnAns = max(mnAns, subSz[u]);
mxAns += subSz[u];
}
bool used[MAX_N + 5];
int ans[MAX_N + 5];
int p_ans;
int32_t main() {
ios_base::sync_with_stdio(0);
cin >> N >> K;
for (int i = 1; i < N; i++) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
DFS0(1, 0);
if (K < mnAns || K > mxAns) {
cout << "No\n";
} else {
vector<int> sortedSubSz(N);
iota(sortedSubSz.begin(), sortedSubSz.end(), 1);
sort(sortedSubSz.begin(), sortedSubSz.end(), [&](const int lhs, const int rhs) {
return subSz[lhs] > subSz[rhs];
});
long long k = K;
vector<int> notUseds;
vector<int> useds;
for (int u : sortedSubSz) {
if (subSz[u] <= k) {
k -= subSz[u];
// used[u] = true;
useds.emplace_back(u);
} else {
notUseds.emplace_back(u);
}
}
// print_vector(useds, ' ');
sort(notUseds.begin(), notUseds.end(), [&](const int lhs, const int rhs) {
return depth[lhs] > depth[rhs];
});
for (int u : notUseds) {
ans[u] = ++p_ans;
}
sort(useds.begin(), useds.end(), [&](const int lhs, const int rhs) {
return depth[lhs] < depth[rhs];
});
for (int u : useds) {
ans[u] = ++p_ans;
}
cout << "Yes\n";
print_array(ans, 1, N, ' ');
}
}
提出情報
提出日時 |
|
問題 |
D - LIS on Tree 2 |
ユーザ |
Noob_king |
言語 |
C++ 20 (gcc 12.2) |
得点 |
700 |
コード長 |
3380 Byte |
結果 |
AC |
実行時間 |
172 ms |
メモリ |
52796 KiB |
ジャッジ結果
セット名 |
Sample |
All |
得点 / 配点 |
0 / 0 |
700 / 700 |
結果 |
|
|
セット名 |
テストケース |
Sample |
00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt |
All |
00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 02_hack_01.txt, 02_hack_02.txt, 02_hack_03.txt, 02_hack_04.txt, 02_hack_05.txt, 02_hack_06.txt, 02_hack_07.txt, 02_hack_08.txt, 02_hack_09.txt, 02_hack_10.txt |
ケース名 |
結果 |
実行時間 |
メモリ |
00_sample_01.txt |
AC |
7 ms |
3532 KiB |
00_sample_02.txt |
AC |
3 ms |
3540 KiB |
00_sample_03.txt |
AC |
3 ms |
3532 KiB |
01_test_01.txt |
AC |
118 ms |
25708 KiB |
01_test_02.txt |
AC |
118 ms |
25668 KiB |
01_test_03.txt |
AC |
106 ms |
50372 KiB |
01_test_04.txt |
AC |
77 ms |
21164 KiB |
01_test_05.txt |
AC |
162 ms |
36972 KiB |
01_test_06.txt |
AC |
136 ms |
28016 KiB |
01_test_07.txt |
AC |
121 ms |
52696 KiB |
01_test_08.txt |
AC |
123 ms |
52696 KiB |
01_test_09.txt |
AC |
44 ms |
15700 KiB |
01_test_10.txt |
AC |
93 ms |
22540 KiB |
01_test_11.txt |
AC |
84 ms |
20384 KiB |
01_test_12.txt |
AC |
37 ms |
14224 KiB |
01_test_13.txt |
AC |
76 ms |
21428 KiB |
01_test_14.txt |
AC |
76 ms |
19464 KiB |
01_test_15.txt |
AC |
61 ms |
17136 KiB |
01_test_16.txt |
AC |
8 ms |
5204 KiB |
01_test_17.txt |
AC |
65 ms |
17520 KiB |
01_test_18.txt |
AC |
101 ms |
23256 KiB |
01_test_19.txt |
AC |
4 ms |
4256 KiB |
01_test_20.txt |
AC |
33 ms |
10492 KiB |
01_test_21.txt |
AC |
3 ms |
3624 KiB |
01_test_22.txt |
AC |
3 ms |
3536 KiB |
01_test_23.txt |
AC |
3 ms |
3468 KiB |
01_test_24.txt |
AC |
3 ms |
3472 KiB |
01_test_25.txt |
AC |
74 ms |
21368 KiB |
01_test_26.txt |
AC |
74 ms |
21364 KiB |
01_test_27.txt |
AC |
75 ms |
21468 KiB |
01_test_28.txt |
AC |
72 ms |
21436 KiB |
02_hack_01.txt |
AC |
125 ms |
39224 KiB |
02_hack_02.txt |
AC |
127 ms |
39124 KiB |
02_hack_03.txt |
AC |
127 ms |
39140 KiB |
02_hack_04.txt |
AC |
171 ms |
52620 KiB |
02_hack_05.txt |
AC |
172 ms |
52596 KiB |
02_hack_06.txt |
AC |
170 ms |
52592 KiB |
02_hack_07.txt |
AC |
171 ms |
52624 KiB |
02_hack_08.txt |
AC |
169 ms |
52796 KiB |
02_hack_09.txt |
AC |
169 ms |
52584 KiB |
02_hack_10.txt |
AC |
170 ms |
52608 KiB |