315 - Network

#include<bits/stdc++.h>
#define NIL -1
#define sz 103
using namespace std;

vector<int>graph[sz];
bool visited[sz];
bool ap[sz];
int par[sz],shomoy;
int start[sz];
int low[sz];

void AP(int u)
{
    int child = 0;
    visited[u] = true;
    ++shomoy;
    start[u] = shomoy;
    low[u] = shomoy;

    for(int i=0; i<graph[u].size(); i++)
    {
        int v = graph[u][i];
        if(visited[v]==false)
        {
            child++;
            par[v] = u;
            AP(v);
            low[u] = min(low[u],low[v]);
            if(par[u]==NIL && child>1)
            {
                ap[u]=true;
            }

            if(par[u]!=NIL && low[v]>=start[u])
            {
                ap[u] = true;
            }
        }

        else if(v!=par[u])
        {
            low[u] = min(low[u],start[v]);
        }
    }
}


int main()
{
    int n,i,j,node_from,node_to,counter,a,len;
    char str[1000];
    while(scanf("%d",&n)&&n)
    {
        getchar();
        while(gets(str))
        {
            a=0;
            if(str[0]=='0')
            {
                break;
            }
            len = strlen(str);
            for(j=0; j<len; j++)
            {
                if(str[j]!=' ')
                {
                    node_from=a*10+str[j]-48;
                    a=node_from;
                }
                else if(str[j]==' ')
                {
                    break;
                }
            }

            a=0;
            for(j=j+1; j<=len; j++)
            {
                if(str[j]!=' '&& str[j]!='\0')
                {
                    node_to=a*10+str[j]-48;
                    a=node_to;
                }
                else if(str[j]==' '||str[j]=='\0')
                {
                    a=0;
                    graph[node_from].push_back(node_to);
                    graph[node_to].push_back(node_from);
                    continue;
                }
            }

        }

        for(i=1; i<=n; i++)
        {
            par[i] = NIL;
        }

        shomoy = 0;
        counter=0;

        for(i=1; i<=n; i++)
        {
            if(visited[i]==false)
            {
                AP(i);
            }
        }

        for(i=1; i<=n; i++)
        {
            if(ap[i]==true)
            {
                counter++;
            }
        }

        printf("%d\n",counter);
        for(i=0; i<=n; i++)
        {
            graph[i].clear();
        }
        memset(ap,false,sizeof(ap));
        memset(visited,false,sizeof(visited));
    }
    return 0;
}

0 comments: (+add yours?)