QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397392 | #6638. Treelection | USP_USP_USP# | WA | 127ms | 32688kb | C++14 | 1.9kb | 2024-04-24 01:13:22 | 2024-04-24 01:13:22 |
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;
}
void solve(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
g[i].clear();
}
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;
}
}
for(int i = l; i < n; i++){
int id = vs[i].se;
ans[id] = '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: 27580kb
input:
2 4 1 2 3 5 1 1 2 2
output:
1100 10000
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 127ms
memory: 32688kb
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:
wrong answer 1st lines differ - expected: '111111111111111111111111111111...0000000000000000000000000000000', found: '111111111111111111111111111111...0000000000000000000000000000000'