/*
Code by @marlov
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <utility>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <iterator>
#include <bitset>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> ii;
#define maxN 1000005
#define INF 1000000000
long long N;
bool visited[maxN];
vector<int> child;
long long root;
long long cg=-1;
long long td[maxN];
vector<vector<ll> > grps;
void dfs(long long n){
if(visited[n]) return;
visited[n]=true;
grps.back().push_back(n);
td[cg]+=abs(n-child[n]);
long long tr=n-root;
dfs(child[n]);
}
//need to fix min distance to do all things
int minimum_walk(vector<int> p, int s){
N=p.size();
child=p;
root=s;
for(long long i=0;i<N;i++){
if(!visited[i]){
cg++;
grps.push_back(vector<ll>());
dfs(i);
}
}
int result=0;
//vector<pi> dist;
for(int i=0;i<=cg;i++){
int mind=INF;
for(int a:grps[i]){
mind=min(a,mind);
}
result+=2*mind+td[i];
}
return result;
}
/*
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cout<<minimum_walk({0,2,3,1},0);
return 0;
}
*/
/* stuff you should look for
* long long overflow, array bounds
* special cases (n=1,n=0?)
* do smth instead of nothing and stay organized
*/
Compilation message
books.cpp: In function 'void dfs(long long int)':
books.cpp:43:12: warning: unused variable 'tr' [-Wunused-variable]
43 | long long tr=n-root;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
380 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '725916' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
3rd lines differ - on the 1st token, expected: '6', found: '10' |
3 |
Halted |
0 ms |
0 KB |
- |