Submission #7290397


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

template <class T>
inline bool read(T &ret)
{
    char c;
    int sgn;
    if (c = getchar(), c == EOF)
    {
        return 0;
    }
    while (c != '-' && (c < '0' || c > '9'))
    {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0');
    }
    ret *= sgn;
    return 1;
}

template <class T>
inline void write(T x)
{
    if (x > 9)
    {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

const int MAXN=1010;
int n,ans;
bool vised[MAXN];
queue<int> que[MAXN];

int main()
{
    read(n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<n;j++)
        {
            int sr;
            read(sr);
            que[i].push(sr);
        }
    }
    ans=0;
    while(true)
    {
        for(int i=1;i<=n;i++)
            vised[i]=false;
        bool pd=false;
        for(int i=1;i<=n;i++)
        {
            if(!que[i].empty()&&!vised[i])
            {
                int cc=que[i].front();
                if(!vised[cc]&&i==que[cc].front())
                {
                    que[i].pop();
                    que[cc].pop();
                    vised[i]=true;
                    vised[cc]=true;
                    pd=true;
//                    cout<<i<<" "<<cc<<" "<<ans<<endl;
                }
            }
        }
        if(!pd)
            break;
        ans++;
    }
    for(int i=1;i<=n;i++)
        if(!que[i].empty())
        {
            cout<<"-1"<<endl;
            return 0;
        }
    cout<<ans<<endl;
}

Submission Info

Submission Time
Task E - League
User Setsuge
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1718 Byte
Status AC
Exec Time 1937 ms
Memory 6784 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 23
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23
Case Name Status Exec Time Memory
a01 AC 2 ms 896 KB
a02 AC 2 ms 896 KB
a03 AC 2 ms 896 KB
b04 AC 2 ms 896 KB
b05 AC 2 ms 896 KB
b06 AC 2 ms 896 KB
b07 AC 2 ms 896 KB
b08 AC 1937 ms 6784 KB
b09 AC 35 ms 4736 KB
b10 AC 56 ms 4736 KB
b11 AC 56 ms 4736 KB
b12 AC 14 ms 2048 KB
b13 AC 13 ms 1664 KB
b14 AC 7 ms 1280 KB
b15 AC 7 ms 1280 KB
b16 AC 4 ms 1024 KB
b17 AC 5 ms 1024 KB
b18 AC 14 ms 1536 KB
b19 AC 11 ms 1536 KB
b20 AC 4 ms 1024 KB
b21 AC 4 ms 1024 KB
b22 AC 2 ms 1024 KB
b23 AC 2 ms 896 KB