QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#741516#9732. Gathering MushroomstriccsrWA 137ms563444kbC++174.7kb2024-11-13 14:32:182024-11-13 14:32:19

Judging History

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

  • [2024-11-13 14:32:19]
  • 评测
  • 测评结果:WA
  • 用时:137ms
  • 内存:563444kb
  • [2024-11-13 14:32:18]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
const int N=4e5+11,inf=0x3f3f3f3f;

int n,k,t[N],a[N],v[N];

vector<int> r[N];
deque<int> loop[N];
int lTail;
vector<int> posOnPath[N];
deque<int> posOnLoop[N];

bool onLoop[N];

struct M{
    int pathDep;
    int cntPath;
    int cntLoop;
    int type;
    bool operator <(const M &m)const{
        if(pathDep!=m.pathDep)return pathDep<m.pathDep;
        if(cntLoop==0)return false;
        if(m.cntLoop==0)return true;
        int cr=(k-cntPath-1)/cntLoop,mcr=(k-m.cntPath-1)/m.cntLoop;
        if(cr!=mcr)return cr<mcr;
        // if(k-cntPath-cr*cntLoop-1<0||k-cntPath-cr*cntLoop-1>(int)posOnLoop[type].size()){
        //     exit(0);
        // }
        // if(k-m.cntPath-mcr*m.cntLoop-1<0||k-m.cntPath-mcr*m.cntLoop-1>(int)posOnLoop[type].size()){
        //     exit(0);
        // }
        return posOnLoop[type].at((k-cntPath)-cr*cntLoop-1)<posOnLoop[m.type].at((k-m.cntPath)-mcr*m.cntLoop-1);
    }
    M()=delete;
    M(int pathDep,int cntPath,int cntLoop,int type):pathDep(pathDep),cntPath(cntPath),cntLoop(cntLoop),type(type){}
};


void dfs(int now,int d,vector<M> &mushrooms,M &pathMin){
    M oldValue=mushrooms[t[now]];
    M oldMin=pathMin;
    posOnPath[t[now]].push_back(d);
    if((int)posOnPath[t[now]].size()>=k){
        mushrooms[t[now]].pathDep=posOnPath[t[now]][(int)posOnPath[t[now]].size()-k];    
    }
    mushrooms[t[now]].cntPath=(int)posOnPath[t[now]].size();
    pathMin=min(pathMin,mushrooms[t[now]]);
    v[now]=pathMin.type;
    for(int to:r[now]){
        if(onLoop[to])continue;
        dfs(to,d+1,mushrooms,pathMin);
    }
    mushrooms[t[now]]=oldValue;
    pathMin=oldMin;
    posOnPath[t[now]].pop_back();
}
void work(){
    cin>>n>>k;
    for(int i=0;i<=n;++i){
        r[i].clear();
        loop[i].clear();
        posOnPath[i].clear();
        posOnLoop[i].clear();
        onLoop[i]=false;
        v[i]=0;
    }
    for(int i=1;i<=n;++i)cin>>t[i];
    for(int i=1;i<=n;++i){
        cin>>a[i];
        r[a[i]].push_back(i);
    }

    lTail=0;
    vector<bool> vis(n+1,false),inStack(n+1,false);
    vector<int> stk;
    for(int i=1;i<=n;++i){
        if(!vis[i]){
            int now=i;
            stk.clear();
            while(1){
                inStack[now]=true;
                vis[now]=true;
                stk.push_back(now);
                if(inStack[a[now]]){
                    while(!stk.empty()){
                        loop[lTail].push_front(stk.back());
                        onLoop[stk.back()]=true;
                        if(stk.back()==a[now])break;
                        inStack[stk.back()]=false;
                        stk.pop_back();
                    }
                    lTail+=1;
                    break;
                }
                if(vis[a[now]])break;
                now=a[now];
            }
            while(!stk.empty()){
                inStack[stk.back()]=false;
                stk.pop_back();
            }
        }
    }

    vector<M> info(n+1,M(inf,0,0,0));
    for(int i=1;i<=n;++i)info[i].type=i;

    for(int l=0;l<lTail;++l){
        vector<int> loopTypes;
        for(int i=0;i<(int)loop[l].size();++i){
            posOnLoop[t[loop[l][i]]].push_back(i);
            loopTypes.push_back(t[loop[l][i]]);
        }

        M pathMin=M(inf,0,0,0);
        for(int type:loopTypes){
            info[type].pathDep=inf;
            info[type].cntPath=0;
            info[type].cntLoop=(int)posOnLoop[type].size();
            pathMin=min(pathMin,info[type]);
        }

        for(int i=(int)loop[l].size()-1;i>=0;--i){
            dfs(loop[l][i],1,info,pathMin);
            posOnPath[t[loop[l][i]]].push_back(-i);
            if(posOnPath[t[loop[l][i]]].size()>=k){
                info[t[loop[l][i]]].pathDep=posOnPath[t[loop[l][i]]].at(posOnPath[t[loop[l][i]]].size()-k);
            }
            info[t[loop[l][i]]].cntPath=posOnPath[t[loop[l][i]]].size();
            pathMin=min(pathMin,info.at(t[loop[l][i]]));
        }
        
        // for(auto rit=loop[l].rbegin();rit!=loop[l].rend();++rit){
        //     info[t[*rit]].cntPath+=1;
        //     pathMin=min(pathMin,info[t[*rit]]);
        //     dfs(*rit,1,info,pathMin);
        // }

        for(int type:loopTypes){
            posOnLoop[type].clear();
            info[type]=M(inf,0,0,type);
        }

    }
    long long ans=0;
    for(int i=1;i<=n;++i){
        //cout<<v[i]<<" ";
        ans+=1ll*i*v[i];
    }
    cout<<ans<<"\n";
}
int main(){
    //freopen("g.in","r",stdin);
    // ios::sync_with_stdio(0);
    // cin.tie(0);
    int T;
    cin>>T;
    while(T--){
        work();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 71ms
memory: 563444kb

input:

3
5 3
2 2 1 3 3
2 5 1 2 4
5 4
2 2 1 3 3
2 5 1 2 4
3 10
1 2 3
1 3 2

output:

41
45
14

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 137ms
memory: 562580kb

input:

6000
19 48
18 19 18 19 11 9 15 19 12 18 11 18 9 18 9 18 19 11 15
12 14 18 8 1 3 19 5 13 14 15 2 14 5 19 2 19 12 9
15 23
3 1 1 3 6 1 4 1 1 6 6 4 12 4 6
14 1 8 8 6 6 12 14 6 8 5 7 14 2 5
9 140979583
4 5 8 9 2 7 6 8 2
8 9 4 6 9 2 4 7 8
4 976357580
2 3 1 3
2 1 1 4
6 508962809
4 3 4 3 4 4
4 5 4 5 5 6
13 ...

output:

3420
260
254
26
84
759
126
30
1092
1
2493
2422
168
360
298
324
2394
2520
220
228
1107
9
3486
1
796
81
223
272
600
3196
32
495
40
128
140
665
1635
702
68
96
90
288
29
594
16
234
445
2928
140
40
477
1197
19
1994
1082
32
522
672
20
390
32
2204
1938
42
21
885
4
1539
196
420
11
1709
801
720
1
458
40
17
2...

result:

wrong answer 17th lines differ - expected: '2424', found: '2394'