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

寻大侠赐教!TLE了无数次,不知道怎么办了。

Posted by lovecanon at 2008-11-05 18:12:53 on Problem 2762 and last updated at 2008-11-05 18:13:41
这是我的代码,强连通分连缩点后,拓扑排序,但就是TLE!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
	int id;
	node *next;
}edge1[60001],//原图
 edge2[6001],//无向图
 edge3[6001];//逆图
struct ARC{
    int u,v;
}arc[6001];
int n,m,tot,top,label,flag,mem[6001],mark[6001],indegree[6001],hashtable[1000001][2];
bool vis[1000001];
void insert(int a,int b){
	node *s=new node;node *t=new node;node *p=new node;node *q=new node;
	s->id=b;t->id=a; p->id=a;q->id=b;
	s->next=edge1[a].next;t->next=edge2[b].next;
	p->next=edge3[b].next;q->next=edge3[a].next;
	edge1[a].next=s;edge2[b].next=t;edge3[a].next=q;edge3[b].next=p;
}
void check(int t){//检查无向图的连通性
	tot++;vis[t]=true;
	node *ptr=edge3[t].next;
	while(ptr){
		if(!vis[ptr->id]) check(ptr->id);
		ptr=ptr->next;
	}
}
void dfsR(int t){//对逆图dfs
    vis[t]=true;
    node *ptr=edge2[t].next;
    while(ptr){
        if(!vis[ptr->id]) dfsR(ptr->id);
        ptr=ptr->next;
    }
    mem[++top]=t;
}
void dfs(int t){//标记强连通分量
    mark[t]=label;
    vis[t]=true;
    node *ptr=edge1[t].next;
    while(ptr){
        if(!vis[ptr->id]) dfs(ptr->id);
        ptr=ptr->next;
    }
}
int hashf(int a,int b){//hash
    int tmp=(a*131313+b*13131+a*b)%100001;
    while(!vis[tmp] && !(hashtable[tmp][0]==a&&hashtable[tmp][1]==b))
    return tmp;
}
int main(){
	int i,t,a,b;
	scanf("%d",&t);
	while(t--){
		memset(edge1,0,sizeof(edge1));
		memset(edge2,0,sizeof(edge2));
		memset(edge3,0,sizeof(edge3));
		scanf("%d%d",&n,&m);
		for(i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			arc[i].u=a;arc[i].v=b;
			insert(a,b);
		}
		tot=0;
		memset(vis,false,sizeof(vis));
		check(1);//检查连通性
		if(tot!=n) {printf("No\n");continue;}
		memset(vis,false,sizeof(vis));
		top=0;
		for(i=1;i<=n;i++){
                    if(!vis[i]) dfsR(i);//对逆图dfs
                }

                memset(vis,false,sizeof(vis));
                label=0;
                for(i=top;i>0;i--){//标记连通分量
                    label++;
                    dfs(mem[i]);
                }

                memset(indegree,0,sizeof(indegree));
                memset(vis,false,sizeof(vis));
                memset(edge1,0,sizeof(edge1));

                for(i=1;i<=m;i++){//重建图
                    a=mark[arc[i].u];b=mark[arc[i].v];
                    if(a!=b){
                        int tmp=hashf(a,b);
                        if(!vis[tmp]){
                            node *s=new node;
                            s->id=b;s->next=edge1[a].next;
                            indegree[b]++;
                            vis[tmp]=true;
                            hashtable[tmp][0]=a;hashtable[tmp][1]=b;
                        }
                    }
                }
                top=0;flag=0;
                for(i=1;i<=label && top<=1;i++) 
                if(!indegree[i]) mem[++top]=i;
                if(top>1) {printf("No\n");continue;}
                while(top){//拓扑排序,如果有分支就不满足
                    int tmp=mem[top--];
                    node *ptr=edge1[tmp].next;
                    while(ptr){
                        indegree[ptr->id]--;
                        if(!indegree[ptr->id]) mem[++top]=ptr->id;
                        ptr=ptr->next;
                    }
                    if(top>=1){flag=1;break;}
                }
                if(flag) printf("No\n");
                else printf("Yes\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