Network
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7272 | Accepted: 3415 |
Description
A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure
occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure
occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.
Input
The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
Output
The output contains for each block except the last in the input file one line containing the number of critical places.
Sample Input
5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0
Sample Output
1 2
Hint
You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.
题意:求无向图的割点。学习了一种新的输入方法。
割点的充分必要条件:①是根节点,并且有两个及两个以上的孩子②不是根节点,并且有孩子。
一:
二
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
#define maxn 105
#define maxm 10005
int n,m;
int min(int a,int b)
{
return a<b?a:b;
}
struct node
{
int data;
node *next;
};
node edge[maxn];
int dfn[maxn],low[maxn],time[maxn];//time记录孩子树
int index;
void init()
{
index=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(time,0,sizeof(time));
}
void dfs(int s)
{
dfn[s]=low[s]=++index;
node *p=edge[s].next;
time[s]=0;
while(p)
{
if(!dfn[p->data])
{
dfs(p->data);
if(low[p->data]>=dfn[s])
time[s]++;
low[s]=min(low[s],low[p->data]);
}
else
low[s]=min(low[s],dfn[p->data]);
p=p->next;
}
}
char str[5];
int main()
{
int n,m,a;
while(cin>>n,n)
{
init();
for(int i=1;i<=n;i++)
edge[i].next=0;
while(cin>>m,m)
{
while(getchar()!='\n')
{
cin>>a;
node *p=new node;
p->data=a;
p->next=edge[m].next;
edge[m].next=p;
node *q=new node;
q->data=m;
q->next=edge[a].next;
edge[a].next=q;
}
}
dfs(1);
int sum=0;
if(time[1]>1)sum++;
for(int i=2; i<=n; i++)
if(time[i]>0)
sum++;
cout<<sum<<endl;
}
return 0;
}
二
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define maxn 105
#define maxm 10005
int dfn[maxn],low[maxn],head[maxn],vis[maxn];
int cut[maxn];
int e,m,n,root;
struct node
{
int to;
int next;
} edge[maxm];
int min(int a,int b)
{
return a<b?a:b;
}
void init()
{
e=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(head,-1,sizeof(head));
memset(cut,0,sizeof(cut));
memset(vis,0,sizeof(vis));
}
void addedge(int u,int v)
{
edge[e].to=v;
edge[e].next=head[u];
head[u]=e++;
}
void dfs(int s,int father,int dep)
{
int son=0;
vis[s]=1;
dfn[s]=low[s]=dep;
for(int i=head[s]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(vis[v]==1&&v!=father)
{
low[s]=min(low[s],dfn[v]);
}
if(!vis[v])
{
dfs(v,s,dep+1);
son++;
low[s]=min(low[s],low[v]);
if((s==root&&son>1)||(s!=root&&dfn[s]<=low[v]))
cut[s]=1;
}
}
vis[s]=2;
}
int main()
{
int a;
while(cin>>m,m)
{
init();
while(cin>>n,n)
{
while(getchar()!='\n')
{
cin>>a;
addedge(n,a);
addedge(a,n);
}
}
root=1;
dfs(1,-1,1);
int sum=0;
for(int i=1; i<=m; i++)
if(cut[i])
sum++;
cout<<sum<<endl;
}
}