Skip to content

Instantly share code, notes, and snippets.

@SamZhangQingChuan
Created August 16, 2020 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamZhangQingChuan/222ea1ee47b28524dc2e7bd29e3ddc8e to your computer and use it in GitHub Desktop.
Save SamZhangQingChuan/222ea1ee47b28524dc2e7bd29e3ddc8e to your computer and use it in GitHub Desktop.
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
//#pragma GCC optimize(3)
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC target("sse3","sse2","sse")
//#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
//#pragma GCC target("f16c")
//#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
//#pragma GCC diagnostic error "-fwhole-program"
//#pragma GCC diagnostic error "-fcse-skip-blocks"
//#pragma GCC diagnostic error "-funsafe-loop-optimizations"
//#pragma GCC diagnostic error "-std=c++14"
#include "bits/stdc++.h"
#include "ext/pb_ds/tree_policy.hpp"
#include "ext/pb_ds/assoc_container.hpp"
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define REP(x, l, u) for(ll x = l;x<u;x++)
#define RREP(x, l, u) for(ll x = l;x>=u;x--)
#define complete_unique(a) a.erase(unique(begin(a),end(a)),end(a))
#define mst(x, a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define lowbit(x) ((x)&(-(x)))
#define bitcnt(x) (__builtin_popcountll(x))
#define lson (ind<<1)
#define rson (ind<<1|1)
#define se second
#define fi first
#define sz(x) ((int)x.size())
#define EX0 exit(0);
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace __gnu_pbds; //required
using namespace std;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef vector<ll> VLL;
typedef vector<int> VI;
const ll mod = 1e9 + 7;
string to_string (string s) { return '"' + s + '"'; }
string to_string (const char *s) { return to_string ((string) s); }
string to_string (bool b) { return (b ? "true" : "false"); }
template<typename A, typename B>
string to_string (pair<A, B> p) { return "(" + to_string (p.first) + ", " + to_string (p.second) + ")"; }
template<typename A>
string to_string (A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) { res += ", "; }
first = false;
res += to_string (x);
}
res += "}";
return res;
}
void debug_out () { cerr<<endl; }
template<typename Head, typename... Tail>
void debug_out (Head H, Tail... T) {
cerr<<" "<<to_string (H);
debug_out (T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) {}
#endif
template<typename T, typename S>
inline bool upmin (T &a, const S &b) { return a > b ? a = b, 1 : 0; }
template<typename T, typename S>
inline bool upmax (T &a, const S &b) { return a < b ? a = b, 1 : 0; }
ull twop (ll x) { return 1ULL<<x; }
ll MOD (ll a, ll m) {
a %= m;
if (a < 0)a += m;
return a;
}
ll inverse (ll a, ll m) {
a = MOD (a, m);
if (a <= 1)return a;
return MOD ((1 - inverse (m, a) * m) / a, m);
}
template<typename T>
T sqr (T x) { return x * x; }
ll gcd (ll a, ll b) {
a = abs (a), b = abs (b);
while (b != 0) {
a %= b;
swap (a, b);
}
return a;
}
ll fast (ll a, ll b, ll mod) {
a %= mod;
if (b < 0)a = inverse (a, mod), b = -b;
ll ans = 1;
while (b) {
if (b & 1)ans = ans * a % mod;
a = a * a % mod;
b /= 2;
}
return ans % mod;
}
namespace SOLVE {
const int N = 100010;
bool vis[N], in_stack[N];
int fa[N], n, q;
vector<int> s;
int cycle_id[N], cycle_pos[N], cycle_len[N];
int cnt = 1;
int up[N][18], dep[N];
bool calced[N];
void calc (int node) {
if (cycle_id[node] != 0)return;
else if (calced[node])return;
else {
calced[node] = true;
calc (fa[node]);
dep[node] = dep[fa[node]] + 1;
up[node][0] = fa[node];
for (int i = 1; i < 18; i++) {
up[node][i] = up[up[node][i - 1]][i - 1];
}
}
}
void dfs (int cur) {
if (vis[cur]) {
if (in_stack[cur]) {
int id = cnt++;
int pos = 0;
for (int i = s.size () - 1; i >= 0; i--) {
cycle_id[s[i]] = id;
cycle_pos[s[i]] = pos++;
if (s[i] == cur) {
break;
}
}
cycle_len[id] = pos;
}
} else {
vis[cur] = 1;
in_stack[cur] = 1;
s.push_back (cur);
dfs (fa[cur]);
in_stack[cur] = 0;
s.pop_back ();
}
}
void input () {
cin>>n>>q;
for (int i = 0; i < n; i++)cin>>fa[i];
}
int process (int start, int target) {
if (dep[target] > dep[start]) {
return -1;
}
int ans = dep[start] - dep[target];
for (int i = 17; i >= 0; i--) {
// 2^i
if ((1<<i) <= dep[start] - dep[target]) {
start = up[start][i];
}
}
// for (int i = 0; i < 18; i++) {
// if (((1<<i) & (dep[start] - dep[target])) != 0) {
// start = up[start][i];
// }
// }
if (start == target)return ans;
if (cycle_id[target] == 0) return -1;
if (cycle_id[start] != cycle_id[target])return -1;
int diff = cycle_pos[start] - cycle_pos[target];
if (diff < 0) diff += cycle_len[cycle_id[start]];
return ans + diff;
}
void main () {
input ();
for (int i = 0; i < n; i++) {
if (!vis[i]) {
dfs (i);
}
}
for (int i = 0; i < n; i++) {
calc (i);
}
while (q--) {
int start, target;
cin>>start>>target;
cout<<process (start, target)<<endl;
}
}
}
signed main () {
#ifdef LOCAL
fr("/Users/zhangqingchuan/Desktop/cp/cp/input.txt");
fw("/Users/zhangqingchuan/Desktop/cp/cp/output.txt");
#endif
ios::sync_with_stdio (false);
cin.tie (nullptr);
cout.tie (nullptr);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout<<"Case #"<<i<<": ";
SOLVE::main ();
}
// clock_t st = clock();
// while(clock() - st < 3.0 * CLOCKS_PER_SEC){
//
// }
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment