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

用vector出错

Posted by zhby1990 at 2011-04-20 20:42:13 on Problem 3417
这是我的错误代码,如果把用vector的两处改为数组,就能AC。求解,为什么?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 1000
int cnt,head[maxn],n,iner[maxn];
int set[maxn],ancestors[maxn];
bool flag[maxn];
vector<int>qes[maxn];
struct node
{
    int y,next;
}a[maxn];
void ad(int x,int y)
{
    a[cnt].next=head[x];
    a[cnt].y=y;
    head[x]=cnt++;
}
void init(int n)
{
    cnt=1;
    for(int i=0;i<=n;i++)
    {
        iner[i]=0;head[i]=0;
        ancestors[i]=0;set[i]=i;
        flag[i]=false;
        qes[i].clear();
    }
}
int find_set(int x)
{
    if(x!=set[x])
        set[x]=find_set(set[x]);
    return set[x];
}
void union_set(int x,int y)
{
    x=find_set(x);
    y=find_set(y);
    if(x==y)return;
    set[y]=x;
}
void LCA(int x)
{
    ancestors[x]=x;
    for(int i=head[x];i!=0;i=a[i].next)
    {
        LCA(a[i].y);
        union_set(a[i].y,x);
        ancestors[find_set(x)]=x;
    }
    flag[x]=true;
    int size=qes[x].size();
    for(int i=0;i<size;i++)
    {
        if(flag[qes[x][i]])
            iner[ancestors[find_set(qes[x][i])]]++;
    }
}
int main()
{
    int i,j,t,x,y;
    while(scanf("%d",&n)!=-1)
    {
        init(n);
        for(i=1;i<=n;i++)
        {
            scanf("%d:(%d)",&x,&t);
            for(j=1;j<=t;j++)
            {
                scanf("%d",&y);
                ad(x,y);
                flag[y]=true;
            }
        }
        scanf("%d",&t);
        for(i=1;i<=t;i++)
        {
            while(getchar()!='(');
            scanf("%d %d)*%c",&x,&y);
            qes[x].push_back(y);
            qes[y].push_back(x);
        }
        for(i=1;i<=n;i++)
            if(!flag[i])break;
        memset(flag,0,sizeof(bool)*(n+2));
        LCA(i);
        for(i=1;i<=n;i++)
            if(iner[i])
                printf("%d:%d\n",i,iner[i]);
    }
}

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