-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_ish_bt.cpp
143 lines (129 loc) · 3.04 KB
/
count_ish_bt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* TODO
*/
#include <iostream>
#include <vector>
#include <sstream>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
template <class T> int size(const T &x) { return x.size(); }
const int INF = 2147483647;
#define rep(i,a,b) for (__typeof(a) i=(a); i<(b); ++i)
#define iter(it,c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
int n;
struct node {
node *parent;
node **children;
bool end;
int mxchild;
node(node *par) {
parent = par;
children = new node*[n];
memset(children, 0, n * sizeof(node*));
end = false;
mxchild = -1;
}
};
ll **nck, *res;
void bt(int at, int cnt, vector<node*> nds) {
int idx = 0;
while (idx < size(nds)) {
if (nds[idx]->mxchild < at) {
swap(nds[idx], nds[size(nds)-1]);
nds.pop_back();
} else {
idx++;
}
}
if (size(nds) == 0) {
int left = n-at;
for (int add = 0; add <= left; add++) {
res[cnt + add] += nck[left][add];
}
return;
}
bt(at+1, cnt, nds);
vector<node*> nxt(nds);
rep(i,0,size(nds)) {
if (nds[i]->children[at]) {
if (nds[i]->children[at]->end) {
return;
}
nxt.push_back(nds[i]->children[at]);
}
}
bt(at+1, cnt+1, nxt);
}
int main() {
string line;
getline(cin, line);
stringstream ss(line);
int m;
ss >> n >> m;
nck = new ll*[n+1];
rep(i,0,n+1) {
nck[i] = new ll[i+1];
nck[i][0] = nck[i][i] = 1;
rep(j,1,i) {
nck[i][j] = nck[i-1][j-1] + nck[i-1][j];
}
}
node *root = new node(NULL);
vector<ii> ecnt(n);
rep(i,0,n) ecnt[i].second = i;
vector<set<int> > paths;
rep(i,0,m) {
getline(cin, line);
ss.str(line);
ss.clear();
int cur;
set<int> path;
while (ss >> cur) {
path.insert(cur);
}
paths.push_back(path);
iter(it,path) {
ecnt[*it].first++;
}
}
sort(ecnt.rbegin(), ecnt.rend());
// sort(ecnt.begin(), ecnt.end());
vi newidx(n);
rep(i,0,size(ecnt)) {
newidx[ecnt[i].second] = i;
}
rep(i,0,size(paths)) {
set<int> path;
iter(it,paths[i]) {
path.insert(newidx[*it]);
}
node *at = root;
iter(it,path) {
if (!at->children[*it]) {
at->children[*it] = new node(at);
}
at->mxchild = max(at->mxchild, *it);
at = at->children[*it];
}
at->end = true;
}
// TODO: Relabel vertices to maximize use of quick evaluation
res = new ll[n+1];
memset(res, 0, sizeof(ll)*(n+1));
bt(0, 0, vector<node*>(1,root));
rep(i,0,n+1) {
cout << res[i] << endl;
}
rep(i,0,n+1) {
delete[] nck[i];
}
delete[] nck;
delete[] res;
return 0;
}