結果

問題 No.382 シャイな人たち (2)
ユーザー 👑 NachiaNachia
提出日時 2022-08-26 00:37:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,690 ms / 8,000 ms
コード長 3,551 bytes
コンパイル時間 906 ms
コンパイル使用メモリ 69,600 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-21 06:28:54
合計ジャッジ時間 31,484 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,622 ms
5,248 KB
testcase_01 AC 1,690 ms
6,944 KB
testcase_02 AC 1,379 ms
6,940 KB
testcase_03 AC 1,439 ms
6,940 KB
testcase_04 AC 1,326 ms
6,940 KB
testcase_05 AC 1,506 ms
6,940 KB
testcase_06 AC 1,140 ms
6,948 KB
testcase_07 AC 1,550 ms
6,940 KB
testcase_08 AC 1,174 ms
6,940 KB
testcase_09 AC 1,266 ms
6,944 KB
testcase_10 AC 1,272 ms
6,940 KB
testcase_11 AC 1,153 ms
6,944 KB
testcase_12 AC 1,341 ms
6,944 KB
testcase_13 AC 1,357 ms
6,940 KB
testcase_14 AC 1,550 ms
6,940 KB
testcase_15 AC 1,610 ms
6,940 KB
testcase_16 AC 1,288 ms
6,940 KB
testcase_17 AC 1,197 ms
6,940 KB
testcase_18 AC 1,505 ms
6,940 KB
testcase_19 AC 1,315 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "Main.cpp"

#line 2 "nachia\\graph\\maximum-independent-set.hpp"

#include <vector>
#include <algorithm>
#include <cassert>

namespace nachia{

std::vector<int> MaximumIndependentSet(std::vector<std::vector<int>> matrix){
    int n = matrix.size();
    assert(1 <= n && n <= 1000);
    for(int i=0; i<n; i++) assert(n == (int)matrix[i].size());
    for(int i=0; i<n; i++) for(int j=0; j<n; j++) assert(0 <= matrix[i][j] && matrix[i][j] <= 1);
    for(int i=0; i<n; i++) for(int j=0; j<n; j++) assert(matrix[i][j] == matrix[j][i]);
    std::vector<int> ans, tmp(n), can, idx(n);
    int tmpsz = 0;
    for(int i=0; i<n; i++){
        if(!matrix[i][i]){ can.push_back(i); }
        else{ for(int j=0; j<n; j++){ idx[j] -= matrix[j][i]; } }
    }
    for(int i=0; i<n; i++) for(int j=0; j<n; j++) idx[i] += matrix[i][j];
    auto registerTmp = [&]() -> void {
        if((int)ans.size() < tmpsz) ans = std::vector(tmp.begin(), tmp.begin() + tmpsz);
    };
    auto dfs = [&](std::vector<int>::iterator subcan, int len, auto& self) -> void {
        if(ans.size() >= can.size() + len) return;
        if(len == 0) return registerTmp();
        int ty = 0;
        for(int i=0; i<len; i++) if(idx[subcan[i]] <= 1){ std::swap(subcan[0], subcan[i]); ty = 1; break; }
        if(!ty){
            for(int i=0; i<len; i++) if(idx[subcan[0]] < idx[subcan[i]]){ std::swap(subcan[0], subcan[i]); }
            if(idx[subcan[0]] >= 3){ ty = 2; }
        }
        if(ty){
            int p = subcan[0], cnt = 1;
            for(int i=1; i<len; i++) if(matrix[p][subcan[i]]){
                std::swap(subcan[cnt], subcan[i]);
                for(int j=cnt+1; j<len; j++) idx[subcan[j]] -= matrix[subcan[cnt]][subcan[j]];
                cnt++;
            }
            tmp[tmpsz++] = p;
            self(subcan+cnt, len-cnt, self);
            for(int j=cnt-1; j>=1; j--) for(int i=j+1; i<len; i++) idx[subcan[i]] += matrix[subcan[j]][subcan[i]];
            tmpsz--;
            if(ty == 1){  return; }
            for(int i=0; i<len; i++) idx[subcan[i]] -= matrix[p][subcan[i]];
            self(subcan + 1, len - 1, self);
            for(int i=0; i<len; i++) idx[subcan[i]] += matrix[p][subcan[i]];
            return;
        }
        std::vector<int> vis(len, 0);
        int cnt = 0;
        for(int s=0; s<len; s++) if(vis[s] == 0){
            int t = 0, p = s;
            while(!vis[p]){
                int nx = -1;
                for(int j=0; j<len; j++) if(!vis[j]) if(matrix[subcan[p]][subcan[j]]){ nx = j; break; }
                if(nx == -1) break;
                if((t ^= 1) == 0){ tmp[tmpsz++] = subcan[p]; cnt++; }
                vis[p] = 1; p = nx;
            }
        }
        registerTmp();
        tmpsz -= cnt;
    };
    dfs(can.begin(), (int)can.size(), dfs);
    std::sort(ans.begin(), ans.end());
    return ans;
}

} // namespace nachia
#line 3 "Main.cpp"

#include <cstdio>

int main(){
    int S; scanf("%d", &S);
    long long x = S;
    auto next = [&]() -> long long { return x = x * 12345 % 1000003; };
    int N = next() % 120 + 2;
    int P = next();
    std::vector<std::vector<int>> G(N, std::vector<int>(N));
    for(int i=0; i<N; i++) for(int j=i+1; j<N; j++) if(next() >= P) G[i][j] = G[j][i] = 1;
    std::vector<int> ans = nachia::MaximumIndependentSet(G);
    for(int& a : ans) a++;
    int K = ans.size();
    if(K == N){ printf("-1\n"); return 0; }
    printf("%d\n", K+1);
    for(int i=0; i<K; i++){
        if(i) printf(" ");
        printf("%d", ans[i]);
    }
    printf("\n");
    return 0;
}
0