QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#741289#9732. Gathering MushroomstriccsrRE 87ms563280kbC++174.2kb2024-11-13 14:02:132024-11-13 14:02:15

Judging History

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

  • [2024-11-13 14:02:15]
  • 评测
  • 测评结果:RE
  • 用时:87ms
  • 内存:563280kb
  • [2024-11-13 14:02:13]
  • 提交

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(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;
}

详细

Test #1:

score: 100
Accepted
time: 87ms
memory: 563280kb

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
322
254
26
84
759
126
30
1092
1
2493
2422
168
360
298
324
2452
2520
220
228
1005
9
3486
1
796
69
340
272
600
3430
32
495
40
135
140
708
1635
702
68
96
90
288
29
588
16
234
445
2844
140
40
477
1197
19
1994
1164
32
522
672
20
390
28
2204
1938
42
21
885
4
1539
196
420
11
1589
801
720
1
458
40
17
2...

result: