QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#882315#9184. Team CodingI_love_Riley_Andersen#0 32ms24784kbC++173.5kb2025-02-04 23:47:442025-02-04 23:47:45

Judging History

This is the latest submission verdict.

  • [2025-02-04 23:47:45]
  • Judged
  • Verdict: 0
  • Time: 32ms
  • Memory: 24784kb
  • [2025-02-04 23:47:44]
  • Submitted

answer

#include"bits/stdc++.h"
#include"ext/pb_ds/tree_policy.hpp"
#include"ext/pb_ds/assoc_container.hpp"
using namespace std;
using namespace __gnu_pbds;
#define FOR(i,a,b)for(int i=a;i<b;i++)
#define F0R(i,a)FOR(i,0,a)
#define ROF(i,a,b)for(int i=b-1;a<=i;i--)
#define R0F(i,a)ROF(i,0,a)
#define rep(a)F0R(_,a)
#define each(a,x)for(auto&a:x)
#define all(a)begin(a),end(a)
#define sz(x)int(size(x))
#define lla(x)rbegin(x),rend(x)
#define SUM(a)accumulate(all(a),0ll)
#define SUMM(a,b)accumulate(a,a+b,0ll)
#define MAX(a)*max_element(all(a))
#define MAXX(a,b)*max_element(a,a+b)
#define MIN(a)*min_element(all(a))
#define MINN(a,b)*min_element(a,a+b)
#define con const int
#define lwb lower_bound
#define upb upper_bound
#define bry binary_search
#ifdef LOCAL
#define print(x)cout<<x;
#else
#define print(...)
#endif
#define debug(x)print(#x<<" = "<<x<<endl)
using ld=long double;
using ll=long long;
using str=string;
template<class T>using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class T>using omset=tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class T>T minn(T a){return a;}
template<class T,class...Args>T minn(T a,Args... args){return min(a,minn(args...));}
template<class T>T maxx(T a){return a;}
template<class T,class...Args>T maxx(T a,Args... args){return max(a,maxx(args...));}
#define SMIN(a,b...)a=min(a,minn(b))
#define SMAX(a,b...)a=max(a,maxx(b))
void fileset(str a){assert(nullptr!=freopen((a+".in").c_str(),"r",stdin)&&nullptr!=freopen((a+".out").c_str(),"w",stdout));}
con mxN = 4e5 + 5;

vector<int> aj[mxN];

int dep[mxN];
int cnt[mxN];
int l[mxN];

int N;

int findDepth(int u, int d){
    int ans = INT_MAX;

    if (l[u] ^ l[0]){
        ans = d, cnt[d] ++;
    }

    dep[u] = d;

    each (v, aj[u]){
        ans = min(ans, findDepth(v, d + 1));
    }

    return ans;
}

vector<int> subtree(int u){
    vector<int> ans;

    F0R (i, sz(ans)){
        if (ans[i] < N){
            if (0 < i && dep[ans[i]] > dep[ans[i - 1]]){
                ans.push_back(N + dep[i]);
            }

            each (v, aj[i]){
                ans.push_back(v);
            }
        }
    }

    reverse(all(ans));

    return ans;
}

