调试代码
查看原帖
调试代码
253342
HYX1124楼主2023/7/4 18:08
// 生成
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e5 + 10, K = 105;
const ll mod = 1e9 + 7;

template<typename T, typename... Ts>
auto print(T value, Ts... args) {
    std::cerr << value << " ";
    (void) std::initializer_list<T>{([&args] {
        std::cerr << args << " ";
    }(), value)...};
    std::cerr << std::endl;
}

int main() {
    srand(1145124);
    int n = 10, k = 8;
    print(n, k);
    rep(i, 2, n) print(i, rand() % (i - 1) + 1);
}
// 暴力
#include <bits/stdc++.h>
#include <cstring>
#ifndef ONLINE_JUDGE
#include "lib.h"
#endif
#define rep(i, min, max) for(int i = (min); i <= (max); ++i)
#define nrep(i, max, min) for(int i = (max); i >= (min); --i)
#define case() int Ts = read(); rep(T, 1, Ts)
#define putf(flag) puts((flag) ? "YES" : "NO")
#define put(x) printf("%d ", x)
#define putl(x) printf("%lld ", x)
#define endl() putchar('\n')
#define reads(str) scanf("%s", str + 1)
using namespace std;

typedef long long ll;
inline int read()
{
    int now=0; bool nev=false; char c=getchar();
    while(c<'0' || c>'9') { if(c=='-') nev=true; c=getchar(); }
    while(c>='0' && c<='9') { now=(now<<1)+(now<<3)+(c&15); c=getchar(); }
    return nev?-now:now;
}

const int N = 1e5 + 10, K = 105;
const ll mod = 1e9 + 7;
int n, k, fa[N], dep[N], siz[N];
vector<int> to[N], G[N];

void init(int x, int fath) {
    fa[x] = fath;
    dep[x] = dep[fath] + 1;
    siz[x] = 1;
    for(int y : to[x]) if(y != fath) init(y, x), siz[x] += siz[y];
}


bool mark[N], vis[N];
void solve() {
    int sum = 0;
    rep(S, 0, (1 << n) - 1) {
        if(__builtin_popcount(S) != k) continue;
        rep(i, 1, n) mark[i] = (S >> (i - 1)) & 1, vis[i] = 0;
        rep(i, 1, n) {
            for(int y : to[i]) if(mark[y]) vis[i] = 1;
        }
        bool flag = 1;
        rep(i, 1, n) flag &= vis[i];
        sum += flag;
    }
    put(sum); 
}

int main(){
    n = read(), k = read();
    rep(i, 1, n - 1) {
        int x = read(), y = read();
        to[x].push_back(y);
        to[y].push_back(x);
    }
    init(1, 0);
    solve();
}
2023/7/4 18:08
加载中...