QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#883483 | #9184. Team Coding | I_love_Riley_Andersen | 0 | 3ms | 25364kb | C++17 | 3.6kb | 2025-02-05 16:36:25 | 2025-02-05 16:36:28 |
Judging History
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 = 2e5 + 5;
map<int,int> cntDepth[mxN];
vector<int> teamies[mxN];
vector<int> aj[mxN];
int ent[mxN];
int ext[mxN];
int col[mxN];
int dep[mxN];
int ans = INT_MIN;
int bbl = INT_MAX;
int tr;
int isInside(int u, int v){
return ent[u] <= ent[v] && ext[v] <= ext[u];
}
void preCalc(int u, int d){
dep[u] = d, ent[u] = tr ++;
each (v, aj[u]){
preCalc(v, d + 1);
}
ext[u] = tr ++;
}
void solve(int u){
cntDepth[u][dep[u]] = 1;
each (v, aj[u]){
solve(v);
if (sz(cntDepth[v]) > sz(cntDepth[u])){
swap(cntDepth[v], cntDepth[u]);
}
each (pr, cntDepth[v]){
auto [depth, cnt] = pr;
cntDepth[u][depth] += cnt;
}
}
int loc = 1;
int swp = 0;
int cnt = 0;
int und = 0;
int n = sz(teamies[col[u]]);
F0R (i, n){
int v = teamies[col[u]][i];
if (dep[u] ^ dep[v]){
if (isInside(u, v)) loc ++, und ++;
else cnt ++;
if (i < n - 1 && dep[v] ^ dep[teamies[col[u]][i + 1]]){
if (cntDepth[u].count(dep[v])){
int tmp = min(cnt, cntDepth[u][dep[v]] - und);
loc += tmp, swp += tmp;
}
cnt = 0;
}
} else {
cnt = 0;
}
}
if (loc > ans){
ans = loc, bbl = swp;
} else if (loc == ans){
bbl = min(bbl, swp);
}
}
int main(){
int N, K;
cin >> N >> K;
F0R (i, N){
cin >> col[i];
}
FOR (i, 1, N){
int B;
cin >> B;
aj[B].push_back(i);
}
preCalc(0, 0);
F0R (i, N){
teamies[col[i]].push_back(i);
}
ent[N] = INT_MIN;
ext[N] = INT_MAX;
F0R (i, K){
sort(all(teamies[i]),[](int i, int j){
return dep[i] < dep[j];
});
teamies[col[i]].push_back(N);
}
solve(0);
cout << ans << ' ' << bbl << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 12
Accepted
time: 2ms
memory: 25136kb
input:
1 1 0
output:
1 0
result:
ok single line: '1 0'
Test #2:
score: 0
Wrong Answer
time: 1ms
memory: 23760kb
input:
5 2 0 1 0 0 0 0 1 2 3
output:
2 -1
result:
wrong answer 1st lines differ - expected: '4 0', found: '2 -1'
Subtask #2:
score: 0
Wrong Answer
Test #17:
score: 19
Accepted
time: 3ms
memory: 25364kb
input:
1 1 0
output:
1 0
result:
ok single line: '1 0'
Test #18:
score: 0
Wrong Answer
time: 1ms
memory: 24028kb
input:
5 2 0 1 0 0 0 0 1 2 3
output:
2 -1
result:
wrong answer 1st lines differ - expected: '4 0', found: '2 -1'
Subtask #3:
score: 0
Wrong Answer
Test #39:
score: 27
Accepted
time: 3ms
memory: 24876kb
input:
1 1 0
output:
1 0
result:
ok single line: '1 0'
Test #40:
score: 0
Wrong Answer
time: 1ms
memory: 23880kb
input:
5 2 0 1 0 0 0 0 1 2 3
output:
2 -1
result:
wrong answer 1st lines differ - expected: '4 0', found: '2 -1'
Subtask #4:
score: 0
Wrong Answer
Test #76:
score: 23
Accepted
time: 3ms
memory: 23980kb
input:
1 1 0
output:
1 0
result:
ok single line: '1 0'
Test #77:
score: 0
Wrong Answer
time: 1ms
memory: 23488kb
input:
5 2 0 1 0 0 0 0 1 2 3
output:
2 -1
result:
wrong answer 1st lines differ - expected: '4 0', found: '2 -1'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%