Submission #826789

#TimeUsernameProblemLanguageResultExecution timeMemory
826789AmylopectinThousands Islands (IOI22_islands)C++17
31 / 100
83 ms82656 KiB
#include "islands.h"
#include <stdio.h>
#include <variant>
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int mxn = 1e6 + 10,mxm = 1e6 + 10;
struct we
{
  int to,idx;
};
vector<struct we> pat[mxn] = {},rpat[mxn] = {};
vector<int> lii[mxn] = {},ans,stc;
queue<int> qu;
int n,m,u[mxn] = {},ou[mxn] = {},scc[mxn] = {},cou,sru,nmem[mxn] = {},fon[mxn] = {}
,tog[mxn] = {},upa[mxm] = {},of,cup[mxn] = {},cuu[mxn] = {},cue[mxm] = {};
int setu(int l)
{
  int i,j;
  for(i=0; i<n; i++)
  {
    u[i] = 0;
  }
  return 0;
}
int re(int cn)
{
  int i,j,fn;
  u[cn] = 1;
  for(i=0; i<pat[cn].size(); i++)
  {
    fn = pat[cn][i].to;
    if(u[fn] == 0)
    {
      re(fn);
    }
  }
  ou[cou] = cn;
  cou ++;
  return 0;
}
int re2(int cn)
{
  int i,j,fn;
  scc[cn] = sru;
  nmem[sru] ++;
  for(i=0; i<rpat[cn].size(); i++)
  {
    fn = rpat[cn][i].to;
    if(scc[fn] == 0)
    {
      re2(fn);
    }
  }
  return 0;
}
int re3(int cn,int v)
{
  int i,j,fn,fid;
  if(nmem[scc[cn]] > 1)
  {
    of = 1;
    fon[v] = cn;
    return 0;
  }
  for(i=0; i<pat[cn].size(); i++)
  {
    fn = pat[cn][i].to;
    fid = pat[cn][i].idx;
    if(u[fn] == -1 || cue[fid] == 1)
    {
      continue;
    }
    lii[v].push_back(pat[cn][i].idx);
    re3(fn,v);
    if(of == 0)
    {
      u[fn] = -1;
      lii[v].pop_back();
    }
    else 
    {
      break;
    }
  }
  return 0;
}
int re4(int cn,int v,int tar)
{
  int i,j,fn,fid;
  u[cn] = 1;
  for(i=0; i<pat[cn].size(); i++)
  {
    fn = pat[cn][i].to;
    fid = pat[cn][i].idx;
    if(scc[cn] != scc[fn] || upa[fid] == 1 || upa[fid] == 2 || cue[fid] == 1 || u[fn] == 1)
    {
      continue;
    }
    upa[fid] = 1;
    lii[v].push_back(fid);
    if(fn == tar)
    {
      upa[fid] = 2;
      of = 1;
      return 0;
    }
    re4(fn,v,tar);
    if(of == 0)
    {
      upa[fid] = 0;
      lii[v].pop_back();
    }
    else 
    {
      upa[fid] = 2;
      break;
    }
  }
  u[cn] = 0;
  return 0;
}
int re5(int cn,int v)
{
  int i,j,fn,fid;
  u[cn] = 1;
  for(i=0; i<pat[cn].size(); i++)
  {
    fn = pat[cn][i].to;
    fid = pat[cn][i].idx;
    if(scc[cn] != scc[fn] || upa[fid] == 1 || cue[fid] == 1 || u[fn] == 1)
    {
      continue;
    }
    if(upa[fid] == 2)
    {
      fon[v] = fid;
      of = 1;
      return 0;
    }
    upa[fid] = 1;
    lii[v].push_back(fid);
    re5(fn,v);
    if(of == 0)
    {
      upa[fid] = 0;
      lii[v].pop_back();
    }
    else 
    {
      upa[fid] = 2;
      break;
    }
  }
  u[cn] = 0;
  return 0;
}
std::variant<bool, std::vector<int>> find_journey(int nn, int mm
, std::vector<int> uu, std::vector<int> vv) 
{
  // return false;
  int i,j,cn,cm,fn,fm,cru = 0,fou = 0,cst,ccou;
  n = nn;
  m = mm;
  for(i=0; i<m; i++)
  {
    pat[uu[i]].push_back({vv[i],i});
    rpat[vv[i]].push_back({uu[i],i});
  }
  cou = 0;
  re(0);
  sru = 1;
  for(i=cou-1; i>=0; i--)
  {
    if(scc[ou[i]] == 0)
    {
      re2(ou[i]);
      sru ++;
    }
  }
  for(i=0; i<n; i++)
  {
    if(pat[i].size() == 0)
    {
      qu.push(i);
    }
  }
  while(!qu.empty())
  {
    cn = qu.front();
    qu.pop();
    cuu[cn] = 1;
    for(i=0; i<rpat[cn].size(); i++)
    {
      fn = rpat[cn][i].to;
      cup[fn] ++;
      if(cup[fn] == pat[fn].size())
      {
        qu.push(fn);
      }
    }
  }
  cst = 0;
  // pat[cst].size() - cup[cst] == 1
  while(1)
  {
    ccou = 0;
    for(i=0; i<pat[cst].size(); i++)
    {
      fn = pat[cst][i].to;
      if(cuu[fn] == 0)
      {
        ccou ++;
      }
    }
    if(ccou != 1)
    {
      break;
    }
    for(i=0; i<pat[cst].size(); i++)
    {
      fn = pat[cst][i].to;
      if(cuu[fn] == 0)
      {
        cue[pat[cst][i].idx] = 1;
        cuu[cst] = 1;
        stc.push_back(pat[cst][i].idx);
        cst = fn;
        break;
      }
    }
  }
  // printf("\n");
  // for(i=0; i<n; i++)
  // {
  //   if(cuu[i] == 0)
  //   for(j=0; j<pat[i].size(); j++)
  //   {
  //     fn = pat[i][j].to;
  //     if(cuu[fn] == 0 && scc[i] == scc[fn] && scc[i] == 2)
  //     {
  //       printf("%d %d\n",i,fn);
  //     }
  //   }
  // }
  // for(i=0; i<n; i++)
  // {
  //   ans.push_back(scc[i]);
  // }
  // return ans;
  for(i=0; i<pat[cst].size(); i++)
  {
    cn = pat[cst][i].to;
    if(cuu[cn] == 1)
    {
      continue;
    }
    of = 0;
    lii[fou].push_back(pat[cst][i].idx);
    if(scc[cn] == scc[cst])
    {
      fon[fou] = cn;
      tog[fou] = 1;
      fou ++;
      if(fou == 2)
      {
        break;
      }
      continue;
    }
    of = 0;
    re3(cn,fou);
    if(of == 0)
    {
      lii[fou].pop_back();
    }
    else 
    {
      fou ++;
      if(fou == 2)
      {
        break;
      }
    }
  }
  if(fou < 2)
  {
    return false;
  }
  for(i=0; i<stc.size(); i++)
  {
    ans.push_back(stc[i]);
  }
  // printf("\n%d %d \n",fon[0],fon[1]);
  if(tog[0] == tog[1] && tog[0] == 1)
  {
    of = 0;
    setu(0);
    upa[lii[0][0]] = 1;
    lii[2].push_back(lii[0][0]);
    re4(fon[0],2,cst);
    if(of == 0)
    {
      return false;
    }
    upa[lii[0][0]] = 2;
    of = 0;
    upa[lii[1][0]] = 1;
    lii[3].push_back(lii[1][0]);
    setu(0);
    re5(fon[1],3);
    if(of == 0)
    {
      return false;
    }
    upa[lii[1][0]] = 2;
    for(i=0; i<lii[2].size(); i++)
    {
      if(fon[3] == lii[2][i])
      {
        cn = i-1;
      }
      ans.push_back(lii[2][i]);
    }
    for(i=0; i<lii[3].size(); i++)
    {
      ans.push_back(lii[3][i]);
    }
    for(i=cn; i>=0; i--)
    {
      ans.push_back(lii[2][i]);
    }
    for(i=lii[2].size()-1; i>cn; i--)
    {
      ans.push_back(lii[2][i]);
    }
    for(i=lii[3].size()-1; i>=0; i--)
    {
      ans.push_back(lii[3][i]);
    }
  }
  else if(scc[fon[0]] == scc[fon[1]])
  {
    if(fon[0] == fon[1])
    {
      of = 0;
      setu(0);
      re4(fon[0],2,fon[0]);
      if(of == 0)
    {
      return false;
    }
      for(i=0; i<lii[0].size(); i++)
      {
        ans.push_back(lii[0][i]);
      }
      for(i=0; i<lii[2].size(); i++)
      {
        ans.push_back(lii[2][i]);
      }
      for(i=lii[0].size()-1; i>=0; i--)
      {
        ans.push_back(lii[0][i]);
      }
      for(i=0; i<lii[1].size(); i++)
      {
        ans.push_back(lii[1][i]);
      }
      for(i=lii[2].size()-1; i>=0; i--)
      {
        ans.push_back(lii[2][i]);
      }
      for(i=lii[1].size()-1; i>=0; i--)
      {
        ans.push_back(lii[1][i]);
      }
    }
    else 
    {
      of = 0;
      setu(0);
      re4(fon[0],2,fon[1]);
      if(of == 0)
    {
      return false;
    }
      of = 0;
      setu(0);
      re4(fon[1],3,fon[0]);
      if(of == 0)
    {
      return false;
    }
      for(i=0; i<lii[0].size(); i++)
      {
        ans.push_back(lii[0][i]);
      }
      for(i=0; i<lii[2].size(); i++)
      {
        ans.push_back(lii[2][i]);
      }
      for(i=0; i<lii[3].size(); i++)
      {
        ans.push_back(lii[3][i]);
      }
      for(i=lii[0].size()-1; i>=0; i--)
      {
        ans.push_back(lii[0][i]);
      }
      for(i=0; i<lii[1].size(); i++)
      {
        ans.push_back(lii[1][i]);
      }
      for(i=lii[2].size()-1; i>=0; i--)
      {
        ans.push_back(lii[2][i]);
      }
      for(i=lii[3].size()-1; i>=0; i--)
      {
        ans.push_back(lii[3][i]);
      }
      for(i=lii[1].size()-1; i>=0; i--)
      {
        ans.push_back(lii[1][i]);
      }
    }
  }
  else 
  {
    if(tog[0] == 1)
    {
      of = 0;
      setu(0);
      re4(cst,2,cst);
      lii[0].clear();
    }
    else 
    {
      of = 0;
      setu(0);
      re4(fon[0],2,fon[0]);
    }
    if(of == 0)
    {
      return false;
    }
    if(tog[1] == 1)
    {
      of = 0;
      setu(0);
      re4(cst,3,cst);
      lii[1].clear();
    }
    else 
    {
      of = 0;
      setu(0);
      re4(fon[1],3,fon[1]);
    }
    if(of == 0)
    {
      return false;
    }
    for(i=0; i<lii[0].size(); i++)
      {
        ans.push_back(lii[0][i]);
      }
      for(i=0; i<lii[2].size(); i++)
      {
        ans.push_back(lii[2][i]);
      }
      for(i=lii[0].size()-1; i>=0; i--)
      {
        ans.push_back(lii[0][i]);
      }
      for(i=0; i<lii[1].size(); i++)
      {
        ans.push_back(lii[1][i]);
      }
      for(i=0; i<lii[3].size(); i++)
      {
        ans.push_back(lii[3][i]);
      }
      for(i=lii[1].size()-1; i>=0; i--)
      {
        ans.push_back(lii[1][i]);
      }

      for(i=0; i<lii[0].size(); i++)
      {
        ans.push_back(lii[0][i]);
      }
      for(i=lii[2].size()-1; i>=0; i--)
      {
        ans.push_back(lii[2][i]);
      }
      for(i=lii[0].size()-1; i>=0; i--)
      {
        ans.push_back(lii[0][i]);
      }
      for(i=0; i<lii[1].size(); i++)
      {
        ans.push_back(lii[1][i]);
      }
      for(i=lii[3].size()-1; i>=0; i--)
      {
        ans.push_back(lii[3][i]);
      }
      for(i=lii[1].size()-1; i>=0; i--)
      {
        ans.push_back(lii[1][i]);
      }
  }
  for(i=stc.size()-1; i>=0; i--)
  {
    ans.push_back(stc[i]);
  }
  return ans;
}

