QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#343195 | #1652. One Piece | ngpin04 | WA | 103ms | 22916kb | C++20 | 3.9kb | 2024-03-02 04:23:03 | 2024-03-02 04:23:04 |
Judging History
answer
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define bit(n) (1LL << (n))
#define getbit(x, i) (((x) >> (i)) & 1)
#define pii pair<int, int>
#define ALL(x) x.begin(), x.end()
using namespace std;
const int M = 5e5 + 5;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e9;
const long long ooo = 1e18;
const double pi = acos(-1);
template<typename T1, typename T2> bool mini(T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
typedef long long ll;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("file.inp", "r",stdin);
#endif
int n; cin >> n;
vector <vector <int>> adj(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector <int> a(n);
for (int &x : a)
cin >> x;
vector <int> minval(n);
vector <int> h(n, 0), f(n, 0);
function <void(int, int)> dfs = [&](int u, int p) {
minval[u] = a[u] - h[u];
for (int v : adj[u]) if (v != p) {
h[v] = h[u] + 1;
dfs(v, u);
mini(minval[u], minval[v]);
}
};
h[0] = 0;
dfs(0, -1);
function <void(int, int, int)> dfs2 = [&](int u, int p, int cur) {
f[u] = min(cur - h[u], minval[u] + h[u]);
pair <int, int> pir = {a[u] - h[u], oo};
for (int v : adj[u]) if (v != p) {
if (mini(pir.se, minval[v])) {
if (pir.se < pir.fi)
swap(pir.fi, pir.se);
}
}
for (int v : adj[u]) if (v != p) {
int val = (minval[v] == pir.fi) ? pir.se : pir.fi;
dfs2(v, u, min(cur, val + 2 * h[u]));
}
};
dfs2(0, -1, oo);
// for (int x : f)
// cerr << x << " ";
int r = 0;
while (f[r] != 0)
r++;
h[r] = 0;
dfs(r, -1);
int mx = r;
for (int i = 0; i < n; i++) if (f[i] == 0) {
if (h[i] > h[mx])
mx = i;
}
// cerr << r << " " << mx << " " << h[mx] << "\n";
function <void(int, int, vector <int>&)> getnodes = [&](int u, int p, vector <int> &s) {
if (f[u] == 0) {
s.push_back(u);
return;
}
for (int v : adj[u]) if (v != p) {
getnodes(v, u, s);
}
};
vector<pair <int, int>> ord;
if (h[mx] % 2 == 0) {
if (mx == r) {
cout << r + 1 << " ";
} else {
for (int rep = 0; rep < h[mx] / 2; rep++) {
for (int i : adj[mx]) {
if (h[i] < h[mx]) {
mx = i;
break;
}
}
}
vector <int> s;
h[mx] = 0;
for (int i : adj[mx]) {
s.clear();
getnodes(i, mx, s);
for (int x : s) {
ord.push_back({s.size(), x});
}
}
}
} else {
int lef = 0, rig = 0;
for (int i = 0; i < n; i++) if (f[i] == 0) {
if (h[i] < h[mx])
lef++;
else
rig++;
}
for (int i = 0; i < n; i++) if (f[i] == 0) {
if (h[i] < h[mx])
ord.push_back({lef, i});
else
ord.push_back({rig, i});
}
}
sort(ALL(ord));
for (auto [sz, i] : ord) {
cout << i + 1 << " ";
}
for (int i = 0; i < n; i++) if (f[i] > 0) {
cout << i + 1 << " ";
}
for (int i = 0; i < n; i++) if (f[i] < 0) {
cout << i + 1 << " ";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
input:
5 1 2 1 3 2 4 2 5 2 2 3 3 3
output:
3 4 5 1 2
result:
ok single line: '3 4 5 1 2 '
Test #2:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
4 2 1 3 2 3 4 1 0 1 2
output:
2 1 3 4
result:
ok single line: '2 1 3 4 '
Test #3:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
1 0
output:
1
result:
ok single line: '1 '
Test #4:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
2 1 2 0 1
output:
1 2
result:
ok single line: '1 2 '
Test #5:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
3 2 3 1 2 2 1 1
output:
2 3 1
result:
ok single line: '2 3 1 '
Test #6:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
4 3 2 1 4 3 1 1 3 2 1
output:
1 4 2 3
result:
ok single line: '1 4 2 3 '
Test #7:
score: 0
Accepted
time: 0ms
memory: 3884kb
input:
4 4 3 4 2 1 2 3 2 2 1
output:
2 3 4 1
result:
ok single line: '2 3 4 1 '
Test #8:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
4 3 2 1 2 4 1 1 2 3 1
output:
1 4 2 3
result:
ok single line: '1 4 2 3 '
Test #9:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
4 3 1 2 3 2 4 3 1 2 0
output:
4 1 2 3
result:
ok single line: '4 1 2 3 '
Test #10:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
4 2 4 3 4 1 4 2 2 2 1
output:
1 2 3 4
result:
ok single line: '1 2 3 4 '
Test #11:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
5 4 5 2 3 2 1 5 3 4 3 2 1 1
output:
4 5 1 2 3
result:
ok single line: '4 5 1 2 3 '
Test #12:
score: 0
Accepted
time: 0ms
memory: 3644kb
input:
5 2 5 3 1 3 4 3 5 0 3 1 2 2
output:
1 2 3 4 5
result:
ok single line: '1 2 3 4 5 '
Test #13:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
5 2 3 2 5 1 3 1 4 1 3 2 1 4
output:
1 4 2 3 5
result:
ok single line: '1 4 2 3 5 '
Test #14:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
5 3 4 1 2 2 5 3 2 3 2 2 3 3
output:
4 1 5 2 3
result:
ok single line: '4 1 5 2 3 '
Test #15:
score: 0
Accepted
time: 0ms
memory: 3884kb
input:
5 5 1 3 2 5 4 5 3 3 3 2 3 2
output:
2 1 4 3 5
result:
ok single line: '2 1 4 3 5 '
Test #16:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
6 2 4 6 2 5 2 5 3 1 4 1 1 3 0 2 2
output:
4 1 2 3 5 6
result:
ok single line: '4 1 2 3 5 6 '
Test #17:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
6 3 5 5 6 4 2 2 1 6 1 3 4 3 5 2 2
output:
1 3 5 6 2 4
result:
ok single line: '1 3 5 6 2 4 '
Test #18:
score: 0
Accepted
time: 0ms
memory: 3760kb
input:
6 5 2 6 5 6 3 4 6 2 1 2 1 4 4 2 3
output:
1 5 2 3 4 6
result:
ok single line: '1 5 2 3 4 6 '
Test #19:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
6 1 6 5 6 1 4 2 4 3 5 2 4 2 3 1 1
output:
5 6 1 2 3 4
result:
ok single line: '5 6 1 2 3 4 '
Test #20:
score: 0
Accepted
time: 0ms
memory: 3880kb
input:
6 4 5 1 3 6 3 3 2 3 5 2 2 1 3 2 2
output:
1 2 5 6 3 4
result:
ok single line: '1 2 5 6 3 4 '
Test #21:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
7 7 6 7 5 4 1 3 2 4 7 2 7 3 3 4 2 3 3 2
output:
1 2 5 6 4 7 3
result:
ok single line: '1 2 5 6 4 7 3 '
Test #22:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
7 6 7 4 1 3 6 4 7 6 2 5 2 2 3 3 1 4 2 1
output:
4 7 1 2 3 5 6
result:
ok single line: '4 7 1 2 3 5 6 '
Test #23:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
7 7 5 4 7 6 7 1 2 3 7 7 2 2 1 2 2 2 2 1
output:
2 7 1 3 4 5 6
result:
ok single line: '2 7 1 3 4 5 6 '
Test #24:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
7 4 7 4 1 4 2 5 3 3 7 3 6 2 1 3 1 4 4 2
output:
2 4 1 3 5 6 7
result:
ok single line: '2 4 1 3 5 6 7 '
Test #25:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
8 3 4 1 4 1 6 7 4 8 5 4 5 6 2 2 4 2 1 2 3 2 3
output:
1 3 5 7 4 2 6 8
result:
ok single line: '1 3 5 7 4 2 6 8 '
Test #26:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
8 7 5 1 7 3 6 7 4 6 7 8 6 2 5 3 4 2 3 3 1 2 1
output:
6 8 1 2 3 4 5 7
result:
ok single line: '6 8 1 2 3 4 5 7 '
Test #27:
score: 0
Accepted
time: 0ms
memory: 3644kb
input:
8 5 7 1 7 8 7 6 5 3 7 4 5 2 5 3 2 3 1 1 2 2 3
output:
4 5 1 2 3 6 7 8
result:
ok single line: '4 5 1 2 3 6 7 8 '
Test #28:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
8 8 7 4 6 2 4 3 8 8 1 8 2 2 5 2 2 2 3 3 4 2 1
output:
1 2 3 7 8 4 5 6
result:
ok single line: '1 2 3 7 8 4 5 6 '
Test #29:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
8 3 2 6 1 3 5 3 7 8 2 4 6 6 2 3 2 3 3 4 2 4 3
output:
1 3 4 8 2 6 5 7
result:
ok single line: '1 3 4 8 2 6 5 7 '
Test #30:
score: 0
Accepted
time: 100ms
memory: 22916kb
input:
250000 27197 102813 107013 109797 16583 152577 164884 11838 214586 173971 248203 141910 7896 111843 80520 130209 6277 128026 55601 234265 95559 88739 152936 50808 194534 24226 159232 83111 103712 33855 142546 246859 40404 81467 125784 239727 24202 165110 57020 155999 133375 118407 156303 150104 1493...
output:
10 23 36 42 44 70 71 101 111 113 116 141 177 183 204 217 221 251 252 253 260 264 271 272 307 318 325 328 338 360 397 423 429 442 494 506 518 521 526 537 564 567 602 633 673 683 721 738 739 744 754 770 827 835 837 865 872 890 893 914 946 948 967 973 981 1003 1014 1063 1068 1083 1118 1133 1152 1166 11...
result:
ok single line: '10 23 36 42 44 70 71 101 111 1...50 249954 249976 249982 249986 '
Test #31:
score: -100
Wrong Answer
time: 103ms
memory: 22864kb
input:
250000 198658 122606 213093 143827 45640 230664 242707 66336 228202 70151 54424 136589 31088 11371 216202 169155 131901 199050 54921 227981 114533 38096 180601 164687 21723 12006 68933 157358 93093 147993 50664 240700 41575 183691 172663 45572 194543 35989 237102 202526 171096 138704 245144 179222 2...
output:
93537 121369 176012 29820 71115 88542 108448 9809 47151 121550 144977 236229 139839 161050 163150 195894 204395 233808 235650 5253 20588 24232 78832 123443 129706 186465 192098 231670 2226 7287 22455 39200 47180 73052 94922 108611 126826 140903 141050 146734 150350 208053 211364 223305 223657 225726...
result:
wrong answer 1st lines differ - expected: '246154 41241 48703 165441 1660...983 249992 249993 249994 249997', found: '93537 121369 176012 29820 7111...83 249992 249993 249994 249997 '