minhngocbui50đã bình luận lúc 30, Tháng 5, 2025, 15:31
include <iostream>
include <vector>
include <queue>
include <set>
using namespace std;
int main() {
int N;
cin >> N;
vector<queue> need(N); // danh sách đối thủ của mỗi người chơi</queue>
// Nhập danh sách đối thủ
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N - 1; ++j) {
int x;
cin >> x;
need[i].push(x - 1); // chuyển về chỉ số 0-based
}
}
int days = 0;
int totalMatches = N * (N - 1) / 2;
int played = 0;
set<pair<int, int>> today;
// Tìm các cặp có thể đấu ngay hôm nay
while (true) {
today.clear();
for (int i = 0; i < N; ++i) {
if (need[i].empty()) continue;
int opp = need[i].front();
if (need[opp].empty()) continue;
if (need[opp].front() == i) {
int a = min(i, opp);
int b = max(i, opp);
today.insert({a, b});
}
}
if (today.empty()) break;
for (auto [a, b] : today) {
need[a].pop();
need[b].pop();
played++;
}
days++;
}
if (played == totalMatches)
cout << days << endl;
else
cout << -1 << endl;
return 0;
Bình luận
include <iostream>
include <vector>
include <queue>
include <set>
using namespace std;
int main() { int N; cin >> N; vector<queue> need(N); // danh sách đối thủ của mỗi người chơi</queue>
}