0分求助
查看原帖
0分求助
447573
zevan楼主2024/7/18 21:23
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define SZ(v) ((int)v.size())
#define pii pair<int, int>
#define int long long
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef double db;
using namespace std;
const int N = 1e5+10;
const int mod = 1e9+7;
int _;

int n;
int a[N], b[N], c[N];
int q, m;
map<int, vector<int>> mp;

bool check(int x) {
    b[x] = 0;
    for(auto t : mp[x]) {
        b[t] = 0;
        for(auto tt : mp[t]) {
            b[tt] = 0;
        }
    }
    for(int i = 0; i < n; i++) {
        if(b[i] == 1) return 0;
    }
    return 1;
}

void solve() {
    cin >> n;
    for(int i = 1; i < n; i++) {
        cin >> a[i];
        mp[a[i]].pb(i);
    }
    cin >> q;
    while(q--) {
        cin >> m;
        memset(c, 0, sizeof(c));
        for(int i = 0; i < m; i++) {
            int x;
            cin >> x;
            c[x] = 1;
        }
        int ans = 0;
        for(int i = n - 1; i >= 1; i--) {
            for(int j = 0; j < n; j++) {
                b[j] = c[j];
            }
            if(check(i)) {
                ans = i;
                break;
            }
        }
        cout << ans << "\n";
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    _ = 1;
    // cin >> _;
    
    while(_--) {
        solve();
    }
    
    return 0;
}

2024/7/18 21:23
加载中...