Compilation message (stderr)

islands.cpp: In function 'int setu(int)':
islands.cpp:21:9: warning: unused variable 'j' [-Wunused-variable]
   21 |   int i,j;
      |         ^
islands.cpp: In function 'int re(int)':
islands.cpp:32:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |   for(i=0; i<pat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~
islands.cpp:30:9: warning: unused variable 'j' [-Wunused-variable]
   30 |   int i,j,fn;
      |         ^
islands.cpp: In function 'int re2(int)':
islands.cpp:49:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   for(i=0; i<rpat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~~
islands.cpp:46:9: warning: unused variable 'j' [-Wunused-variable]
   46 |   int i,j,fn;
      |         ^
islands.cpp: In function 'int re3(int, int)':
islands.cpp:68:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |   for(i=0; i<pat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~
islands.cpp:61:9: warning: unused variable 'j' [-Wunused-variable]
   61 |   int i,j,fn,fid;
      |         ^
islands.cpp: In function 'int re4(int, int, int)':
islands.cpp:94:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |   for(i=0; i<pat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~
islands.cpp:92:9: warning: unused variable 'j' [-Wunused-variable]
   92 |   int i,j,fn,fid;
      |         ^
islands.cpp: In function 'int re5(int, int)':
islands.cpp:129:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |   for(i=0; i<pat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~
islands.cpp:127:9: warning: unused variable 'j' [-Wunused-variable]
  127 |   int i,j,fn,fid;
      |         ^
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:195:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  195 |     for(i=0; i<rpat[cn].size(); i++)
      |              ~^~~~~~~~~~~~~~~~
islands.cpp:199:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  199 |       if(cup[fn] == pat[fn].size())
      |          ~~~~~~~~^~~~~~~~~~~~~~~~~
islands.cpp:210:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  210 |     for(i=0; i<pat[cst].size(); i++)
      |              ~^~~~~~~~~~~~~~~~
islands.cpp:222:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  222 |     for(i=0; i<pat[cst].size(); i++)
      |              ~^~~~~~~~~~~~~~~~
islands.cpp:253:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  253 |   for(i=0; i<pat[cst].size(); i++)
      |            ~^~~~~~~~~~~~~~~~
islands.cpp:292:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  292 |   for(i=0; i<stc.size(); i++)
      |            ~^~~~~~~~~~~
islands.cpp:319:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  319 |     for(i=0; i<lii[2].size(); i++)
      |              ~^~~~~~~~~~~~~~
islands.cpp:327:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  327 |     for(i=0; i<lii[3].size(); i++)
      |              ~^~~~~~~~~~~~~~
islands.cpp:355:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  355 |       for(i=0; i<lii[0].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:359:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  359 |       for(i=0; i<lii[2].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:367:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  367 |       for(i=0; i<lii[1].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:396:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  396 |       for(i=0; i<lii[0].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:400:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  400 |       for(i=0; i<lii[2].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:404:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  404 |       for(i=0; i<lii[3].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:412:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  412 |       for(i=0; i<lii[1].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:466:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  466 |     for(i=0; i<lii[0].size(); i++)
      |              ~^~~~~~~~~~~~~~
islands.cpp:470:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  470 |       for(i=0; i<lii[2].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:478:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  478 |       for(i=0; i<lii[1].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:482:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  482 |       for(i=0; i<lii[3].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:491:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  491 |       for(i=0; i<lii[0].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:503:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  503 |       for(i=0; i<lii[1].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:164:9: warning: unused variable 'j' [-Wunused-variable]
  164 |   int i,j,cn,cm,fn,fm,cru = 0,fou = 0,cst,ccou;
      |         ^
islands.cpp:164:14: warning: unused variable 'cm' [-Wunused-variable]
  164 |   int i,j,cn,cm,fn,fm,cru = 0,fou = 0,cst,ccou;
      |              ^~
islands.cpp:164:20: warning: unused variable 'fm' [-Wunused-variable]
  164 |   int i,j,cn,cm,fn,fm,cru = 0,fou = 0,cst,ccou;
      |                    ^~
islands.cpp:164:23: warning: unused variable 'cru' [-Wunused-variable]
  164 |   int i,j,cn,cm,fn,fm,cru = 0,fou = 0,cst,ccou;
      |                       ^~~
#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...