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

代码党,求优化,没用STL 500MS++

Posted by lvhao945 at 2010-08-26 01:33:03 on Problem 1470
#include<iostream>
using namespace std;
#define N 1001
#define C(a) memset(a,0,len)
struct node
{
	int v,next;
}a1[N],a2[N*500];
int n,m,v0;
int root[N],cnt[N],used[N],d[N];
int g1[N],g2[N],l1,l2,len;
void add1(int a,int b)
{
	a1[++l1].v=b;
	a1[l1].next=g1[a];
	g1[a]=l1;
}
void add2(int a,int b)
{
	a2[++l2].v=b;
	a2[l2].next=g2[a];
	g2[a]=l2;
}
int find(int a)
{
	int b=a;
    while(b!=root[b])b=root[b];
    return root[a]=b;
}
void dfs(int s)
{
	int i;
    root[s]=s;
    for(i=g1[s];i;i=a1[i].next){
		int v=a1[i].v;
        dfs(v);
		root[v]=s;
    }
	used[s]=1;
    for(i=g2[s];i;i=a2[i].next){
		int v=a2[i].v;
        if(used[v])cnt[find(v)]++;
    }
}
int main()
{ 
    while(scanf("%d",&n)!=EOF){
		int u,v,i,k;
		len=sizeof(int)*(n+1);
		C(d);C(cnt);C(used);C(g1);C(g2);
		l1=l2=0;
		for(k=1;k<=n;k++){
			scanf("%d:(%d)",&u,&i);
			while(i--){
				scanf("%d",&v);
				d[v]++;
				add1(u,v);
			}
		}
		for(v0=1;d[v0];v0++);
		scanf("%d",&m);
		while(m--)
		{
			while(getchar()!='(');
			scanf("%d%d",&u,&v);
			if(u==v)cnt[u]++;
			else{
				add2(u,v);
				add2(v,u);
			} 
		}
		while(getchar()!=')');
        dfs(v0);
		for(i=1;i<=n;i++)
			if(cnt[i])printf("%d:%d\n",i,cnt[i]);
    }
    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