QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#739694#9732. Gathering MushroomstriccsrRE 40ms282788kbC++173.8kb2024-11-12 22:42:452024-11-12 22:42:50

Judging History

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

  • [2024-11-12 22:42:50]
  • 评测
  • 测评结果:RE
  • 用时:40ms
  • 内存:282788kb
  • [2024-11-12 22:42:45]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
const int N=2e5+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;
        return posOnLoop[type][(k-cntPath)-cr*cntLoop-1]<posOnLoop[m.type][(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){
    posOnPath[t[now]].push_back(d);
    M oldValue=mushrooms[t[now]];
    M oldMin=pathMin;
    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> mushrooms(n+1,M(inf,0,0,0));
    for(int i=1;i<=n;++i)mushrooms[i].type=i;
    for(int l=0;l<lTail;++l){
        vector<int> usedT;
        M mn=M(inf,0,0,-1);
        for(int u:loop[l]){
            usedT.push_back(t[u]);
            mushrooms[t[u]].cntLoop+=1;
            mn=min(mn,mushrooms[t[u]]);
        }
        for(int i=0;i<(int)loop[l].size();++i){
            posOnLoop[t[loop[l][i]]].push_back(i);
        }
        for(int i=0;i<(int)loop[l].size();++i){
            posOnLoop[t[loop[l][i]]].pop_front();
            posOnLoop[t[loop[l][i]]].push_back(i+(int)loop[l].size());
            
            dfs(loop[l][i],1,mushrooms,mn);
        }
        for(int type:usedT){
            posOnLoop[type].clear();
            mushrooms[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(){
    // 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: 40ms
memory: 282788kb

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
Runtime Error

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
2424
2520
220
228
909
9
3486
1
796
81
340
272
600
2350
32
495
40
128
140
665
1635
702
68
96
90
288
29
588
16
234
445
2748
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
556
40
17
21...

result: