QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397395 | #6638. Treelection | USP_USP_USP# | WA | 633ms | 97976kb | C++14 | 2.8kb | 2024-04-24 01:56:23 | 2024-04-24 01:56:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define int long long
void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
using ll = long long;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 1e6+5;
const int MOD = 1e9+7; //998244353;
const int INF = 0x3f3f3f3f;
const ll INF64 = ll(4e18) + 5;
vector<int> g[MAXN];
int sz[MAXN];
void dfs(int u, int p){
sz[u] = 1;
for(auto v : g[u]) if(v != p){
dfs(v,u);
sz[u] += sz[v];
}
}
int rec(int u, int x, int lim){
if(lim < 0){
return INF;
}
if(u == x) return 1;
int sum = 0;
for(auto v : g[u]){
sum += rec(v,x,lim);
}
sum -= lim;
sum = max(sum,0ll);
sum += 1;
return sum;
}
int RET[MAXN];
int LIV[MAXN];
int rec2(int u, int lim){
int sum = 0;
for(auto v : g[u]){
sum += rec2(v,lim);
}
if(sum >= lim){
LIV[u] = sum-lim;
}else{
LIV[u] = 0;
}
sum = max(sum-lim,0ll);
sum += 1;
RET[u] = sum;
return RET[u];
}
int OK[MAXN];
void process(int u, int p, int tam){
if(u != 0){
LIV[u] = min(LIV[p],LIV[u]);
}
if(u == 0){
OK[u] = 1;
}else if(sz[u] == tam){
int V = RET[0];
if(V == 1){
OK[u] = 1;
}else{
int dif = V-1;
int muda = RET[u]-1;
int k = min(LIV[p],muda);
if(k >= dif){
OK[u] = 1;
}
}
}
for(auto v : g[u]){
process(v,u,tam);
}
}
void solve(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
g[i].clear();
}
for(int i = 0; i < n; i++){
OK[i] = 0;
}
for(int i = 1; i < n; i++){
int p;
cin >> p;
p--;
g[p].pb(i);
}
dfs(0,0);
vector<pair<int,int>> vs(n);
for(int i = 0; i <n; i++){
vs[i]= { sz[i],i};
}
sort(vs.begin(),vs.end());
int l = 0;
int r = n-1;
string ans(n,'0');
while(l < r){
int mid = (l+r)/2;
//I cannot have more than vs[mid].fi-2 since mid will get vs[mid].fi - 1
int ret = rec(0,vs[mid].se, vs[mid].fi-2);
if(ret == 1){
r = mid;
}else{
l = mid+1;
}
}
int SZ = vs[l].fi;
//todo mundo maior serve
for(int i = 0; i < n; i++){
if(sz[i] > SZ) ans[i] = '1';
}
ans[0] = '1';
vector<int> op = {SZ,SZ-1};
for(auto x : op){
if(x <= 2) continue;
rec2(0,x-2);
process(0,0,x);
}
for(int i = 0; i < n; i++){
if(OK[i]) ans[i] = '1';
}
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(false); cin.tie(NULL);
int t = 1;
cin >> t;
while(t--){
solve();
}
return 0;
}
/*
Makefile:
CXXFLAGS=-Wall -Wextra -Wshadow -g -pedantic -fsanitize=address,undefined -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUGPEDANTIC -std=gnu++17
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 33436kb
input:
2 4 1 2 3 5 1 1 2 2
output:
1100 10000
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 148ms
memory: 42196kb
input:
10 100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...
output:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 186ms
memory: 97976kb
input:
1 1000000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...
output:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
result:
ok single line: '111111111111111111111111111111...0000000000000000000000000000000'
Test #4:
score: 0
Accepted
time: 633ms
memory: 87476kb
input:
1 1000000 1 2 2 3 5 1 7 3 9 8 4 11 9 6 10 12 10 13 16 18 14 16 7 24 23 21 14 8 26 27 17 21 33 24 20 17 5 34 29 34 37 20 12 22 39 44 33 37 32 27 31 25 30 46 39 26 53 4 11 55 41 29 6 22 32 36 52 57 65 38 59 67 51 56 30 61 36 64 62 19 51 63 53 83 38 75 31 41 49 43 60 18 62 67 91 81 44 25 55 98 86 64 46...
output:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
result:
ok single line: '111111111111111111111111111111...0000000000000000000000000000000'
Test #5:
score: -100
Wrong Answer
time: 170ms
memory: 39000kb
input:
65764 13 1 1 1 1 1 1 1 1 2 10 10 11 11 1 1 2 2 2 4 4 4 5 10 14 1 2 2 3 3 3 4 4 5 6 10 12 13 14 1 2 2 3 3 3 4 5 5 6 6 7 8 13 1 2 2 3 3 4 5 5 5 6 8 12 13 1 1 1 2 2 3 5 7 7 7 9 9 12 1 1 1 2 2 3 5 7 7 9 10 14 1 1 2 2 3 4 5 6 7 7 8 12 12 14 1 2 3 4 4 5 5 5 7 7 7 10 13 14 1 1 1 1 1 2 2 2 3 10 10 10 11 13 ...
output:
1000000000000 11000000000 11101000010000 11100000000000 1110100000000 1010001000000 101000100000 11011001000000 11111010000000 10000000000000 1111010000000 111000100010000 111010001000 11000000000000 11100000000000 11001001000000 11000000000000 11111000000000 110011000000 1010000100000 1100111010000...
result:
wrong answer 228th lines differ - expected: '11100000000000', found: '11000000000000'