QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#633976#9101. Zayin and BusSatonAC ✓180ms11496kbC++201.5kb2024-10-12 16:30:322024-10-12 16:30:37

Judging History

你现在查看的是最新测评结果

  • [2024-10-12 16:30:37]
  • 评测
  • 测评结果:AC
  • 用时:180ms
  • 内存:11496kb
  • [2024-10-12 16:30:32]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
#define read read()
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}


const int maxn = 1e5 + 10;

int n,f[maxn],dep[maxn];
vector<int>g[maxn];
struct node{
  int id,val;
}a[maxn];

bool cmp(node a,node b){
  return a.id+a.val<b.id+b.val;
}

void bfs(){
  memset(dep,0x3f,sizeof dep);
  queue<int>q;q.push(1);dep[1]=0;
  while(!q.empty()){
    int t=q.front();q.pop();
    for(auto tt:g[t]){
      if(dep[tt]>dep[t]+1){
        dep[tt]=dep[t]+1;
        q.push(tt);
      }
    }
  }
}

int main() {
    int _=read;
    while(_--){
      n=read;
      rep(i,1,n) g[i].clear();
      rep(i,1,n-1){
        f[i]=read;
      g[f[i]].push_back(i+1);
      g[i+1].push_back(f[i]);       
      }
      rep(i,1,n) a[i].val=read,a[i].id=i;
      bfs();  
      sort(a+1,a+1+n,cmp);
      sort(dep+1,dep+1+n);reverse(dep+1,dep+1+n);
      int ans=0;
      rep(i,1,n){
        ans=max(ans,dep[i]+a[i].id+a[i].val);
      }
      cout<<ans<<endl;
    }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4008kb

input:

14
1
1
1
2
1
3
2
1
1 1
2
1
1 2
2
1
2 1
2
1
1 3
2
1
3 1
2
1
1 4
2
1
4 1
3
1 1
1 1 1
3
1 2
1 1 1
3
1 1
1 3 2
3
1 2
1 3 2

output:

2
3
4
3
4
4
5
4
6
5
4
4
6
6

result:

ok 14 lines

Test #2:

score: 0
Accepted
time: 180ms
memory: 11496kb

input:

15
1000
1 2 2 1 1 1 4 8 1 7 7 7 9 3 4 7 15 18 18 4 6 11 19 7 6 1 9 13 2 21 28 6 17 24 24 2 28 5 32 24 23 8 3 26 15 28 25 34 46 41 33 16 46 11 7 2 13 53 12 59 52 53 51 52 31 41 63 18 55 49 55 62 15 19 23 67 18 37 2 4 23 75 58 55 14 84 20 3 7 89 82 15 53 77 60 25 97 69 5 40 54 24 72 10 87 90 99 43 71 ...

output:

98572828
100088663
99870474
100076153
99995412
100076982
99971239
100079684
99928633
100093408
99432584
100093568
99620300
100058966
99565256

result:

ok 15 lines