Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
7
Indexable
#include <iostream>
#include <cstring>
using namespace std;

const int MAXN = 45;
int M[MAXN][MAXN];
int n;
bool selected[MAXN];
int maxSize = 0;

// 判斷當前選擇的學生是否構成一個團
bool isClique(int clique[], int size, int newStudent) {
    for (int i = 0; i < size; ++i) {
        if (M[clique[i]][newStudent] == 0) {
            return false;
        }
    }
    return true;
}

// 使用回溯法來找到最大的團
void findMaxClique(int pos, int clique[], int size) {
    // 剪枝:如果剩餘學生數量不足以超過當前最大團大小,則終止遞迴
    if (size + (n - pos) <= maxSize) {
        return;
    }

    if (pos == n) {
        if (size > maxSize) {
            maxSize = size;
        }
        return;
    }

    // 不選擇當前學生
    findMaxClique(pos + 1, clique, size);

    // 剪枝:如果當前學生與已選擇的學生不構成團,則不選擇該學生
    if (isClique(clique, size, pos)) {
        clique[size] = pos;
        findMaxClique(pos + 1, clique, size + 1);
    }
}

int main() {
    cin >> n;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> M[i][j];
        }
    }

    int clique[MAXN];
    memset(selected, false, sizeof(selected));
    findMaxClique(0, clique, 0);

    cout << maxSize << endl;

    return 0;
}
Editor is loading...
Leave a Comment