int main(){
    int K;
    cin >> N >> K;

    int ans = 0;

    F0R (i, N){
        cin >> l[i];

        if (l[0] == l[i]){
            ans ++;
        }
    }

    FOR (i, 1, N){
        int B;
        cin >> B;

        aj[B].push_back(i);
    }

    // find possible roots' depth

    int depth = findDepth(0, 0);

    // find possible roots

    vector<int> roots;

    F0R (i, N){
        if (dep[i] == depth && l[i] ^ l[0]){
            roots.push_back(i);
        }
    }

    // reverse dfs order with additional nodes for each possible root

    multiset<pair<int,int>> mt;

    each (r, roots){
        auto loc = subtree(r);

        int ans = 0;
        int swp = 0;

        int cap = 0;
        int neg = 0;

        each (u, loc){
            if (u < N){
                if (l[u] == l[0]) cap ++;
                else neg ++;
            } else {
                int dpt = u - N;

                swp += min(cap, cnt[dpt] - neg);
                ans += min(cap + neg, cnt[dpt]);

                cap = neg = 0;
            }
        }

        mt.emplace(-ans, swp);
    }

    // result
    auto [loc, swp] = *begin(mt);

    ans = min(-ans, loc);

    cout << -ans << ' ' << swp << endl;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 12
Accepted
time: 1ms
memory: 14792kb

input:

1 1
0

output:

1 0

result:

ok single line: '1 0'

Test #2:

score: 12
Accepted
time: 0ms
memory: 16480kb

input:

5 2
0 1 0 0 0
0
1
2
3

output:

4 0

result:

ok single line: '4 0'

Test #3:

score: 0
Wrong Answer
time: 2ms
memory: 16280kb

input:

10 9
6 5 6 2 5 4 5 3 4 1
0
1
2
3
4
5
6
7
8

output:

2 0

result:

wrong answer 1st lines differ - expected: '3 0', found: '2 0'

Subtask #2:

score: 0
Wrong Answer

Test #17:

score: 19
Accepted
time: 0ms
memory: 16408kb

input:

1 1
0

output:

1 0

result:

ok single line: '1 0'

Test #18:

score: 19
Accepted
time: 1ms
memory: 16420kb

input:

5 2
0 1 0 0 0
0
1
2
3

output:

4 0

result:

ok single line: '4 0'

Test #19:

score: 19
Accepted
time: 3ms
memory: 17484kb

input:

100 2
0 1 0 0 1 0 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 1 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 1 1 0 0 0 1 1 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1
0
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...

output:

50 0

result:

ok single line: '50 0'

Test #20:

score: 19
Accepted
time: 3ms
memory: 17492kb

input:

2000 2
0 0 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 0 1 1 0 0 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 1 1 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0...

output:

1001 0

result:

ok single line: '1001 0'

Test #21:

score: 0
Wrong Answer
time: 32ms
memory: 24784kb

input:

100000 2
0 0 1 0 0 0 1 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 1 0 1 0 1 1 0 1 0 1 1 0 1 1 1 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 0 0 1 0 0 1 1 1 1 1 1 0 0 1 0 1 1 0 1 1 0 0 1 0 0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 1 1 0 1 1 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 0 1 0 1 0 0...

output:

49802 0

result:

wrong answer 1st lines differ - expected: '50198 0', found: '49802 0'

Subtask #3:

score: 0
Wrong Answer

Test #39:

score: 27
Accepted
time: 2ms
memory: 15400kb

input:

1 1
0

output:

1 0

result:

ok single line: '1 0'

Test #40:

score: 27
Accepted
time: 1ms
memory: 15916kb

input:

5 2
0 1 0 0 0
0
1
2
3

output:

4 0

result:

ok single line: '4 0'

Test #41:

score: 0
Wrong Answer
time: 0ms
memory: 17416kb

input:

10 9
6 5 6 2 5 4 5 3 4 1
0
1
2
3
4
5
6
7
8

output:

2 0

result:

wrong answer 1st lines differ - expected: '3 0', found: '2 0'

Subtask #4:

score: 0
Wrong Answer

Test #76:

score: 23
Accepted
time: 2ms
memory: 15520kb

input:

1 1
0

output:

1 0

result:

ok single line: '1 0'

Test #77:

score: 23
Accepted
time: 0ms
memory: 16036kb

input:

5 2
0 1 0 0 0
0
1
2
3

output:

4 0

result:

ok single line: '4 0'

Test #78:

score: 0
Wrong Answer
time: 2ms
memory: 17292kb

input:

10 9
6 5 6 2 5 4 5 3 4 1
0
1
2
3
4
5
6
7
8

output:

2 0

result:

wrong answer 1st lines differ - expected: '3 0', found: '2 0'

Subtask #5:

score: 0
Skipped

Dependency #1:

0%