-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaa.cpp
92 lines (85 loc) · 2.17 KB
/
aa.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
// #pragma GCC optimize(2)
#include <bits/stdc++.h>
#define fi first
#define se second
#define mkp(x, y) make_pair((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef double db;
typedef pair<int, int> PII;
int n, m, person;
vector<int> ve, cnt, ans, ccnt;
void dfs(int u) {
if(u == n-1) {
int mx = 0, mx_ = 0, p = -1, p_ = -1, tcnt = 0, tcnt_ = 0;
for(int i = 1; i <= m; i ++) {
if(cnt[i] > mx) {
mx = cnt[i];
p = i;
}
if(cnt[i] == mx) tcnt++;
}
mx_ = mx-1;
if(mx_) {
for(int i = 1; i <= m; i ++) {
if(cnt[i] == mx_) {
if(tcnt_ == 0) p_ = i;
tcnt_++;
}
}
}
bool ok = false;
if(p > 0 && p_ > 0) {
if(tcnt == n-2 && tcnt_ == 1) {
ans.push_back(p_);
ok = true;
}
else if(tcnt == 1 && tcnt_ == n-2) {
ans.push_back(p);
ok = true;
}
}
int tp = -1;
for(int i = 1; i <= m; i ++) {
if(cnt[i] == mx || mx_ && cnt[i] == mx_) {
tp = i;
break;
}
}
if(!ok) ans.push_back(tp);
return;
}
for(int i = 1; i <= m; i ++) {
ve.push_back(i);
cnt[i]++;
dfs(u+1);
ve.pop_back();
cnt[i]--;
}
}
void solve() {
cin >> n >> m;
cnt.resize(m+1);
dfs(0);
while(n--) {
for(auto x: ans) {
cout << x << ' ';
}
cout << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed; // << setprecision(20); // double
// freopen("i.txt", "r", stdin);
// freopen("o.txt", "w", stdout);
// time_t t1 = clock();
int Tcase = 1;
cin >> Tcase; // scanf("%d", &Tcase);
while (Tcase--)
solve();
// cout << "time: " << 1000.0 * (1.*(clock() - t1) / CLOCKS_PER_SEC) << "ms\n";
return 0;
}