Submission #813077

#TimeUsernameProblemLanguageResultExecution timeMemory
813077andrei_boacaThousands Islands (IOI22_islands)C++17
10 / 100
100 ms29052 KiB
#include "islands.h"

#include <variant>
#include <vector>
#include <algorithm>
#include <assert.h>
#include <map>
//#include <bits/stdc++.h>
//#include "grader.cpp"
using namespace std;
int n,m;
map<int,vector<int>> c[100005];
vector<int> muchii[100005];
vector<int> sol;
void n2(int x,int y)
{
    int A=c[1][0][0];
    int B=c[0][1][0];
    int C=c[0][1][1];
    vector<int> aux={B,A,C,B,A,C};
    for(int i:aux)
        sol.push_back(i);
}
bool use[100005];
vector<int> lant;
bool good;
bool inst[100005];
int from[100005];
int who;
void s1(int nod)
{
    if(good)
        return;
    use[nod]=1;
    inst[nod]=1;
    for(int i:muchii[nod])
    {
        if(!inst[i])
        {
            if(c[nod][i].size()>=2&&c[i][nod].size()>=1)
            {
                who=i;
                good=1;
                int x=nod;
                while(true)
                {
                    lant.push_back(x);
                    if(x==0)
                        break;
                    x=from[x];
                }
                return;
            }
            if(!use[i])
                s1(i);
        }
    }
}
void build(int x,int y,int z)
{
    assert(x>=0&&y>=0&&z>=0);
    int A=c[x][y][0];
    int B=c[y][x][0];
    int C=c[x][z][0];
    int D=c[z][x][0];
    if(y==z)
    {
        C=c[x][z][1];
        D=c[z][x][1];
    }
    vector<int> aux={A,B,C,D,B,A,D,C};
    for(int i:aux)
        sol.push_back(i);
}
int A,B;
void s2(int nod)
{
    if(good)
        return;
    use[nod]=1;
    inst[nod]=1;
    vector<int> dub;
    for(int i:muchii[nod])
    {
        if(!inst[i])
        {
            for(int j=1;dub.size()<2&&j<=min(c[nod][i].size(),c[i][nod].size());j++)
                dub.push_back(i);
        }
    }
    if(dub.size()>=2)
    {
        good=1;
        A=dub[0];
        B=dub[1];
        int x=nod;
        while(true)
        {
            lant.push_back(x);
            if(x==0)
                break;
            x=from[x];
        }
        return;
    }
    for(int i:muchii[nod])
    {
        if(good)
            return;
        if(!use[i])
            s2(i);
    }
}

std::variant<bool, std::vector<int>> find_journey(int N, int M, std::vector<int> U, std::vector<int> V)
{
    n=N;
    m=M;
    for(int i=0;i<m;i++)
    {
        int a=U[i],b=V[i];
        c[a][b].push_back(i);
        if(c[a][b].size()==1)
            muchii[a].push_back(b);
    }
    s1(0);
    if(good)
    {
        reverse(lant.begin(),lant.end());
        for(int i=1;i<lant.size();i++)
            sol.push_back(c[lant[i-1]][lant[i]][0]);
        n2(lant.back(),who);
        reverse(lant.begin(),lant.end());
        for(int i=1;i<lant.size();i++)
            sol.push_back(c[lant[i]][lant[i-1]][0]);
        return sol;
    }
    for(int i=0;i<n;i++)
    {
        use[i]=0;
        inst[i]=0;
    }
    lant.clear();
    s2(0);
    if(good)
    {
        reverse(lant.begin(),lant.end());
        for(int i=1;i<lant.size();i++)
            sol.push_back(c[lant[i-1]][lant[i]][0]);
        build(lant.back(),A,B);
        reverse(lant.begin(),lant.end());
        for(int i=1;i<lant.size();i++)
            sol.push_back(c[lant[i]][lant[i-1]][0]);
        return sol;
    }
    return false;
}

Compilation message (stderr)

islands.cpp: In function 'void s2(int)':
islands.cpp:87:40: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   87 |             for(int j=1;dub.size()<2&&j<=min(c[nod][i].size(),c[i][nod].size());j++)
      |                                       ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
islands.cpp: In function 'std::variant<bool, std::vector<int, std::allocator<int> > > find_journey(int, int, std::vector<int>, std::vector<int>)':
islands.cpp:130:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |         for(int i=1;i<lant.size();i++)
      |                     ~^~~~~~~~~~~~
islands.cpp:134:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |         for(int i=1;i<lant.size();i++)
      |                     ~^~~~~~~~~~~~
islands.cpp:148:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |         for(int i=1;i<lant.size();i++)
      |                     ~^~~~~~~~~~~~
islands.cpp:152:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |         for(int i=1;i<lant.size();i++)
      |                     ~^~~~~~~~~~~~
#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...