ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

1272. Non-Yekaterinburg Subway
Posted by Điền 3 Apr 2013 16:48
struct graph
{
    int ndinh;
    int nham;
    int ncau;
    vector<EDGE> T;
    int nT;
};
typedef struct graph GRAPH;
vector<int> V;
int nV;
void DocFile(GRAPH &);
void Doc(GRAPH &);
void InitGraph(GRAPH &);
void XacDinHam(GRAPH);
int XetLienThong(GRAPH);
bool IsCirCle(GRAPH,int);
void main()
{
    GRAPH g;
    ::InitGraph(g);
    ::Doc(g);
    ::XacDinHam(g);
    int kq =::XetLienThong(g);
    printf("%d\n",kq);
}
void DocFile(GRAPH &g)
{
    FILE * fo = fopen("MyFile.txt","rt");
    if(!fo)
    {
        //printf("Khong mo duoc tap tin!!!\n");
        return;
    }
    fscanf(fo,"%d",&g.ndinh);
    fscanf(fo,"%d",&g.nham);
    fscanf(fo,"%d",&g.ncau);
    g.nT=0;
    EDGE Temp;
    while( !feof(fo) )
    {
        fscanf(fo,"%d",&Temp.v1);
        fscanf(fo,"%d",&Temp.v2);
        g.nT++;
        g.T.push_back(Temp);
    }
    fclose(fo);
}
void Doc(GRAPH &g)
{
    int Temp=0;
    scanf("%d",&Temp);
    g.ndinh = Temp;
    scanf("%d",&Temp);
    g.nham = Temp;
    scanf("%d",&Temp);
    g.ncau = Temp;
    int Temp2=0,Temp3=0;
    int n= g.ncau + g.nham;
    for(int i=0;i<n;i++)
    {
        EDGE Temp4;
        scanf("%d",&Temp2);
        scanf("%d",&Temp3);
        Temp4.v1 = Temp2;
        Temp4.v2 = Temp3;
        g.T.push_back(Temp4);
        g.nT++;
    }
}
void InitGraph(GRAPH &g)
{
    g.nT=0;
    g.ncau=0;
    g.ndinh=0;
    g.nham=0;
}
bool IsCirCle(GRAPH g,int Index)
{
    if( V.at(g.T.at(Index).v1-1) == V.at(g.T.at(Index).v2-1) )
        return true;
    else
    {
        int label1,label2;
        if( V.at(g.T.at(Index).v1-1) > V.at(g.T.at(Index).v2-1) )
        {
            label1 = V.at(g.T.at(Index).v2-1);
            label2 = V.at(g.T.at(Index).v1-1);
        }
        else
        {
            label1 = V.at(g.T.at(Index).v1-1);
            label2 = V.at(g.T.at(Index).v2-1);
        }
        for(int i=0;i<g.ndinh;i++)
            if( V.at(i) == label2 )
                V.at(i) = label1;
        return false;
    }
}
void XacDinHam(GRAPH g)
{
    V.resize(g.ndinh);
    for(int i=0;i<g.ndinh;i++)
    {
        V.at(i) = i;
    }
    int nHam=0;
    int nCau=0;
    for(int i=0;i<g.nT;i++)
    {
        if(nHam!=g.nham)
        {
            if( !IsCirCle(g,i) )
            {
                nHam++;
            }
        }
        else
        {
            break;
        }
    }
}
int XetLienThong(GRAPH g)
{
    int dem=0;
    for(int i=0;i<g.ndinh;i++)
    {
        int co=1;
        if(i==g.ndinh-1)
        {
            dem++;
        }
        else
        {
            for(int j=i+1;j<g.ndinh;j++)
            {
                if(V.at(i) == V.at(j))
                {
                    co=0;
                    break;
                }
            }
            if(co==1)
                dem++;
        }
    }
    return dem-1;
}
không biết bị lỗi gì mà chỉ chạy được tới test5. Mong được chỉ giáo