QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#794831 | #9806. Growing Tree | ucup-team3802# | WA | 3ms | 3828kb | C++23 | 3.8kb | 2024-11-30 16:18:55 | 2024-11-30 16:18:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (ll i = (l); i < (r); ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pl = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
struct SCC {
using Graph = vector<vector<int>>;
Graph g, rg;
vector<vector<int>> scc;
vector<int> cmp;
Graph dag;
SCC(int n) : g(n), rg(n) {}
void addEdge(int u, int v){
g[u].push_back(v);
rg[v].push_back(u);
}
vector<bool> seen;
vector<int> vs,rvs;
void dfs(int v){
seen[v] = true;
for(auto e: g[v]) if(!seen[e]) dfs(e);
vs.push_back(v);
}
void rdfs(int v, int k){
seen[v] = true;
cmp[v] = k;
for(auto e: rg[v]) if(!seen[e]) rdfs(e,k);
rvs.push_back(v);
}
void build(){
int n = g.size();
seen.assign(n, false);
vs.clear();
for(int v = 0; v < n; v++) if(!seen[v]) dfs(v);
int k = 0;
scc.clear();
cmp.assign(n,-1);
seen.assign(n,false);
for(int i = n-1; i >= 0; i--){
if(!seen[vs[i]]){
rvs.clear();
rdfs(vs[i], k++);
scc.push_back(rvs);
}
}
set<pair<int,int>> newEdges;
int dv = scc.size();
dag.assign(dv, vector<int>(0));
newEdges.clear();
for(int i = 0; i < n; i++){
int u = cmp[i];
for(auto e: g[i]){
int v = cmp[e];
if(u == v) continue;
if(!newEdges.count({u,v})){
dag[u].push_back(v);
newEdges.insert({u,v});
}
}
}
}
};
ll p(ll x,ll m){
if(x>=0)return x%m;
else return (m-1)-(-x-1)%m;
}
bool dfs(vector<vector<pair<ll,ll>>> &z,vector<ll> &club,vector<ll> &kai,vector<bool> &rai,ll now){
rai[now]=true;
bool ans =false;
rep(i,0,z[now].size()){
if(club[now]==club[z[now][i].first]){
if(!rai[z[now][i].first]){
kai[z[now][i].first]=kai[now]+z[now][i].second;
rai[z[now][i].first]=true;
if((dfs(z,club,kai,rai,z[now][i].first)))ans=true;
}
else{
if(kai[now]+z[now][i].second!=kai[z[now][i].first])ans=true;
}
}
}
return ans;
}
int ans(vector<int> z,ll dep,ll n,ll yoyu){
vector<ll> f;
ll gr=(1<<(n-(dep)-1));
ll sg=(1<<(dep+1));
rep(i,0,gr){
unordered_set<int> ss;
rep(j,0,sg){
if(ss.count(z[i*sg+j])){
f.push_back(i);
break;
}
if(z[i*sg+j]!=-1)ss.insert(z[i*sg+j]);
}
}
if(n==dep+1){
if(f.size()==1)return 1;
else return 0;
}
if(yoyu<f.size())return -1;
int zans=10000;
rep(i,0,(1<<(int)(f.size()))){
vector<int> p=z;
rep(j,0,(f.size())){
if(i&(1<<j)){
rep(k,0,sg/2){
p[f[j]*sg+k]=-1;
}
}
else{
rep(k,0,sg/2){
p[f[j]*sg+sg/2+k]=-1;
}
}
}
int t=ans(z,dep+1,n,yoyu-f.size()+1);
if(t!=-1)zans=min(t,zans);
}
if(zans>1000)return -1;
else return zans+f.size();
}
int main(){
ll t;
cin>>t;
rep(ia,0,t){
ll n;
cin>>n;
ll m=(1<<n);
vector<int> z(m);
ll p=m;
rep(i,0,n){
p/=2;
rep(j,0,(1<<(i+1))){
ll e;
cin>>e;
rep(k,0,p){
z[p*j+k]+=e;
}
}
}
cout<<ans(z,0, n,1)<<endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3828kb
input:
3 2 1 2 4 3 2 1 2 1 2 3 3 2 1 2 1 2 3 3 1 1
output:
1 2 -1
result:
ok 3 number(s): "1 2 -1"
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 3584kb
input:
94 5 44 65 38 61 64 94 71 53 65 10 24 36 98 74 11 4 5 46 72 34 9 24 37 32 76 29 48 88 17 14 36 4 22 6 71 53 24 61 89 79 39 57 99 61 27 85 99 46 81 75 90 25 16 13 1 87 55 81 56 78 67 2 3 83 3 74 14 45 17 22 41 62 74 25 1 56 22 7 21 73 83 99 3 91 16 53 8 10 49 29 54 81 45 10 12 68 32 9 30 11 99 85 73 ...
output:
4 0 -1 5 0 1 -1 0 0 6 0 0 0 4 2 1 0 -1 0 2 0 -1 0 -1 0 0 -1 -1 -1 -1 -1 6 -1 0 -1 2 -1 -1 -1 -1 2 2 -1 0 0 3 -1 2 -1 0 -1 3 -1 0 0 0 -1 1 -1 -1 0 0 1 3 -1 0 1 4 0 -1 0 0 2 2 -1 0 -1 0 0 0 -1 4 -1 1 -1 0 0 0 0 1 0 -1 6 4
result:
wrong answer 1st numbers differ - expected: '2', found: '4'