PAT(甲级)2024夏季考试A-3

模拟

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
//        Scanner scan = new Scanner(System.in);
//        int n = scan.nextInt();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        ArrayList<Integer>[] node = new ArrayList[n + 1];
        Arrays.setAll(node, i -> new ArrayList<>());
        int m = Integer.parseInt(st.nextToken());
        for (int i = 0; i < m; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            node[a].add(b);
            node[b].add(a);
        }
        st = new StringTokenizer(br.readLine());
        int k = Integer.parseInt(st.nextToken());
        for (int i = 0; i < k; i++) {
            st = new StringTokenizer(br.readLine());
            int t = Integer.parseInt(st.nextToken());
            boolean[] isVisited = new boolean[n + 1];
            for (int j = 0; j < t; j++) {
                int q = Integer.parseInt(st.nextToken());
                isVisited[q] = true;
            }
            boolean flag = true;
            for (int j = 1; j <= n; j++) {
                if (!isVisited[j]) {
                    if (node[j].isEmpty()) {
                        flag = false;
                        System.out.println("no");
                        break;
                    } else {
                        boolean flag1 = false;
                        for (int e : node[j]) {
                            if (isVisited[e]) {
                                flag1 = true;
                                break;
                            }
                        }
                        if (!flag1) {
                            System.out.println("no");
                            flag = false;
                            break;
                        }
                    }
                }
            }
            if (flag) {
                System.out.println("yes");
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值