コード例 #1
0
ファイル: main.cpp プロジェクト: shurik111333/spbu
int main()
{
    ifstream fin("unitopsort.in");
    fout.sync_with_stdio(false);
    int n = 0, m = 0;
    fin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a = 0, b = 0;
        fin >> a >> b;
        //pow[b]++;
        add(a, b);
    }
    for (int i = 1; i <= n; i++)
    {
        if (used[i] == 0)
            dfs(i);
    }
    bool isOnce = true;
    for (int i = n; i > 1; i--)
    {
        int k = first[topSort[i]];
        bool res = true;
        while (k != 0)
        {
            if (topSort[i - 1] == val[k])
            {
                res = false;
                break;
            }
            k = next1[k];
        }
        if (res)
        {
            isOnce = false;
            break;
        }
    }
    if (isOnce)
    {
        fout << "YES\n";
        for (int i = n; i > 0; i--)
        {
            fout << topSort[i] << " ";
        }
    }
    else
        fout << "NO";
    return 0;
}
コード例 #2
0
ファイル: main.cpp プロジェクト: shurik111333/spbu
int main()
{
    ifstream fin("cycle.in");
    fout.sync_with_stdio(false);
    int n = 0, m = 0;
    fin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a = 0, b = 0;
        fin >> a >> b;
        add(a, b);
    }
    for (int i = 1; i <= n; i++)
    {
        if (used[i] == 0)
            dfs(i);
        if (isFind)
            break;
    }
    if (!isFind)
        fout << "NO";
    return 0;
}