Language: C++

#include <bits/stdc++.h>

using namespace std;

vector<long long> adj[5005];

bool visit[5005];

void dfs(long long src,vector<long long> &nodes) {

    visit[src]=true;

    nodes.push_back(src);

    for(long long x:adj[src]) {

        if(!visit[x]) {

            dfs(x,nodes);

        }

    }

}

long long ConnectedComponent(long long n,vector<long long> &cost,long long kk) {

    long long res=LONG_MIN;

    for(long long i=1; i<=n; i++) {

        memset(visit,0,sizeof visit);

        vector<long long> nodes;

        dfs(i,nodes);

        long long k=kk;

        long long cur=0;

        long long temp=LONG_MIN;

        if(k>nodes.size()) {

            long long base=0;

            for(long long j = 0 ; j < nodes.size() ; j++) {

                base+=(cost[nodes[j]-1]);

            }

            temp=max(temp,base);

            if(base>0) {

                long long mul=(k/nodes.size());

                base=base*(mul);

                k%=nodes.size();

            } else {

                k%=nodes.size();

            }

            for(long long j=0; j<k; j++) {

                cur+=cost[nodes[j%nodes.size()]-1];

                temp=max(temp,base+cur);

            }

            res=max(res,temp);

        } else {

            for(long long j=0; j<k; j++) {

                cur+=cost[nodes[j%nodes.size()]-1];

                temp=max(temp,cur);

            }

            res=max(res,temp);

        }

    }

    return res;

}

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    long long n;

    cin >> n;

    long long k;

    cin >> k;

    vector<long long> a(n);

    for(long long i = 0 ; i < n ; i++) {

        cin >> a[i];

    }

    vector<long long> b(n);

    for(long long i = 0 ; i < n ; i++) {

        cin >> b[i];

    }

    for(long long i = 0 ; i < n ; i++) {

        long long u=i+1;

        long long v=a[i];

        adj[u].push_back(v);

    }

    cout << ConnectedComponent(n,b,k) << '\n';

    return 0;

}



Compiler output:
a.cc: In function 'void dfs(long long int, std::vector<long long int>&)':
a.cc:11:5: error: reference to 'visit' is ambiguous
   11 |     visit[src]=true;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:7:6: note:                 'bool visit [5005]'
    7 | bool visit[5005];
      |      ^~~~~
a.cc:17:13: error: reference to 'visit' is ambiguous
   17 |         if(!visit[x]) {
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:7:6: note:                 'bool visit [5005]'
    7 | bool visit[5005];
      |      ^~~~~
a.cc: In function 'long long int ConnectedComponent(long long int, std::vector<long long int>&, long long int)':
a.cc:33:16: error: reference to 'visit' is ambiguous
   33 |         memset(visit,0,sizeof visit);
      |                ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:7:6: note:                 'bool visit [5005]'
    7 | bool visit[5005];
      |      ^~~~~
a.cc:33:31: error: reference to 'visit' is ambiguous
   33 |         memset(visit,0,sizeof visit);
      |                               ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:7:6: note:                 'bool visit [5005]'
    7 | bool visit[5005];
      |      ^~~~~