CodeForces 412D

同时也是 LETTers 2015 Summer II warm-up - A

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int N = 3e4 + 5;

vector <int> g[N];
int r[N] = {0};
bool f[N] = {false};
int j = 0;

void dfs(int u);

int main()
{
    int n, m, a, b;

    //freopen("A.in", "r", stdin);
    scanf("%d%d", &n, &m);

    for (int i = 0; i < m; i++)
    {
        scanf("%d%d", &a, &b);
        g[a].push_back(b);
    }

    for (int i = 1; i <= n; i++)
    {
        if (!f[i])
        {
            f[i] = true;
            dfs(i);
        }
    }

    for (int i = 0; i < j - 1; i++)
    {
        printf("%d ", r[i]);
    }
    printf("%d\n", r[j-1]);

    return 0;
}

void dfs(int u)
{
    int v;

    for (int i = 0; i < g[u].size(); i++)
    {
        v = g[u][i];
        if (!f[v])
        {
            f[v] = true;
            dfs(v);
        }
    }
    r[j++] = u;
}
添加新评论