結果
| 問題 |
No.2263 Perms
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2024-07-04 11:45:18 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 6 ms / 2,000 ms |
| コード長 | 2,007 bytes |
| コンパイル時間 | 935 ms |
| コンパイル使用メモリ | 61,472 KB |
| 最終ジャッジ日時 | 2025-02-22 01:56:09 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 39 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:60:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
60 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:62:38: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
62 | for (int j = 0; j < n; j++) scanf("%d", as[i] + j);
| ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-
*
* 2263.cc: No.2263 Perms - yukicoder
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
/* constant */
const int MAX_N = 50;
const int MAX_M = 50;
/* typedef */
using vi = vector<int>;
using vb = vector<bool>;
/* global variables */
int as[MAX_N][MAX_N], ps[MAX_M][MAX_N];
vi nbrs[MAX_N * 2];
/* subroutines */
bool max_match_rec(const vi *nbrs, int u, vi &matches, vb &visited) {
if (u < 0) return true;
for (const int v: nbrs[u]) {
if (! visited[v]) {
visited[v] = true;
if (max_match_rec(nbrs, matches[v], matches, visited)) {
matches[u] = v;
matches[v] = u;
return true;
}
}
}
return false;
}
int max_match(const int n, int l, const vi *nbrs, vi &matches) {
matches.assign(n, -1);
int count = 0;
for (int u = 0; u < l; u++) {
vb visited(n, false);
if (max_match_rec(nbrs, u, matches, visited)) count++;
}
return count;
}
/* main */
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", as[i] + j);
bool ok = true;
for (int i = 0; ok && i < n; i++) {
int s = 0;
for (int j = 0; j < n; j++) s += as[i][j];
ok = (s == m);
}
for (int j = 0; ok && j < n; j++) {
int s = 0;
for (int i = 0; i < n; i++) s += as[i][j];
ok = (s == m);
}
if (! ok) { puts("-1"); return 0; }
int n2 = n * 2;
for (int i = 0; i < m; i++) {
for (int u = 0; u < n2; u++) nbrs[u].clear();
for (int u = 0; u < n; u++)
for (int v = 0; v < n; v++)
if (as[u][v] > 0) {
nbrs[u].push_back(n + v);
nbrs[n + v].push_back(u);
}
vi matches;
int cnt = max_match(n2, n, nbrs, matches);
if (cnt < n) { puts("-1"); return 0; }
for (int j = 0; j < n; j++) {
ps[i][j] = matches[j] - n;
as[j][ps[i][j]]--;
}
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
printf("%d%c", ps[i][j] + 1, (j + 1 < n) ? ' ' : '\n');
return 0;
}
tnakao0123