이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n1,n2,k;
vector<int> adjl1[300005];
vector<int> adjl2[300005];
int preord1[300005];
int someval[300005];
int finord[300005];
int cur = 1;
int root1,root2;
int min1[300005];
int max1[300005];
int min2[300005];
int max2[300005];
void dfs1(int node){
preord1[node] = cur++;
for (auto x : adjl1[node]){
dfs1(x);
}
}
bool cmp(int a, int b){
return someval[a]<someval[b];
}
void dfs2(int node){
if (node<=k){
someval[node] = preord1[node];
return;
}
someval[node] = 999999999;
for (auto x : adjl2[node]){
dfs2(x);
someval[node] = min(someval[node],someval[x]);
}
sort(adjl2[node].begin(),adjl2[node].end(),cmp);
}
void dfs3(int node){
finord[node] = cur++;
if (node<=k) {
min2[node] = finord[node];
max2[node] = finord[node];
}
else{
min2[node] = 999999999;
max2[node] = -1;
}
for (auto x : adjl2[node]){
dfs3(x);
min2[node] = min(min2[node],min2[x]);
max2[node] = max(max2[node],max2[x]);
}
}
void dfs4(int node){
if (node<=k) {
min1[node] = finord[node];
max1[node] = finord[node];
}
else{
min1[node] = 999999999;
max1[node] = -1;
}
for (auto x : adjl1[node]){
dfs4(x);
min1[node] = min(min1[node],min1[x]);
max1[node] = max(max1[node],max1[x]);
}
}
bool cmp2(pair<int,int>a,pair<int,int>b){
if (a.first<b.first) return true;
if (a.first>b.first) return false;
return a.second>b.second;
}
int main(){
scanf("%d%d%d",&n1,&n2,&k);
for (int x = 1; x<=n1; x++){
int t;
scanf("%d",&t);
if (t!=0){
adjl1[t].push_back(x);
}
else root1 = x;
}
for (int x = 1; x<=n2; x++){
int t;
scanf("%d",&t);
if (t!=0){
adjl2[t].push_back(x);
}
else root2 = x;
}
dfs1(root1);
//printf("1\n");
dfs2(root2);
//printf("2\n");
dfs3(root2);
//printf("3\n");
dfs4(root1);
//printf("4\n");
vector<pair<int,int> > stuff;
for (int x = k+1; x<=n1; x++){
stuff.push_back({min1[x],max1[x]});
}
for (int x = k+1; x<=n2; x++){
stuff.push_back({min2[x],max2[x]});
}
sort(stuff.begin(),stuff.end(),cmp2);
stack<int> ends;
for (auto x : stuff){
while (!ends.empty() && ends.top()<x.first)ends.pop();
if (ends.empty() || x.second<=ends.top()){
ends.push(x.second);
}
else{
printf("NO");
return 0;
}
}
printf("YES");
}
컴파일 시 표준 에러 (stderr) 메시지
family.cpp: In function 'int main()':
family.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d%d%d",&n1,&n2,&k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
family.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d",&t);
| ~~~~~^~~~~~~~~
family.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d",&t);
| ~~~~~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |