PAT --- A1142 Maximal Clique (25分)

题意

给出一个无向图,给出m组顶点集,判断是否为Maximal Clique。
Clique:集合内的任意两个顶点相连的顶点集。
Maximal Clique:不能再加入任何顶点以组成新的Clique的Clique。

思路

cnt[id]非常关键,否则后面遍历cnt对该组进行判定的时候,cnt可能会不包含in_vertices中的某些key,而算法正确的前提是cnt要包含in_vertices中所有的key。

#include "bits/stdc++.h"
using namespace std;
const int N = 210;
vector<int> links[N];
int main(){
//     freopen("input.txt","r",stdin);
    int nv, ne;  scanf("%d%d",&nv,&ne);
    for (int i = 0; i < ne; ++i) {
        int a,b; scanf("%d%d",&a,&b);
        links[a].push_back(b); links[b].push_back(a);
    }
    int m;  scanf("%d",&m);
    for (int i = 0; i < m; ++i) {
        int k; scanf("%d",&k);
        map<int,int> cnt, in_vertices;
        for (int j = 0; j < k; ++j) {
            int id; scanf("%d",&id); in_vertices[id] = 1,cnt[id];
            for(int t:links[id]) cnt[t] ++;
        }

        bool not_clique = false, not_maximal = false;
        for(auto &entry:cnt){
            if (in_vertices.count(entry.first) && entry.second != k-1) {
                not_clique = true;
                break;
            }else if (!in_vertices.count(entry.first) && entry.second == k){
                not_maximal = true;
            }
        }
        if (not_clique) printf("Not a Clique\n");
        else if (not_maximal) printf("Not Maximal\n");
        else printf("Yes\n");
    }
}

Sample Input:

8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
3 4 3 6
3 3 2 1

Sample Output:

Yes
Yes
Yes
Yes
Not Maximal
Not a Clique
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值