Untitled

 avatar
unknown
plain_text
a year ago
2.7 kB
5
Indexable
#ifdef DS
    #include "debug.h"
#else 
    #include<bits/stdc++.h>
    #define deb(...) 
#endif
using namespace std;
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FOD(i,a,b) for (int i=a;i>=b;i--)
#define ALL(x) x.begin(),x.end()
#define NALL(x) x.begin()+1,x.end()
#define TIME "Time elapsed : "<<(double)clock()/1000<<" s"
#define int long long
#define vi vector<int>
#define pii pair<int,int>
const int MOD=1e9+7,INF=4e18;
#define maxn 
#define bit(i,n) (n >> i & 1)
int d,n;

struct DSU
{
    vector<int> r, sz;
    int n;
    DSU(int _sz)
    {
        n = _sz;
        r.resize(_sz + 1);
        sz.assign(_sz + 1, 1);
        FOR(i, 1, n)
            r[i] = i;
    }
    int get_root(int u)
    {
        if (u == r[u]) return u;
        return r[u] = get_root(r[u]);
    }
    bool join(int u, int v)
    {
        u = get_root(u);
        v = get_root(v);
        if (u == v)
            return 0;
        if (sz[u] < sz[v])
            swap(u, v);
        sz[u] += sz[v];
        sz[v] = 0;
        return r[v] = u, 1;
    }
};
bool check(int m1,int m2)
{
    int c1 = __builtin_popcount(m1);
    int c2 = __builtin_popcount(m2);
    if (c1 == c2 || c1 == 0 || c2 == 0) 
        return false;
    if (c1 > c2) 
        swap(m1,m2);
    FOR(i,0,d-1)
        if (bit(i,m1) && !bit(i,m2)) 
        {
            // deb(i)
            return false;
        }
    // deb(m1,m2)
    return true;
}

int get(vi &com)
{
    int ans = 0;
    ans += __builtin_popcount(com.front());
    FOR(i,1,com.size()-1)\
        ans += (__builtin_popcount(com[i]) - __builtin_popcount(com[i-1]));
    return ans;
}

signed main()
{
    #ifndef ONLINE_JUDGE
    freopen("thu.inp","r",stdin);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin>>d>>n;
    vi a(n+1);
    set<int> se;
    FOR(i,1,n)
    {
        string s; cin>>s;
        FOR(j,0,d-1)
            if (s[j] == '1') a[i] += (1 << j);
        se.insert(a[i]);
    }
    DSU dsu(1500);
    FOR(i,1,n)  
        FOR(j,i+1,n)
        {
            if (check(a[i], a[j])) dsu.join(a[i], a[j]);
        }
    deb(a)
    int ans = 0;
    vector<vi> com(1500);
    for (int val = 1; val < (1 << (d+1)); val++)
        if (se.find(val) != se.end()) com[dsu.get_root(val)].push_back(val);
    for (int val = 1; val < (1 << (d+1)); val++)
    {
        if (com[val].size() == 0) continue;
        ans++;
        sort(ALL(com[val]), [](int &a,int &b)
        {
            return __builtin_popcount(a) < __builtin_popcount(b);
        });
        deb(com[val])
        ans += get(com[val]);
    }
    cout<<ans;
}
Editor is loading...
Leave a Comment