编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#6133 #1014. 波奇农场 Accepted 100 173 ms 12160 K C++ 17 / 661 B C192022211986 2023-10-12 22:01:12
显示原始代码
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<vector<int>> b(n + 1);
    vector<bool> ok(n, 0);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        b[p].push_back(a[i]), b[a[i]].push_back(p);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (!ok[a[i]]) {
            int cnt = 0;
            queue<int> q;
            q.push(a[i]);
            ok[a[i]] = 1;
            while ((int)q.size()) {
                int hz = q.front();
                q.pop();
                cnt++;
                for (auto x : b[hz]) {
                    if (!ok[x])
                        q.push(x);
                    ok[x] = 1;
                }
            }
            ans += (cnt - 1) * 2;
        }
    }
    cout << ans << endl;
}
子任务 #1
Accepted
得分:100
测试点 #1
Accepted
得分:100
用时:5 ms
内存:304 KiB

输入文件(1.in

5
2 4 1 5 3
3 1 2 4 5

答案文件(1.out

8

用户输出

8

系统信息

Exited with return code 0
测试点 #2
Accepted
得分:100
用时:11 ms
内存:936 KiB

输入文件(2.in

10000
8410 2851 4799 7461 2256 1672 1961 330 2341 7908 7122 4283 4603 3214 520 2 6385 2691 137 1795
<97695 bytes omitted>

答案文件(2.out

19974

用户输出

19974

系统信息

Exited with return code 0
测试点 #3
Accepted
得分:100
用时:157 ms
内存:12160 KiB

输入文件(3.in

199999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<2577686 bytes omitted>

答案文件(3.out

199998

用户输出

199998

系统信息

Exited with return code 0