QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#90924#6131. TournamentksunhokimTL 3ms3400kbC++171.7kb2023-03-26 07:40:002023-03-26 07:40:01

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-26 07:40:01]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:3400kb
  • [2023-03-26 07:40:00]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve() {
  int n,k;
  cin >> n >> k;
  if (n%2 == 1) {
    cout << "Impossible" << "\n";
    return;
  }
  vector g(n, vector<int>(n));
  for (int i=0;i<n;i++){
    g[i][i] = true;
  }
  vector<vector<int>> ans;
  for (int i=0;i<k;i++){
    ans.push_back(vector<int>(n));
    bool ok = true;
    vector<int> vis(n);
    set<int> todo;
    for (int j=0;j<n;j++) todo.insert(j);
    while (!todo.empty()) {
      int cur = *todo.begin();
      todo.erase(todo.begin());
      int next = -1;
      for (int j=0;j<n;j++){
        if (cur != j && !g[cur][j] && !vis[j]) {
          next = j;
          break;
        }
      }
      if (next == -1) {
        ok = false;
        break;
      }
      auto dfs = [&] (auto self, int u, int v) -> void {
        todo.erase(u);
        todo.erase(v);
        vis[u] = true;
        vis[v] = true;
        g[u][v] = true;
        g[v][u] = true;
        ans.back()[u] = v;
        ans.back()[v] = u;
        for (int l=0;l<n;l++){
          if (l == u || vis[l] || g[u][l] || !g[v][l])
            continue;
          for (int r=l+1;r<n;r++){
            if (r == v || vis[r] || g[v][r] || !g[u][r])
              continue;
            self(self, l, r);
          }
        }
      };
      dfs(dfs, cur, next);
    }
    if (!ok) {
      cout << "Impossible" << "\n";
      return;
    }
  }
  for (int i=0;i<k;i++) {
    for (int j=0;j<n;j++) {
      if (j != n-1) {
        cout << ans[i][j] + 1 << " ";
      } else {
        cout << ans[i][j] + 1;
      }
    }
    cout << "\n";
  }
  
}

int main() {
  int t;
  cin >> t;
  while (t--)
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3364kb

input:

2
3 1
4 3

output:

Impossible
2 1 4 3
3 4 1 2
4 3 2 1

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 3ms
memory: 3400kb

input:

100
1 4
2 1
2 2
2 3
3 6
4 2
4 3
4 4
4 5
5 4
6 1
6 2
6 4
7 1
8 3
8 7
8 8
8 14
9 4
10 1
10 2
10 3
12 2
12 3
12 4
12 8
13 2
14 1
14 2
14 4
15 4
16 9
16 15
16 16
16 28
17 6
18 1
18 2
18 4
19 5
20 1
20 3
20 4
20 6
21 1
22 1
22 2
22 3
23 4
24 5
24 7
24 8
24 15
25 3
26 1
26 2
26 3
27 5
28 1
28 3
28 4
28 6
...

output:

Impossible
2 1
Impossible
Impossible
Impossible
2 1 4 3
3 4 1 2
2 1 4 3
3 4 1 2
4 3 2 1
Impossible
Impossible
Impossible
2 1 4 3 6 5
Impossible
Impossible
Impossible
2 1 4 3 6 5 8 7
3 4 1 2 7 8 5 6
4 3 2 1 8 7 6 5
2 1 4 3 6 5 8 7
3 4 1 2 7 8 5 6
4 3 2 1 8 7 6 5
5 6 7 8 1 2 3 4
6 5 8 7 2 1 4 3
7 8 5 ...

result:

ok 194 lines

Test #3:

score: -100
Time Limit Exceeded

input:

5
512 511
512 513
896 120
896 130
960 60

output:


result: