赞
踩
PTA | 程序设计类实验辅助教学平台 (pintia.cn)
- #include <iostream>
- using namespace std;
- int fa[100005] = { 0 };
- int vis[100005] = { 0 };
- int count = 0, s = 0;
- int fun(int a) {
- if (fa[a] == -1 || vis[a]) return vis[a];
- ::count++;
- return vis[a] = 1 + fun(fa[a]);
- }
- int main() {
- int n, m;
- cin >> n >> m;
- for (int i = 1; i <= n; i++) cin >> fa[i];
- int a;
- while (m--) {
- cin >> a;
- s = max(s, fun(a));
- cout << 2 * ::count - s << endl;
- }
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。