답안 #594444

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
594444 2022-07-12T13:00:18 Z andrei_boaca Potemkin cycle (CEOI15_indcyc) C++14
60 / 100
68 ms 6288 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;
int n,m;
vector<pii> edges;
vector<int> muchii[1005];
bool adj[1005][1005];
int dp[1005][22],par[1005],dist[1005][1005],niv[1005],in[1005],out[1005],from[1005];
int comp[1005];
bool use[1005];
int Dp[1005];
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;
}
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];
        }
        vector<int> v;
        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);
        bool ok=1;
        /*for(int x:v)
            for(int y:v)
                if(adj[x][y])
                    ok=0;*/
        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;
    }
    cout<<"no";
    return 0;
}

Compilation message

indcyc.cpp: In function 'int main()':
indcyc.cpp:121:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         for(int j=0;j<v.size();j++)
      |                     ~^~~~~~~~~
indcyc.cpp:115:14: warning: unused variable 'ok' [-Wunused-variable]
  115 |         bool ok=1;
      |              ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 852 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1968 KB Output is correct
2 Correct 5 ms 1876 KB Output is correct
3 Correct 6 ms 2020 KB Output is correct
4 Correct 7 ms 2004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 1876 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 6208 KB Output is correct
2 Correct 49 ms 5780 KB Output is correct
3 Correct 60 ms 6288 KB Output is correct
4 Correct 46 ms 5764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 36 ms 5720 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 43 ms 4340 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -