답안 #840065

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840065 2023-08-31T04:24:32 Z Fysty 가장 긴 여행 (IOI23_longesttrip) C++17
40 / 100
1000 ms 908 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;
    for(int v:ed[u]) if(!vis[v])
    {
        par[v]=u;
        cnt[u]++;
        dfs(v);
    }
}

void dfs2(int u,vector<int> &ret)
{
    vis[u]=1;
    ret.pb(u);
    for(int v:ed[u]) if(!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];
      |         ~~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 180 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 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 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 30 ms 208 KB Output is correct
3 Correct 169 ms 348 KB Output is correct
4 Correct 412 ms 336 KB Output is correct
5 Correct 779 ms 568 KB Output is correct
6 Correct 11 ms 256 KB Output is correct
7 Correct 19 ms 208 KB Output is correct
8 Correct 140 ms 340 KB Output is correct
9 Correct 287 ms 384 KB Output is correct
10 Correct 837 ms 564 KB Output is correct
11 Correct 799 ms 644 KB Output is correct
12 Correct 813 ms 680 KB Output is correct
13 Correct 716 ms 684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 32 ms 208 KB Output is correct
3 Correct 165 ms 332 KB Output is correct
4 Correct 427 ms 384 KB Output is correct
5 Correct 837 ms 600 KB Output is correct
6 Correct 13 ms 208 KB Output is correct
7 Correct 24 ms 208 KB Output is correct
8 Correct 170 ms 336 KB Output is correct
9 Correct 261 ms 384 KB Output is correct
10 Correct 829 ms 640 KB Output is correct
11 Correct 752 ms 628 KB Output is correct
12 Correct 836 ms 724 KB Output is correct
13 Correct 750 ms 676 KB Output is correct
14 Correct 9 ms 208 KB Output is correct
15 Correct 16 ms 208 KB Output is correct
16 Correct 39 ms 208 KB Output is correct
17 Correct 117 ms 328 KB Output is correct
18 Correct 158 ms 340 KB Output is correct
19 Correct 316 ms 352 KB Output is correct
20 Correct 264 ms 484 KB Output is correct
21 Correct 750 ms 668 KB Output is correct
22 Correct 766 ms 732 KB Output is correct
23 Correct 837 ms 664 KB Output is correct
24 Correct 644 ms 668 KB Output is correct
25 Correct 12 ms 208 KB Output is correct
26 Correct 11 ms 208 KB Output is correct
27 Correct 32 ms 208 KB Output is correct
28 Correct 36 ms 208 KB Output is correct
29 Correct 38 ms 208 KB Output is correct
30 Correct 199 ms 352 KB Output is correct
31 Correct 166 ms 340 KB Output is correct
32 Correct 218 ms 340 KB Output is correct
33 Correct 318 ms 464 KB Output is correct
34 Correct 313 ms 384 KB Output is correct
35 Correct 370 ms 372 KB Output is correct
36 Correct 787 ms 660 KB Output is correct
37 Correct 761 ms 532 KB Output is correct
38 Correct 784 ms 784 KB Output is correct
39 Correct 896 ms 648 KB Output is correct
40 Correct 866 ms 664 KB Output is correct
41 Correct 790 ms 392 KB Output is correct
42 Correct 774 ms 508 KB Output is correct
43 Correct 762 ms 656 KB Output is correct
44 Correct 833 ms 764 KB Output is correct
45 Correct 11 ms 208 KB Output is correct
46 Correct 12 ms 208 KB Output is correct
47 Correct 27 ms 208 KB Output is correct
48 Correct 26 ms 208 KB Output is correct
49 Correct 35 ms 208 KB Output is correct
50 Correct 186 ms 336 KB Output is correct
51 Correct 189 ms 336 KB Output is correct
52 Correct 186 ms 336 KB Output is correct
53 Correct 279 ms 336 KB Output is correct
54 Correct 335 ms 376 KB Output is correct
55 Correct 327 ms 336 KB Output is correct
56 Correct 762 ms 588 KB Output is correct
57 Correct 726 ms 808 KB Output is correct
58 Correct 913 ms 768 KB Output is correct
59 Correct 825 ms 780 KB Output is correct
60 Correct 811 ms 664 KB Output is correct
61 Correct 749 ms 508 KB Output is correct
62 Correct 806 ms 640 KB Output is correct
63 Correct 840 ms 780 KB Output is correct
64 Correct 659 ms 788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 27 ms 208 KB Output is correct
3 Partially correct 161 ms 336 KB Output is partially correct
4 Partially correct 415 ms 464 KB Output is partially correct
5 Partially correct 797 ms 652 KB Output is partially correct
6 Correct 10 ms 208 KB Output is correct
7 Correct 32 ms 208 KB Output is correct
8 Partially correct 151 ms 336 KB Output is partially correct
9 Partially correct 351 ms 376 KB Output is partially correct
10 Partially correct 825 ms 676 KB Output is partially correct
11 Partially correct 797 ms 544 KB Output is partially correct
12 Partially correct 766 ms 556 KB Output is partially correct
13 Partially correct 812 ms 636 KB Output is partially correct
14 Correct 10 ms 208 KB Output is correct
15 Correct 9 ms 208 KB Output is correct
16 Correct 55 ms 208 KB Output is correct
17 Partially correct 113 ms 208 KB Output is partially correct
18 Partially correct 157 ms 340 KB Output is partially correct
19 Partially correct 312 ms 352 KB Output is partially correct
20 Partially correct 314 ms 348 KB Output is partially correct
21 Correct 12 ms 208 KB Output is correct
22 Correct 11 ms 208 KB Output is correct
23 Correct 32 ms 208 KB Output is correct
24 Correct 43 ms 208 KB Output is correct
25 Correct 31 ms 208 KB Output is correct
26 Partially correct 221 ms 352 KB Output is partially correct
27 Partially correct 219 ms 336 KB Output is partially correct
28 Partially correct 222 ms 336 KB Output is partially correct
29 Partially correct 310 ms 360 KB Output is partially correct
30 Partially correct 335 ms 380 KB Output is partially correct
31 Partially correct 264 ms 388 KB Output is partially correct
32 Correct 15 ms 208 KB Output is correct
33 Correct 14 ms 208 KB Output is correct
34 Correct 23 ms 208 KB Output is correct
35 Correct 30 ms 208 KB Output is correct
36 Correct 22 ms 208 KB Output is correct
37 Partially correct 178 ms 336 KB Output is partially correct
38 Partially correct 204 ms 340 KB Output is partially correct
39 Partially correct 209 ms 340 KB Output is partially correct
40 Partially correct 320 ms 380 KB Output is partially correct
41 Partially correct 261 ms 336 KB Output is partially correct
42 Partially correct 305 ms 344 KB Output is partially correct
43 Partially correct 797 ms 720 KB Output is partially correct
44 Partially correct 839 ms 796 KB Output is partially correct
45 Partially correct 750 ms 908 KB Output is partially correct
46 Partially correct 756 ms 556 KB Output is partially correct
47 Partially correct 824 ms 756 KB Output is partially correct
48 Partially correct 931 ms 676 KB Output is partially correct
49 Partially correct 947 ms 824 KB Output is partially correct
50 Partially correct 815 ms 884 KB Output is partially correct
51 Execution timed out 1010 ms 680 KB Time limit exceeded
52 Halted 0 ms 0 KB -