The problem tag might not be correct. Please feel free to leave a comment or use the report button at the top right of the navigation bar.
Tag Assigner
Đồ thị darkkcyan

Comments

Please read the guidelines before commenting.



  • 1
    minhngocbui50  commented on May 30, 2025, 3:31 p.m.

    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&lt;pair&lt;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;
    

    }