YUGI - Yugi-Oh

Tác giả: ll931110

Ngôn ngữ: C++

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <queue>
#include <vector>
#include <utility>
using namespace std;

int a[202][202];
bool chs[202];
int n,k;

bool ok(int x)
{
    memset(chs,false,sizeof(chs));
    int gr = 0;
    for (int i = 1; i <= n; i++) if (!chs[i])
    {
        gr++; if (gr == k) return true;
        chs[i] = true;
        queue<int> q;  q.push(i);
        while (!q.empty())
        {
            int u = q.front();  q.pop();
            for (int v = 1; v <= n; v++)
              if (!chs[v] && a[u][v] < x)
              {
                    chs[v] = true;  q.push(v);
                };
        };
    };
    return false;
};

int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
    int inf = 0;  int sup = 32767;  int ret = 0;
    while (inf <= sup)
    {
        int med = (inf + sup) / 2;
        if (ok(med))
        {
            ret = med;  inf = med + 1;
        }
        else sup = med - 1;
    };
    printf("%d\n", ret);
};

Download