Submission #1190730

#TimeUsernameProblemLanguageResultExecution timeMemory
1190730AmrThousands Islands (IOI22_islands)C++20
1.75 / 100
1074 ms1148064 KiB
#include "islands.h"

#include <variant>
#include <vector>

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define F first
#define S second
#define sz size()
const int W =2e5+2;
vector<pair<ll,ll> > vec,vec2;
pair<ll,ll> p[W];
vector<pair<ll,ll> > v[W];
ll vis[W];
void dfs(ll x, ll in)
{
    vis[x] = 1;
    for(int i = 0; i < v[x].sz; i++)
    {
        ll in2 = v[x][i].S;
        ll newn = v[x][i].F;
        if(in%2==0)
        {
            if(in2-in==1) continue;
        }
        else
        {
            if(in-in2==1) continue;
        }
        if(vis[newn])
        {
            if(vec.sz) continue;

            ll y = x;
            vec.push_back({newn,in2});
            while(y!=newn){ vec.push_back(p[y]); y = p[y].F;}
            while(y!=0)   { vec2.push_back(p[y]); y = p[y].F;}

        }
        p[newn] = {x,in2};
        dfs(newn,in2);
    }
}
ll d(ll x)
{
    if(x%2) x--; else x++;
    return x;
}
std::variant<bool, std::vector<int>> find_journey( int N, int M, std::vector<int> U, std::vector<int> V) {

    for(int i = 0; i < M; i++)
    {
        ll x = U[i] , y = V[i];
        v[x].push_back({y,i});
    }
    dfs(0,-1);
    if(vec.sz==0) return false;
    // for(int i = 0; i < v[x].sz; i++) cout << vec[i].F << " " << vec[i].S << endl;
    vector<int> ans;
    for(int i = vec2.sz-1; i >= 0; i--) ans.push_back(vec2[i].S);
    for(int i = vec.sz-1; i>= 0; i--) ans.push_back(vec[i].S);
    for(int i = 0; i < vec.sz; i--) ans.push_back(d(vec[i].S));
    for(int i = 0; i < vec.sz; i--) ans.push_back(d(vec[i].S));
    for(int i = vec.sz-1; i>= 0; i--) ans.push_back(vec[i].S);
    for(int i = 0; i < vec2.sz; i++) ans.push_back(d(vec2[i].S));
    return ans;

}
#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...