SPOJ9119 Indiana Jones and the lost Soccer Cup

來源:互聯網
上載者:User

水題。


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1e4 + 5;const int MAXM = 1e5 + 5;int n, m;struct Edge{    int v, next;} edge[MAXM];int head[MAXN], edgeNum;int in[MAXN];void clearEdge(){    edgeNum = 0;    memset(in, 0, sizeof(in));    memset(head, -1, sizeof(head));}void addEdge(int u, int v){    edge[edgeNum].v = v;    edge[edgeNum].next = head[u];    head[u] = edgeNum++;}bool visit[MAXN];int stack[MAXN], top;int dfn[MAXN], low[MAXN], timeStamp;bool dfs(int u){    dfn[u] = low[u] = ++timeStamp;    stack[top++] = u;    visit[u] = true;    for (int i = head[u]; i != -1; i = edge[i].next)    {        int v = edge[i].v;        if (dfn[v] == -1)        {            if (dfs(v))            {                return true;            }            low[u] = min(low[u], low[v]);        }        else if (visit[v])        {            low[u] = min(low[u], dfn[v]);        }    }    if (dfn[u] == low[u])    {        int temp, num = 0;        do        {            temp = stack[--top];            visit[temp] = false;            ++num;        }        while (temp != u);        if (num >= 2)        {            return true;        }    }    return false;}bool containCircle(){    timeStamp = top = 0;    memset(visit, false, sizeof(visit));    memset(dfn, -1, sizeof(dfn));    for (int i = 1; i <= n; ++i)    {        if (dfn[i] == -1)        {            if (dfs(i))            {                return true;            }        }    }    return false;}int ans[MAXN];int main(){    int T;    int u, v;    scanf("%d", &T);    while (T--)    {        scanf("%d%d", &n, &m);        clearEdge();        for (int i = 0; i < m; ++i)        {            scanf("%d%d", &u, &v);            addEdge(u, v);            ++in[v];        }        bool missing = false;        bool recheck = false;        if (containCircle())        {            recheck = true;        }        int inIndex = -1;        for (int i = 1; i <= n; ++i)        {            if (in[i] == 0)            {                if (inIndex == -1)                {                    inIndex = i;                }                else                {                    missing = true;                }            }        }        if (inIndex == -1)        {            recheck = true;        }        ans[0] = inIndex;        for (int i = 1; i < n && !missing && !recheck; ++i)        {            int next = -1;            for (int j = head[inIndex]; j != -1; j = edge[j].next)            {                int v = edge[j].v;                if (--in[v] == 0)                {                    if (next == -1)                    {                        next = v;                    }                    else                    {                        missing = true;                        break;                    }                }            }            if (next == -1)            {                recheck = true;            }            ans[i] = inIndex = next;        }        if (recheck)        {            printf("recheck hints\n");        }        else if (missing)        {            printf("missing hints\n");        }        else        {            for (int i = 0; i < n; ++i)            {                if (i)                {                    printf(" ");                }                printf("%d", ans[i]);            }            printf("\n");        }    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.