[关闭]
@jesseliu612 2016-07-06T23:06:35.000000Z 字数 4235 阅读 1242

POJ1144(无向图求割点模版题)

模版题 20160706


题目

Network

Time Limit: 1000MS
Memory Limit: 10000K

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.

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;

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.

解题方法

由于高三学长已经透露了这道题是一道求割点的模版题,所以可以直接上模版。
这是AC的代码

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <vector>
  8. using namespace std;
  9. const int maxn=1000;
  10. int dfn[maxn],low[maxn],father[maxn];
  11. bool edge[maxn][maxn]={0};
  12. vector < int > e[maxn],son[maxn];
  13. int top,dnow,n,t,t2;
  14. void tarjan(int x){
  15. if(dfn[x]==-1){
  16. dfn[x]=dnow;
  17. dnow++;
  18. low[x]=dfn[x];//由于是无向图,这里可以不使用stack来保存信息
  19. for(int i=0;i<e[x].size();i++){
  20. int j=e[x][i];
  21. if(edge[x][j]==0){
  22. if(dfn[j]==-1){
  23. tarjan(j,x);
  24. edge[j][x]=1;
  25. edge[x][j]=1;
  26. son[x].push_back(j);
  27. father[j]=x;
  28. low[x]=min(low[x],low[j]);
  29. }
  30. else if(j!=x){
  31. low[x]=min(low[x],dfn[j]);
  32. }
  33. }
  34. }
  35. }
  36. }
  37. int main(){
  38. freopen("1.in","r",stdin);
  39. freopen("1.out","w",stdout);
  40. while(scanf("%d",&n)==1 && n>0){
  41. for(int i=0;i<=n;i++){
  42. dfn[i]=-1;
  43. low[i]=-1;
  44. }
  45. memset(e,0,sizeof(e));
  46. memset(son,0,sizeof(son));
  47. memset(instack,0,sizeof(instack));
  48. memset(edge,0,sizeof(edge));
  49. top=0;
  50. dnow=1;
  51. while(scanf("%d",&t)==1 && t>0){
  52. char ch=getchar();
  53. while(ch!='\n'){
  54. scanf("%d",&t2);
  55. e[t].push_back(t2);
  56. e[t2].push_back(t);
  57. ch=getchar();
  58. }
  59. }
  60. tarjan(1,0);
  61. int ans=0;
  62. for(int i=1;i<=n;i++){
  63. //printf("i=%d\n",i);
  64. for(int j=0;j<son[i].size();j++){
  65. if(son[i][j]>=0 && low[son[i][j]] >= dfn[i] && i!=1){
  66. ans++;
  67. break;
  68. }
  69. }
  70. if(i==1 && son[i].size()>1){
  71. ans++;
  72. }
  73. }
  74. printf("%d\n",ans);
  75. }
  76. return 0;
  77. }

第一次WA的提交

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <vector>
  8. using namespace std;
  9. const int maxn=1000;
  10. int stack[maxn],dfn[maxn],low[maxn];
  11. bool instack[maxn]={0};
  12. vector < int > e[maxn],son[maxn];
  13. int top,dnow,n,t,t2;
  14. void tarjan(int x){
  15. if(dfn[x]==-1){
  16. //printf("tarjan(%d)\n",x);
  17. dfn[x]=dnow;
  18. dnow++;
  19. low[x]=dfn[x];
  20. stack[top]=x;
  21. top++;
  22. instack[x]=1;
  23. for(int i=0;i<e[x].size();i++){
  24. int j=e[x][i];
  25. //printf("j=%d\n",j);
  26. if(dfn[j]==-1){
  27. //printf("dfn[j]==-1\n");
  28. tarjan(j);
  29. son[x].push_back(j);
  30. for(int k=0;k<son[x].size();k++){
  31. //printf("son[%d][%d]=%d\n",x,k,son[x][k]);
  32. }
  33. low[x]=min(low[x],low[j]);
  34. }
  35. else if(instack[j]==1){
  36. low[x]=min(low[x],low[j]);
  37. }
  38. }
  39. top--;
  40. while(stack[top]!=x){
  41. top--;
  42. }
  43. }
  44. }
  45. int main(){
  46. while(scanf("%d",&n)==1 && n>0){
  47. //printf("while(%d)\n",n);
  48. for(int i=0;i<=n;i++){
  49. dfn[i]=-1;
  50. low[i]=-1;
  51. }
  52. memset(e,0,sizeof(e));
  53. memset(son,0,sizeof(son));
  54. memset(instack,0,sizeof(instack));
  55. top=0;
  56. dnow=1;
  57. while(scanf("%d",&t)==1 && t>0){
  58. //printf("while(**%d)\n",t);
  59. char ch=getchar();
  60. while(ch!='\n'){
  61. scanf("%d",&t2);
  62. //printf("***%d\n",t2);
  63. e[t].push_back(t2);
  64. e[t2].push_back(t);
  65. ch=getchar();
  66. }
  67. }
  68. for(int i=1;i<=n;i++){
  69. tarjan(i);
  70. }
  71. int ans=0;
  72. for(int i=1;i<=n;i++){
  73. //printf("i=%d\n",i);
  74. for(int j=0;j<son[i].size();j++){
  75. if(low[son[i][j]] >= dfn[i]){
  76. ans++;
  77. break;
  78. }
  79. //printf("low[%d]=%d, dfn[%d]=%d\n",son[i][j],low[son[i][j]],i,dfn[i]);
  80. }
  81. }
  82. printf("%d\n",ans);
  83. }
  84. return 0;
  85. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注