题解 CF1143C 【Queen】

· · 题解

题意:给定一颗树,求所有不尊重父亲且所有儿子都不尊重的点

分析&解法:很容易发现题目树的给法根本不需要建树。我们对每个节点定两个布尔值 a,b。a 表示该节点是否不尊重父亲,是题目给出的;b 表示是否所有儿子都不尊重它,显然该值成立当且仅当它的所有儿子都不尊重它,所以该值为所有儿子 a 值的逻辑且。

代码就很简单了


// Problem: CF1143C Queen
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1143C
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <cstring>
#include <iostream>
using namespace std;
bool a[ 100010 ], b[ 100010 ];
int main()
{
    int n;
    cin >> n;
    memset( a, 1, sizeof( a ) );
    for ( int i = 1; i <= n; i++ )
    {
        int x, y;
        cin >> x >> y;
        if ( x != -1 )
        {
            a[ x ] &= y;
            b[ i ] = y;
        }
    }
    bool flag = false;
    for ( int i = 1; i <= n; i++ )
    {
        if ( b[ i ] && a[ i ] )
        {
            flag = true;
            cout << i << " ";
        }
    }
    if ( !flag )
    {
        cout << -1;
    }
    cout << endl;
}