#include"beechtree.h"
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
const int mxn=200010;
vector<pi>adj[mxn];
int sz[mxn];
void dfs(int x)
{
sz[x]=1;
for(pi&t:adj[x])
dfs(t.fi),sz[x]+=sz[t.fi];
return;
}
bool isinc(const vector<int>&x,const vector<int>&y)
{
if(x.size()>y.size())
return 0;
for(const int&t:x)
if(!binary_search(all(y),t))
return 0;
return 1;
}
vector<int>subtr[mxn];
multimap<int,int>ss[mxn];
map<int,set<pi> >mm[mxn];
int n,m;
int p[mxn],c[mxn];
vector<vector<int> >st;
bool push(int i,int j)
{
subtr[i].eb(j);
int x=sz[j];
auto&s=ss[i];
{
auto it=s.ep(x,j);
if(it!=s.begin())
{
auto it2=prev(it);
if(!isinc(st[it2->se],st[j]))
return 0;
}
auto it2=next(it);
if(it2!=s.end())
{
if(!isinc(st[j],st[it2->se]))
return 0;
}
}
if(i==0)
return 1;
set<pi>&m=mm[i][c[j]];
int y=sz[p[j]];
{
auto it=m.ep(x,y).fi;
if(it!=m.begin())
{
auto it2=prev(it);
if(it2->se>y||(x!=it2->fi&&y==it2->se))
return 0;
}
auto it2=next(it);
if(it2!=m.end())
{
if(y>it2->se||(x!=it2->fi&&y==it2->se))
return 0;
}
}
return 1;
}
vector<int>beechtree(int N,int M,vector<int>P,vector<int>C)
{
n=N,m=M;
for(int i=0;i<n;i++)
p[i]=P[i],c[i]=C[i];
vector<int>isok(n,1);
for(int i=1;i<n;i++)
adj[p[i]].eb(i,c[i]);
st.resize(n);
for(int i=n;i-->0;)
{
for(pi&t:adj[i])
st[i].eb(t.se);
sort(all(st[i]));
auto it=unique(all(st[i]));
if(it!=st[i].end())
isok[i]=0;
if(isok[i]==0&&i>0)
isok[p[i]]=0;
st[i].eb(inf);
}
dfs(0);
vector<int>vv;
for(int i=0;i<n;i++)
if(isok[i]==1)
vv.eb(i);
sort(all(vv),[&](int x,int y){return sz[x]<sz[y];});
for(int&i:vv)
{
if(isok[i]==0)
{
if(i>0)
isok[p[i]]=0;
continue;
}
for(pi&tt:adj[i])
{
int t=tt.fi;
}
push(i,i);
for(pi&tt:adj[i])
{
int t=tt.fi;
if(subtr[t].size()>subtr[i].size())
swap(subtr[t],subtr[i]),
swap(ss[t],ss[i]),
swap(mm[t],mm[i]);
ss[t].clear();
mm[t].clear();
for(int&x:subtr[t])
{
if(!push(i,x))
{
isok[i]=0;
break;
}
}
subtr[t].clear();
if(isok[i]==0)
break;
}
if(isok[i]==0)
{
if(i>0)
isok[p[i]]=0;
continue;
}
}
return isok;
}
Compilation message
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:121:17: warning: unused variable 't' [-Wunused-variable]
121 | int t=tt.fi;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
6 ms |
30552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30552 KB |
Output is correct |
3 |
Correct |
7 ms |
30808 KB |
Output is correct |
4 |
Correct |
6 ms |
30552 KB |
Output is correct |
5 |
Correct |
6 ms |
30552 KB |
Output is correct |
6 |
Correct |
7 ms |
30552 KB |
Output is correct |
7 |
Correct |
7 ms |
30552 KB |
Output is correct |
8 |
Correct |
6 ms |
30552 KB |
Output is correct |
9 |
Incorrect |
6 ms |
30552 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30552 KB |
Output is correct |
3 |
Correct |
7 ms |
30808 KB |
Output is correct |
4 |
Correct |
6 ms |
30552 KB |
Output is correct |
5 |
Correct |
6 ms |
30552 KB |
Output is correct |
6 |
Correct |
7 ms |
30552 KB |
Output is correct |
7 |
Correct |
84 ms |
62416 KB |
Output is correct |
8 |
Correct |
84 ms |
62424 KB |
Output is correct |
9 |
Correct |
7 ms |
30808 KB |
Output is correct |
10 |
Correct |
6 ms |
30808 KB |
Output is correct |
11 |
Correct |
7 ms |
30808 KB |
Output is correct |
12 |
Correct |
6 ms |
30808 KB |
Output is correct |
13 |
Correct |
11 ms |
31324 KB |
Output is correct |
14 |
Correct |
9 ms |
31068 KB |
Output is correct |
15 |
Correct |
7 ms |
31064 KB |
Output is correct |
16 |
Correct |
7 ms |
31064 KB |
Output is correct |
17 |
Correct |
228 ms |
88304 KB |
Output is correct |
18 |
Correct |
169 ms |
79564 KB |
Output is correct |
19 |
Correct |
106 ms |
66528 KB |
Output is correct |
20 |
Correct |
92 ms |
62416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30808 KB |
Output is correct |
2 |
Correct |
6 ms |
30808 KB |
Output is correct |
3 |
Correct |
6 ms |
30808 KB |
Output is correct |
4 |
Correct |
7 ms |
30812 KB |
Output is correct |
5 |
Correct |
6 ms |
30808 KB |
Output is correct |
6 |
Correct |
7 ms |
30808 KB |
Output is correct |
7 |
Correct |
8 ms |
30808 KB |
Output is correct |
8 |
Correct |
6 ms |
30812 KB |
Output is correct |
9 |
Correct |
6 ms |
30808 KB |
Output is correct |
10 |
Correct |
7 ms |
30808 KB |
Output is correct |
11 |
Correct |
8 ms |
31064 KB |
Output is correct |
12 |
Correct |
8 ms |
31064 KB |
Output is correct |
13 |
Correct |
7 ms |
31064 KB |
Output is correct |
14 |
Correct |
7 ms |
31068 KB |
Output is correct |
15 |
Correct |
137 ms |
64864 KB |
Output is correct |
16 |
Correct |
126 ms |
62408 KB |
Output is correct |
17 |
Correct |
120 ms |
62412 KB |
Output is correct |
18 |
Correct |
142 ms |
64204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30552 KB |
Output is correct |
3 |
Correct |
7 ms |
30552 KB |
Output is correct |
4 |
Correct |
6 ms |
30552 KB |
Output is correct |
5 |
Correct |
84 ms |
62416 KB |
Output is correct |
6 |
Correct |
84 ms |
62424 KB |
Output is correct |
7 |
Correct |
6 ms |
30808 KB |
Output is correct |
8 |
Correct |
6 ms |
31064 KB |
Output is correct |
9 |
Correct |
8 ms |
31320 KB |
Output is correct |
10 |
Correct |
8 ms |
31320 KB |
Output is correct |
11 |
Correct |
229 ms |
87512 KB |
Output is correct |
12 |
Correct |
226 ms |
89096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
6 ms |
30552 KB |
Output is correct |
4 |
Correct |
6 ms |
30552 KB |
Output is correct |
5 |
Correct |
7 ms |
30552 KB |
Output is correct |
6 |
Correct |
7 ms |
30808 KB |
Output is correct |
7 |
Correct |
6 ms |
30552 KB |
Output is correct |
8 |
Correct |
6 ms |
30552 KB |
Output is correct |
9 |
Correct |
7 ms |
30552 KB |
Output is correct |
10 |
Correct |
7 ms |
30552 KB |
Output is correct |
11 |
Correct |
6 ms |
30552 KB |
Output is correct |
12 |
Incorrect |
6 ms |
30552 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30552 KB |
Output is correct |
3 |
Correct |
7 ms |
30552 KB |
Output is correct |
4 |
Correct |
6 ms |
30552 KB |
Output is correct |
5 |
Incorrect |
6 ms |
30552 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
6 ms |
30552 KB |
Output is correct |
4 |
Correct |
6 ms |
30552 KB |
Output is correct |
5 |
Correct |
7 ms |
30552 KB |
Output is correct |
6 |
Correct |
7 ms |
30808 KB |
Output is correct |
7 |
Correct |
6 ms |
30552 KB |
Output is correct |
8 |
Correct |
6 ms |
30552 KB |
Output is correct |
9 |
Correct |
7 ms |
30552 KB |
Output is correct |
10 |
Correct |
7 ms |
30552 KB |
Output is correct |
11 |
Correct |
6 ms |
30552 KB |
Output is correct |
12 |
Incorrect |
6 ms |
30552 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30552 KB |
Output is correct |
3 |
Correct |
7 ms |
30552 KB |
Output is correct |
4 |
Correct |
6 ms |
30552 KB |
Output is correct |
5 |
Incorrect |
6 ms |
30552 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
30552 KB |
Output is correct |
2 |
Correct |
7 ms |
30812 KB |
Output is correct |
3 |
Correct |
6 ms |
30552 KB |
Output is correct |
4 |
Correct |
6 ms |
30552 KB |
Output is correct |
5 |
Correct |
7 ms |
30552 KB |
Output is correct |
6 |
Correct |
7 ms |
30808 KB |
Output is correct |
7 |
Correct |
6 ms |
30552 KB |
Output is correct |
8 |
Correct |
6 ms |
30552 KB |
Output is correct |
9 |
Correct |
7 ms |
30552 KB |
Output is correct |
10 |
Correct |
7 ms |
30552 KB |
Output is correct |
11 |
Correct |
6 ms |
30552 KB |
Output is correct |
12 |
Incorrect |
6 ms |
30552 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |