QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#547154#9101. Zayin and Buswzxtsl#TL 1ms7700kbC++171.8kb2024-09-04 18:43:112024-09-04 18:43:12

Judging History

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

  • [2024-09-04 18:43:12]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7700kb
  • [2024-09-04 18:43:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define For(i,j,k) for (int i=(j);i<=(k);i++)
#define rof(i,j,k) for (int i=(j);i>=(k);i--)
#define ull unsigned long long
#define lowbit(x) ((x)&(-(x)))
#define PII pair<int,int>
#define int long long
#define endl "\n"
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
const int mod = 998244353;
const int N=2e5+7;
int n,m;
int a[N];
int fa[N], mxdep[N], dis[N];
//带权并查集
int Find(int x) {
    if (x != fa[x]) {
        int t = fa[x];        // 将x的父亲临时保存
        fa[x] = Find(fa[x]);  // 这是x的父亲已经变为祖宗
        dis[x] += dis[t];  // x原父亲t的距离已经变为到祖宗的距离,x到原父亲距离+原父亲到
                  // 祖宗距离 = x到祖宗距离
    }
    return fa[x];
}
bool cmp(int a,int b){
    return a>=b;
}
void solve(){
    cin>>n;
    //需要去初始化
    for(int i=1;i<=n;i++){
        fa[i]=i;
        //mxdep[i]=0;
        dis[i]=0;
    }
    //开始
    for(int i=1;i<=n-1;i++){
        int u,v,w;
        cin>>u;
        v=i+1;
        int Fa=Find(u),Fb=Find(v); 
        dis[v]=dis[u]+1;
        mxdep[Fa]=max(mxdep[Fa],mxdep[Fb]+dis[v]);//因为要去较大的路径所以取max
        fa[v]=Fa;//路径压缩直接到达根节点而不经过父节点
        //cout<<mxdep[w]<<" ";
    }
    // for(int i=1;i<=n;i++){
    //     cout<<dis[i]<<"!";
    // }
    sort(dis+1,dis+1+n,cmp);
    For(i,1,n)
        cin>>a[i],a[i]=a[i]+i-1;
    sort(a+1,a+1+n);
    int maxn=0;
    For(i,1,n)
        maxn=max(a[i]+1+dis[i],maxn);
    cout<<maxn<<endl;
}
signed main(){
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
}

详细

Test #1:

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

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: -100
Time Limit Exceeded

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

result: