답안 #52671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52671 2018-06-26T10:57:33 Z Alexa2001 Potemkin cycle (CEOI15_indcyc) C++17
100 / 100
584 ms 4228 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int Nmax = 1005;

bool edge[Nmax][Nmax], S[Nmax], used[Nmax];
int comp[Nmax], t[Nmax];
int x, y, n, m, i, C;
vector<int> v[Nmax], comps[Nmax];

void dfs(int node)
{
    used[node] = 1;
    comp[node] = C;
    for(auto it : v[node])
        if(!used[it]) dfs(it);
}

void path(int start, int finish)
{
    memset(t, -1, sizeof(t));
    t[start] = 0;

    queue<int> q;
    q.push(start);

    int node;
    while(q.size() && t[finish] == -1)
    {
        node = q.front();
        q.pop();

        for(auto it : v[node])
            if(t[it] == -1)
            {
                if(it == finish)
                {
                    t[finish] = node;
                    break;
                }
                else if(S[it]) continue;

                t[it] = node;
                q.push(it);
            }
    }

    assert(t[finish] != -1);

    while(finish)
    {
        cout << finish << ' ';
        finish = t[finish];
    }
}

void solve()
{
    int node, i;
    for(node = 1; node <= n; ++node)
    {
        memset(used, 0, sizeof(used));
        memset(S, 0, sizeof(S));
        memset(comp, 0, sizeof(comp));

        for(auto it : v[node]) used[it] = 1, S[it] = 1;
        used[node] = 1, S[node] = 1;

        C = 0;
        for(i=1; i<=n; ++i)
            if(!used[i])
            {
                ++C;
                dfs(i);
            }

        for(auto x : v[node])
        {
            comps[x].clear();
            for(auto y : v[x])
                if(!S[y])
                    comps[x].push_back(comp[y]);
            sort(comps[x].begin(), comps[x].end());
            comps[x].erase( unique(comps[x].begin(), comps[x].end()), comps[x].end() );
        }

        for(auto x : v[node])
            for(auto y : v[node])
                if(x < y && !edge[x][y])
                {
                    if(comps[x].size() > comps[y].size()) swap(x, y);

                    for(auto val : comps[x])
                        if(binary_search(comps[y].begin(), comps[y].end(), val))
                        {
                            path(x, y);
                            cout << node << '\n';
                            return;
                        }
                }
    }
    cout << "no\n";
}

int main()
{
  //  freopen("input", "r", stdin);
  //  freopen("output", "w", stdout);
    cin.sync_with_stdio(false);

    cin >> n >> m;
    for(i=1; i<=m; ++i)
    {
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
        edge[x][y] = edge[y][x] = 1;
    }

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 540 KB Output is correct
4 Correct 2 ms 540 KB Output is correct
5 Correct 2 ms 616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 720 KB Output is correct
2 Correct 2 ms 744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 764 KB Output is correct
2 Correct 5 ms 764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 1020 KB Output is correct
2 Correct 4 ms 1020 KB Output is correct
3 Correct 5 ms 1020 KB Output is correct
4 Correct 23 ms 1276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1276 KB Output is correct
2 Correct 12 ms 1276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 2320 KB Output is correct
2 Correct 15 ms 2320 KB Output is correct
3 Correct 231 ms 3160 KB Output is correct
4 Correct 118 ms 3160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 3160 KB Output is correct
2 Correct 246 ms 3180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 3180 KB Output is correct
2 Correct 434 ms 3180 KB Output is correct
3 Correct 31 ms 3716 KB Output is correct
4 Correct 584 ms 4228 KB Output is correct