Submission #826756

#TimeUsernameProblemLanguageResultExecution timeMemory
826756AmylopectinThousands Islands (IOI22_islands)C++17
31 / 100
77 ms82696 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; 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] = {},in[mxn] = {},scc[mxn] = {},cou,sru,nmem[mxn] = {},fon[mxn] = {} ,tog[mxn] = {},upa[mxn] = {},of,cup[mxn] = {},cuu[mxn] = {},cue[mxn] = {}; int re(int cn) { int i,j,fn; u[cn] = 1; in[cou] = cn; cou ++; for(i=0; i<pat[cn].size(); i++) { fn = pat[cn][i].to; if(u[fn] == 0) { re(fn); } } 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; 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) { 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; } } return 0; } int re5(int cn,int v) { int i,j,fn,fid; 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) { continue; } if(upa[fid] == 2) { fon[3] = 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; } } 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=0; i<cou; i++) { if(scc[in[i]] == 0) { re2(in[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; } } } // 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; 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]); 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; 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; re4(fon[0],2,fon[1]); if(of == 0) { return false; } of = 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; re4(cst,2,cst); lii[0].clear(); } else { of = 0; re4(fon[0],2,fon[0]); } if(of == 0) { return false; } if(tog[1] == 1) { of = 0; re4(cst,3,cst); lii[1].clear(); } else { of = 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 re(int)':
islands.cpp:25:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |   for(i=0; i<pat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~
islands.cpp:21:9: warning: unused variable 'j' [-Wunused-variable]
   21 |   int i,j,fn;
      |         ^
islands.cpp: In function 'int re2(int)':
islands.cpp:40:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |   for(i=0; i<rpat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~~
islands.cpp:37:9: warning: unused variable 'j' [-Wunused-variable]
   37 |   int i,j,fn;
      |         ^
islands.cpp: In function 'int re3(int, int)':
islands.cpp:59:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for(i=0; i<pat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~
islands.cpp:52:9: warning: unused variable 'j' [-Wunused-variable]
   52 |   int i,j,fn,fid;
      |         ^
islands.cpp: In function 'int re4(int, int, int)':
islands.cpp:84:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |   for(i=0; i<pat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~
islands.cpp:83:9: warning: unused variable 'j' [-Wunused-variable]
   83 |   int i,j,fn,fid;
      |         ^
islands.cpp: In function 'int re5(int, int)':
islands.cpp:117:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |   for(i=0; i<pat[cn].size(); i++)
      |            ~^~~~~~~~~~~~~~~
islands.cpp:116:9: warning: unused variable 'j' [-Wunused-variable]
  116 |   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:182:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  182 |     for(i=0; i<rpat[cn].size(); i++)
      |              ~^~~~~~~~~~~~~~~~
islands.cpp:186:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  186 |       if(cup[fn] == pat[fn].size())
      |          ~~~~~~~~^~~~~~~~~~~~~~~~~
islands.cpp:197:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  197 |     for(i=0; i<pat[cst].size(); i++)
      |              ~^~~~~~~~~~~~~~~~
islands.cpp:209:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  209 |     for(i=0; i<pat[cst].size(); i++)
      |              ~^~~~~~~~~~~~~~~~
islands.cpp:227:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<we>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  227 |   for(i=0; i<pat[cst].size(); i++)
      |            ~^~~~~~~~~~~~~~~~
islands.cpp:266:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  266 |   for(i=0; i<stc.size(); i++)
      |            ~^~~~~~~~~~~
islands.cpp:291:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  291 |     for(i=0; i<lii[2].size(); i++)
      |              ~^~~~~~~~~~~~~~
islands.cpp:299:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  299 |     for(i=0; i<lii[3].size(); i++)
      |              ~^~~~~~~~~~~~~~
islands.cpp:326:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  326 |       for(i=0; i<lii[0].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:330:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  330 |       for(i=0; i<lii[2].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:338:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  338 |       for(i=0; i<lii[1].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:365:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  365 |       for(i=0; i<lii[0].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:369:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  369 |       for(i=0; i<lii[2].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:373:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  373 |       for(i=0; i<lii[3].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:381:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  381 |       for(i=0; i<lii[1].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:431:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  431 |     for(i=0; i<lii[0].size(); i++)
      |              ~^~~~~~~~~~~~~~
islands.cpp:435:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  435 |       for(i=0; i<lii[2].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:443:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  443 |       for(i=0; i<lii[1].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:447:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  447 |       for(i=0; i<lii[3].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:456:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  456 |       for(i=0; i<lii[0].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:468:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  468 |       for(i=0; i<lii[1].size(); i++)
      |                ~^~~~~~~~~~~~~~
islands.cpp:151:9: warning: unused variable 'j' [-Wunused-variable]
  151 |   int i,j,cn,cm,fn,fm,cru = 0,fou = 0,cst,ccou;
      |         ^
islands.cpp:151:14: warning: unused variable 'cm' [-Wunused-variable]
  151 |   int i,j,cn,cm,fn,fm,cru = 0,fou = 0,cst,ccou;
      |              ^~
islands.cpp:151:20: warning: unused variable 'fm' [-Wunused-variable]
  151 |   int i,j,cn,cm,fn,fm,cru = 0,fou = 0,cst,ccou;
      |                    ^~
islands.cpp:151:23: warning: unused variable 'cru' [-Wunused-variable]
  151 |   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...