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

到底为啥RE……求真相……(附代码=。=)

Posted by Tuzki at 2010-04-13 21:29:58 on Problem 2762
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 1010
#define MAXN 10000
using namespace std;
int cnt;
struct edge
{
    int v;
    edge * next;
};
struct Graph
{
    edge memory[MAXN];
    edge * head[MAX];
    int dfn[MAX],low[MAX],S[MAX],inS[MAX];
    int TOP,index,sp;
    void init(int n)
    {
        int i;
        TOP=-1;
        for(i=1;i<=n;i++)
            head[i]=NULL;
    }
    void addedge(int u,int v)
    {
        edge * ptr=&memory[++TOP];
        ptr->v=v;
        ptr->next=head[u];
        head[u]=ptr;
    }
    void DFS(int u,int * ID)
    {
        dfn[u]=low[u]=++index;
        inS[u]=1;
        S[++sp]=u;
        for(edge * ptr=head[u];ptr;ptr=ptr->next)
        {
            int v=ptr->v;
            if(!dfn[v])
            {
                DFS(v,ID);
                if(low[u]>low[v])
                  low[u]=low[v];
            }
            else if(inS[v]&&dfn[v]<low[u])
                 low[u]=dfn[v];
        }
        if(dfn[u]==low[u])
        {
            int v=S[sp--];inS[v]=1;ID[v]=cnt;
            while(u!=v)
            {
                v=S[sp--];inS[v]=1;ID[v]=cnt;
            }
            cnt++;
        }
    }

    void Tarjan(int n,int * ID)
    {
        int i;
        cnt=0;index=0;sp=-1;
        for(i=1;i<=n;i++)
           dfn[i]=0;
        for(i=1;i<=n;i++)
           if(!dfn[i])
             DFS(i,ID);
    }
};
int G[MAX][MAX];
int main()
{
    Graph mt;
    int T,n,m,i,s,t,error,x,p,front,rear;
    int u[MAXN],v[MAXN],ID[MAX],in[MAX];
    int q[MAX],vis[MAX];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(G,0,sizeof(G));
        memset(in,0,sizeof(in));
        mt.init(n);
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&s,&t);
            mt.addedge(s,t);
            u[i]=s;v[i]=t;
        }
        mt.Tarjan(n,ID);
        for(i=0;i<m;i++)
        {
            int x=u[i],y=v[i];
            if(ID[x]!=ID[y])
            {
                if(!G[ID[x]][ID[y]])
                {
                    G[ID[x]][ID[y]]=1;
                    in[ID[y]]=1;
                }
            }
        }
        error=0;t=0;
        for(i=0;i<cnt;i++)
            if(in[i]==0)
            {
                if(!t){t=1;x=i;}
                else {error=1;break;}
            }
        if(!error)
        {
            memset(vis,0,sizeof(vis));
            q[0]=x;
            vis[x]=1;
            front=0;rear=1;
            while((front<rear)&&!error)
            {
                s=q[front++];
                p=0;
                for(t=0;t<cnt;t++)
                {
                    if(G[s][t]&&!vis[t])
                    {
                        in[t]--;
                        if(in[t]==0)
                        {
                            if(!p)
                            {
                                p=1;
                                q[rear++]=t;
                                vis[t]=1;
                            }
                            else {error=1;break;}
                        }
                    }
                }
            }
        }
        if(!error)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