Submission #840067

# Submission time Handle Problem Language Result Execution time Memory
840067 2023-08-31T04:35:31 Z Fysty Longest Trip (IOI23_longesttrip) C++17
15 / 100
1000 ms 380 KB
#include <bits/stdc++.h>
#include <random>
#include <chrono>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);

const int MOD1=1e9+7;
const int MOD2=998244353;
const ll INF=3e18;

ll fpow(ll a,ll b,ll m)
{
    if(!b) return 1;
    ll tmp=1;
    for(ll cur=a;b;b>>=1,cur=cur*cur%m) if(b&1) tmp=tmp*cur%m;
    return tmp;
}
ll inv(ll a,ll m) {return fpow(a,m-2,m);}

#define MottoHayaku ios::sync_with_stdio(false);cin.tie(0);
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define uni(c) c.resize(distance(c.begin(),unique(c.begin(),c.end())))
#define unisort(c) sort(c.begin(),c.end()),uni(c)

#include "longesttrip.h"

bool has[300][300];
//vector<int> ed[300];
bool vis[300];
int par[300];
int deg[300],cnt[300];
int n;

void dfs(int u)
{
    vis[u]=1;
    rep(v,n) if(has[u][v]&&!vis[v])
    {
        par[v]=u;
        cnt[u]++;
        dfs(v);
    }
}

void dfs2(int u,vector<int> &ret)
{
    vis[u]=1;
    ret.pb(u);
    rep(v,n) if(has[u][v]&&!vis[v]) dfs2(v,ret);
}



/*bool db[300][300];

bool are_connected(vector<int> a,vector<int> b)
{
    bool ret=0;
    for(int x:a) for(int y:b)
    {
        ret|=db[x][y];
    }
    return ret;
}*/

vector<int> longest_trip(int N,int D)
{
    n=N;
    rep(i,n)
    {
        rep(j,n)
        {
            has[i][j]=0;
        }
        //ed[i].clear();
        vis[i]=0;
        par[i]=-1;
        deg[i]=cnt[i]=0;
    }
    vector<int> ret;
    if(D==3)
    {
        rep(i,n) ret.pb(i);
        return ret;
    }
    rep(i,n) rep(j,i)
    {
        bool res=are_connected({i},{j});
        if(res)
        {
            //ed[i].pb(j);
            //ed[j].pb(i);
            has[i][j]=has[j][i]=1;
            deg[i]++,deg[j]++;
        }
    }
    par[0]=-1;
    dfs(0);
    vector<int> a,b;
    rep(i,n)
    {
        if(vis[i]) a.pb(i);
        else b.pb(i);
    }
    if(!b.empty())
    {
        if(a.size()>b.size()) return a;
        else return b;
    }
    int r=-1;
    rep(i,n)
    {
        vis[i]=0;
        if(deg[i]==1) r=i;
    }
    if(r!=-1)
    {
        dfs2(r,ret);
        return ret;
    }
    vector<int> tmp;
    int x;
    rep(i,n)
    {
        if(cnt[i]==0) tmp.pb(i);
        if(cnt[i]==2) x=i;
    }
    if(tmp.size()==1)
    {
        int cur=tmp[0];
        while(cur!=-1)
        {
            ret.pb(cur);
            cur=par[cur];
        }
        return ret;
    }
    vector<int> ret2;
    int cur=tmp[0];
    while(cur!=par[x])
    {
        ret.pb(cur);
        cur=par[cur];
    }
    cur=tmp[1];
    while(cur!=x)
    {
        ret2.pb(cur);
        cur=par[cur];
    }
    reverse(ret2.begin(),ret2.end());
    for(int v:ret2) ret.pb(v);
    if(x==0) return ret;

    vector<int> ret3;
    cur=par[x];
    while(cur!=-1)
    {
        ret3.pb(cur);
        cur=par[cur];
    }
    if(has[par[x]][tmp[0]])
    {
        reverse(ret.begin(),ret.end());
    }
    for(int v:ret3) ret.pb(v);
    return ret;
}

/*
void solve()
{
    int N,D;
    cin>>N>>D;
    rep(i,N)
    {
        rep(j,N) cin>>db[i][j];
    }
    auto ans=longest_trip(N,D);
    for(int x:ans) cout<<x<<" ";
}

signed main()
{
    MottoHayaku

    int t;
    //cin>>t;
    t=1;
    while(t--)
    {
        solve();
    }
}*/

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:172:14: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  172 |     cur=par[x];
      |         ~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 220 ms 368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 22 ms 208 KB Output is correct
3 Correct 148 ms 208 KB Output is correct
4 Correct 518 ms 336 KB Output is correct
5 Correct 902 ms 368 KB Output is correct
6 Correct 12 ms 208 KB Output is correct
7 Correct 30 ms 208 KB Output is correct
8 Correct 134 ms 208 KB Output is correct
9 Correct 346 ms 336 KB Output is correct
10 Correct 902 ms 368 KB Output is correct
11 Correct 825 ms 364 KB Output is correct
12 Correct 693 ms 336 KB Output is correct
13 Correct 862 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 208 KB Output is correct
2 Correct 31 ms 208 KB Output is correct
3 Correct 151 ms 208 KB Output is correct
4 Correct 439 ms 336 KB Output is correct
5 Correct 892 ms 368 KB Output is correct
6 Correct 12 ms 208 KB Output is correct
7 Correct 31 ms 208 KB Output is correct
8 Correct 165 ms 208 KB Output is correct
9 Correct 316 ms 208 KB Output is correct
10 Correct 845 ms 368 KB Output is correct
11 Correct 872 ms 364 KB Output is correct
12 Correct 729 ms 372 KB Output is correct
13 Correct 909 ms 376 KB Output is correct
14 Correct 8 ms 208 KB Output is correct
15 Correct 13 ms 208 KB Output is correct
16 Correct 51 ms 208 KB Output is correct
17 Correct 99 ms 208 KB Output is correct
18 Correct 214 ms 208 KB Output is correct
19 Correct 290 ms 336 KB Output is correct
20 Correct 234 ms 316 KB Output is correct
21 Correct 889 ms 368 KB Output is correct
22 Execution timed out 1072 ms 368 KB Time limit exceeded
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 208 KB Output is correct
2 Correct 31 ms 208 KB Output is correct
3 Partially correct 150 ms 208 KB Output is partially correct
4 Partially correct 482 ms 336 KB Output is partially correct
5 Partially correct 820 ms 380 KB Output is partially correct
6 Correct 12 ms 208 KB Output is correct
7 Correct 35 ms 208 KB Output is correct
8 Partially correct 94 ms 208 KB Output is partially correct
9 Partially correct 340 ms 208 KB Output is partially correct
10 Partially correct 861 ms 364 KB Output is partially correct
11 Partially correct 839 ms 376 KB Output is partially correct
12 Partially correct 898 ms 372 KB Output is partially correct
13 Execution timed out 1018 ms 368 KB Time limit exceeded
14 Halted 0 ms 0 KB -