#include "beechtree.h"
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
using namespace std;
const int mxN=200010;
const int mxM=200100;
const int mxK=61;
const int MOD=1e9;
const ll INF=2e18;
int N, M;
vector <pii> v[mxN];
pii par[mxN];
bool ok[mxN];
int sub[mxN];
set <pii> s[mxN]; ///subtree 크기, 노드
void input(){
cin >> N >> M;
for(int i=1;i<=N;i++){
cin >> par[i].fi;
par[i].fi++;
}
for(int i=1;i<=N;i++) cin >> par[i].se;
for(int i=2;i<=N;i++) v[par[i].fi].emplace_back(par[i].se, i);
for(int i=1;i<=N;i++) if(v[i].size()) sort(all(v[i]));
}
bool cmp(int a, int b){
if((int)v[a].size()>(int)v[b].size()) return false;
for(auto [nxta, x] : v[a]){
int idx=lower_bound(all(v[b]), pii(nxta, 0))-v[b].begin();
if(idx==v[b].size() || v[b][idx].fi!=nxta || sub[v[b][idx].se]<sub[x]) return false;
}
return true;
}
bool add(int now, int idx){
pii nv=pii(sub[now], now);
auto it=s[idx].lower_bound(nv);
if(it!=s[idx].end()) if(!cmp(now, it->se)){
return false;
}
if(it!=s[idx].begin()){
it--;
if(!cmp(it->se, now)){
return false;
}
}
s[idx].insert(nv);
return true;
}
void dfs(int now){
sub[now]=1;
if(v[now].empty()){
ok[now]=true;
s[now].insert(pii(sub[now], now));
return;
}
for(auto [x, nxt] : v[now]) dfs(nxt), sub[now]+=sub[nxt];
for(auto [x, nxt] : v[now]) if(!ok[nxt]) return;
for(int i=1;i<v[now].size();i++) if(v[now][i-1].fi==v[now][i].fi) return;
int bi=-1;
for(auto [x, nxt] : v[now]) if(bi==-1 || sub[nxt]>sub[bi]) bi=nxt;
for(auto [x, nxt] : v[now]) if(nxt!=bi){
for(auto [_, y] : s[nxt]) if(!add(y, bi)){
return;
}
s[nxt].clear();
}
swap(s[now], s[bi]);
ok[now]=true;
s[now].insert(pii(sub[now], now));
}
vector <int> solve(){
dfs(1);
vector <int> res;
for(int i=1;i<=N;i++) res.push_back(ok[i]);
return res;
}
std::vector<int> beechtree(int n, int m, std::vector<int> P, std::vector<int> C)
{
N=n, M=m;
for(int i=0;i<N;i++) par[i+1]=pii(P[i]+1, C[i]);
for(int i=2;i<=N;i++) v[par[i].fi].emplace_back(par[i].se, i);
for(int i=1;i<=N;i++) if(v[i].size()) sort(all(v[i]));
return solve();
}
Compilation message
beechtree.cpp: In function 'bool cmp(int, int)':
beechtree.cpp:41:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(idx==v[b].size() || v[b][idx].fi!=nxta || sub[v[b][idx].se]<sub[x]) return false;
| ~~~^~~~~~~~~~~~~
beechtree.cpp: In function 'void dfs(int)':
beechtree.cpp:69:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i=1;i<v[now].size();i++) if(v[now][i-1].fi==v[now][i].fi) return;
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Incorrect |
4 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Incorrect |
4 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Incorrect |
3 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Incorrect |
4 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Incorrect |
4 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Incorrect |
4 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Incorrect |
4 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Incorrect |
4 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Incorrect |
4 ms |
16988 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |