Submission #594470

# Submission time Handle Problem Language Result Execution time Memory
594470 2022-07-12T13:33:24 Z andrei_boaca Potemkin cycle (CEOI15_indcyc) C++14
90 / 100
987 ms 6292 KB
#include <bits/stdc++.h>

using namespace std;
mt19937 rng(time(NULL));
typedef pair<int,int> pii;
int n,m;
vector<pii> edges;
vector<int> muchii[1005];
vector<int> v;
bool adj[1005][1005];
int dp[1005][22],par[1005],dist[1005][1005],niv[1005],in[1005],out[1005],from[1005];
int comp[1005];
int use[1005];
int Dp[1005];
auto S=chrono::steady_clock::now();
int timp;
bool isancestor(int a,int b)
{
    return in[a]<=in[b]&&out[a]>=out[b];
}
int LCA(int a,int b)
{
    if(niv[a]>niv[b])
        swap(a,b);
    if(isancestor(a,b))
        return a;
    for(int i=12;i>=0;i--)
        if(dp[a][i]!=0&&!isancestor(dp[a][i],b))
            a=dp[a][i];
    return par[a];
}
void Dfs(int nod)
{
    timp++;
    in[nod]=timp;
    use[nod]=1;
    dp[nod][0]=par[nod];
    for(int i=1;i<=12;i++)
        dp[nod][i]=dp[dp[nod][i-1]][i-1];
    for(int i:muchii[nod])
        if(i!=par[nod])
        {
            par[i]=nod;
            niv[i]=niv[nod]+1;
            Dfs(i);
        }
    out[nod]=timp;
}
bool cmp(pii a, pii b)
{
    int d1=dist[a.first][a.second];
    int d2=dist[b.first][b.second];
    return d1<d2;
}
bool block[1005];
int start;
void dfs(int nod)
{
    auto E=chrono::steady_clock::now();
    int x=chrono::duration_cast<chrono::milliseconds>(E - S).count();
    if(v.size()>=21)
        return;
    if(x>=990)
    {
        cout<<"no";
        exit(0);
    }
    use[nod]=v.size();
    bool good=1;
    int pmax=0;
    int pmin=1e9;
    vector<int> mypoz;
    for(int i:muchii[nod])
    {
        if(use[i])
        {
            if(i==v[v.size()-2])
                continue;
            if(i==start)
            {
                if(v.size()>=4);
                else
                    good=0;
            }
            else if(i!=v[v.size()-2])
                good=0;
            pmax=max(pmax,use[i]);
            pmin=min(pmin,use[i]);
            mypoz.push_back(use[i]);
        }
    }
    sort(mypoz.begin(),mypoz.end());
    if(v.size()-pmax+1>=4&&pmax!=0)
    {
        for(int i=pmax-1;i<v.size();i++)
            cout<<v[i]<<' ';
        exit(0);
    }
    if(adj[nod][start]&&pmin+1>=4&&pmin!=1e9)
    {
        for(int i=0;i<pmin;i++)
            cout<<v[i]<<' ';
        cout<<nod;
        exit(0);
    }
    for(int i=1;i<mypoz.size();i++)
    {
        int st=mypoz[i-1];
        int dr=mypoz[i];
        if(dr-st+2>=4)
        {
            for(int j=st-1;j<dr;j++)
                cout<<v[j]<<' ';
            cout<<nod;
            exit(0);
        }
    }
    if(good)
    {
        for(int i:muchii[nod])
            if(!use[i])
            {
                v.push_back(i);
                dfs(i);
                use[i]=0;
                v.pop_back();
            }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        comp[i]=i;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        cin>>a>>b;
        if(comp[a]!=comp[b])
        {
            muchii[a].push_back(b);
            muchii[b].push_back(a);
            int c2=comp[b];
            for(int j=1;j<=n;j++)
                if(comp[j]==c2)
                    comp[j]=comp[a];
        }
        else
            edges.push_back({a,b});
    }
    for(int i=1;i<=n;i++)
        if(!use[i])
        {
            timp=0;
            niv[i]=1;
            Dfs(i);
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(comp[i]==comp[j])
            {
                int lca=LCA(i,j);
                dist[i][j]=niv[i]-niv[lca]+niv[j]-niv[lca]+1;
            }
    sort(edges.begin(),edges.end(),cmp);
    for(auto i:edges)
    {
        int a=i.first;
        int b=i.second;
        muchii[a].push_back(b);
        muchii[b].push_back(a);
        int lca=LCA(a,b);
        vector<int> v1,v2;
        while(a!=lca)
        {
            v1.push_back(a);
            a=par[a];
        }
        while(b!=lca)
        {
            v2.push_back(b);
            b=par[b];
        }
        v.clear();
        for(auto j:v1)
            v.push_back(j);
        v.push_back(lca);
        reverse(v2.begin(),v2.end());
        for(auto j:v2)
            v.push_back(j);
        vector<int> vals;
        for(int j=0;j<v.size();j++)
        {
            if(j==0)
                Dp[v[j]]=1;
            else
                Dp[v[j]]=1e9;
            int x=v[j];
            from[x]=0;
            for(int k=j-1;k>=0;k--)
            {
                int y=v[k];
                if(adj[x][y]||k==j-1)
                {
                    Dp[x]=min(Dp[x],Dp[y]+1);
                    if(Dp[y]+1==Dp[x])
                        from[x]=y;
                }
            }
        }
        int last=v.back();
        if(Dp[last]>=4)
        {
            vector<int> vals;
            while(true)
            {
                vals.push_back(last);
                if(last==v[0])
                    break;
                last=from[last];
            }
            for(int j:vals)
                cout<<j<<' ';
            return 0;
        }
        adj[i.first][i.second]=adj[i.second][i.first]=1;
    }
    v.clear();
    vector<int> nodes;
    for(int i=1;i<=n;i++)
        shuffle(muchii[i].begin(),muchii[i].end(),rng);
    for(int i=1;i<=n;i++)
        nodes.push_back(i);
    shuffle(nodes.begin(),nodes.end(),rng);
    for(int i:nodes)
    {
        start=i;
        v.clear();
        v.push_back(i);
        for(int j=1;j<=n;j++)
            use[j]=0;
        dfs(i);
    }
    cout<<"no";
    return 0;
}

Compilation message

indcyc.cpp: In function 'void dfs(int)':
indcyc.cpp:95:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for(int i=pmax-1;i<v.size();i++)
      |                          ~^~~~~~~~~
indcyc.cpp:106:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |     for(int i=1;i<mypoz.size();i++)
      |                 ~^~~~~~~~~~~~~
indcyc.cpp: In function 'int main()':
indcyc.cpp:194:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  194 |         for(int j=0;j<v.size();j++)
      |                     ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 852 KB Output is correct
2 Correct 633 ms 912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 972 ms 1976 KB Output is correct
2 Correct 5 ms 1876 KB Output is correct
3 Correct 7 ms 2004 KB Output is correct
4 Correct 970 ms 2020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 1976 KB Output is correct
2 Correct 980 ms 1956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 6256 KB Output is correct
2 Correct 48 ms 5776 KB Output is correct
3 Correct 987 ms 6292 KB Output is correct
4 Correct 983 ms 5832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 5720 KB Output is correct
2 Correct 980 ms 5788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 986 ms 4232 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -