제출 #1350193

#제출 시각아이디문제언어결과실행 시간메모리
1350193alexddTable Tennis (JOI24_tabletennis)C++20
4 / 100
1095 ms1114112 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int INF = 1e12;

int comb2(int x)
{
    return x * (x-1) / 2;
}

int n,m;


bool verif(vector<int> v)
{
    int pref = 0;
    for(int i=1;i<=n;i++)
    {
        if(i > 1 && v[i-1] > v[i])
            return 0;
        pref += v[i];
        if(pref < comb2(i))
            return 0;
    }
    if(pref != comb2(n))
        return 0;
    return 1;
}

int calc(vector<int> v)
{
    int tot = 0;
    for(int i=1;i<=n;i++)
        tot += comb2(v[i]);
    return tot;
}

bool done;
vector<int> sol;
map<vector<int>, bool> visited;

void recursiv(vector<int> v)
{
    if(done)
        return;
    if(visited[v])
        return;
    visited[v] = 1;

    if(calc(v) == m)
    {
        done = 1;
        sol = v;
        return;
    }

    int curdist = abs(calc(v) - m);

    vector<pair<int,vector<int>>> ord;
    for(int from=2;from<=n;from++)
    {
        for(int to=1;to<=n;to++)
        {
            if(from == to)
                continue;

            vector<int> newv = v;
            newv[from]--;
            newv[to]++;
            if(newv[to] < n && newv[from] >= 0 && verif(newv))
            {
                int mydist = abs(calc(newv) - m);

                if(mydist < curdist)
                    ord.push_back({mydist, newv});
            }
        }
    }
    sort(ord.begin(), ord.end());
    for(auto [_,newv]:ord)
        recursiv(newv);
}

int mat[5005][5005];
void reconstruct(vector<int> v)
{
    assert(verif(v));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            mat[i][j] = 0;

    vector<pair<int,int>> aux;
    for(int i=1;i<=n;i++)
        aux.push_back({v[i], i});
    while(!aux.empty())
    {
        sort(aux.begin(), aux.end());
        if(aux.back().first == 0)
            break;
        assert(aux.back().first > 0);

        for(int i=0;i<aux.back().first;i++)
        {
            mat[aux.back().second][aux[i].second] = 1;
        }
        for(int i=aux.back().first;i<(int)aux.size()-1;i++)
        {
            aux[i].first--;
            mat[aux[i].second][aux.back().second] = 1;
        }
        aux.back().first = 0;
        aux.pop_back();
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            assert(mat[i][j] + mat[j][i] == 1);
            if(mat[i][j] + mat[j][i] != 1)
            {
                //cerr<<i<<" "<<j<<": "<<mat[i][j]<<" & "<<mat[j][i]<<" zzz\n";
            }
        }
    }

    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<i;j++)
            cout<<mat[i][j];
        cout<<"\n";
    }
}

void solve()
{
    cin>>n>>m;
    m = n * (n-1) * (n-2) / 6 - m;

    //get close to m then do backtrack
    done = 0;
    visited.clear();

    vector<int> init(n+2,0);
    for(int i=1;i<=n;i++)
        init[i] = i - 1;

    assert(verif(init));

    if(m > calc(init))
    {
        cout<<"No\n";
        return;
    }

    recursiv(init);

    if(!done)
    {
        cout<<"No\n";
        return;
    }

    assert(calc(sol) == m);

    //for(int i=1;i<=n;i++) cerr<<sol[i]<<" ";cerr<<"sol\n";

    cout<<"Yes\n";
    reconstruct(sol);
}

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t;
    cin>>t;
    while(t--)
        solve();
    return 0;
}

/*

for each "bad" tuple (x,y,z) of nodes,
exactly one of x,y,z will have outgoing edges towards the other 2
=> M = total - sum(comb(cnt_outgoing(x), 2))

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...