Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

tarjan+topsort 邻接表存数据,313MS,希望对大家有帮助

Posted by smallsunny at 2011-10-27 15:52:17 on Problem 2762
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 1000;
const int MAXM = 6000;
struct node
{
    int to, next;
}edge[MAXM+10];
struct node1
{
    int to, next;
}edge1[MAXM+10];
int box[MAXN+10], ecnt;
int nbox[MAXN+10], necnt;
int indegree[MAXN+10];
int dfn[MAXN+10], low[MAXN+10], st[MAXN+10], color[MAXN+10];
bool instack[MAXN+10];
int index, cnt, top;
int n, m, t;
void make_map(int from, int to)
{
    edge[ecnt].to = to;
    edge[ecnt].next = box[from];
    box[from] = ecnt++;
}
void nmake_map(int from, int to)
{
    edge1[necnt].to = to;
    edge1[necnt].next = nbox[from];
    nbox[from] = necnt++;
}
void tarjan(int x)
{
    dfn[x]=low[x]=++index;
    st[++top] = x;
    instack[x] = true;
    for(int i = box[x]; i+1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(dfn[v] == -1)
        {
            tarjan(v);
            low[x] = min(low[x], low[v]);
        }
        else if(instack[v]) low[x] = min(low[x], dfn[v]);
    }
    if(dfn[x] == low[x])
    {
        int u;
        do
        {
            u = st[top--];
            instack[u] = false;
            color[u] = cnt;
        }while(u != x);
        cnt++;
    }
}
void solve()
{
    memset(dfn, -1, sizeof(dfn));
    memset(instack, false, sizeof(instack));
    index=cnt=top=0;
    for(int i = 1; i <= n; i++)
    {
        if(dfn[i] == -1) tarjan(i);
    }
}
int topsort()
{
    for(int i = 0; i < cnt; i++)
    {
        int sum = 0;
        int pos;
        for(int j = 0; j < cnt; j++)
        {
            if(indegree[j] == 0)
            {
                 sum++;
                 pos = j;
            }
        }
        if(sum > 1) return -1;
        indegree[pos] = -1;
        for(int j = nbox[pos]; j+1; j = edge1[j].next)
        {
            int v = edge1[j].to;
            indegree[v]--;
        }
    }
    return 1;
}
int main()
{
    //freopen("in.txt", "r", stdin);
    scanf("%d", &t);
    for(int Cas = 1; Cas <= t; Cas++)
    {
        memset(box, -1, sizeof(box));
        ecnt = 0;
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= m; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            make_map(u, v);
        }
        solve();
        if(cnt == 1)
        {
            printf("Yes\n");
            continue;
        }
        memset(indegree, 0, sizeof(indegree));
        memset(nbox, -1, sizeof(nbox));
        necnt = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = box[i]; j+1; j = edge[j].next)
            {
                int u = i;
                int v = edge[j].to;
                if(color[u] != color[v])
                {
                    indegree[color[v]]++;
                    nmake_map(color[u], color[v]);
                }
            }
        }
        int sum = 0;
        for(int i = 0; i < cnt; i++)
        {
            if(indegree[i] == 0) sum++;
        }
        if(sum > 1)
        {
            printf("No\n");
            continue;
        }
        int sig = topsort();
        if(sig == 1) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator