POJ 3177 Redundant Paths(Tarjan)

题目链接

看这个题解:http://blog.csdn.net/zhang20072844/article/details/8145588

对于Tarjan还是不太理解。

 1 #include <iostream>

 2 #include <cstring>

 3 #include <cstdio>

 4 #include <cstdlib>

 5 using namespace std;

 6 #define N 10001

 7 #define M 50001

 8 struct node

 9 {

10     int u,v,next;

11 } edge[M+10];

12 int t,top,scnt;

13 int DFN[N+1],Low[N+1],first[N+1];

14 int cnt[N+1];

15 bool mat[5001][5001];

16 void CL()

17 {

18     t = 0;

19     t = scnt = top = 0;

20     memset(first,-1,sizeof(first));

21     memset(DFN,0,sizeof(DFN));

22     memset(mat,0,sizeof(mat));

23 }

24 void add(int u,int v)

25 {

26     edge[t].u = u;

27     edge[t].v = v;

28     edge[t].next = first[u];

29     first[u] = t ++;

30 }

31 void Tarjan(int u,int father)

32 {

33     int v,i;

34     DFN[u] = Low[u] = ++ t;

35     for(i = first[u]; i != -1; i = edge[i].next)

36     {

37         v = edge[i].v;

38         if(!DFN[v])

39         {

40             Tarjan(v,u);

41             if(Low[u] > Low[v])

42             {

43                 Low[u] = Low[v];

44             }

45         }

46         else if(v != father&&Low[u] > DFN[v])

47         {

48             Low[u] = DFN[v];

49         }

50     }

51 }

52 int main()

53 {

54     int n,m,sv,ev,i,j,v,ans;

55     while(scanf("%d%d",&n,&m)!=EOF)

56     {

57         CL();

58         memset(cnt,0,sizeof(cnt));

59         for(i = 1; i <= m; i ++)

60         {

61             scanf("%d%d",&sv,&ev);

62             if(!mat[sv][ev])

63             {

64                 add(sv,ev);

65                 add(ev,sv);

66                 mat[sv][ev] = mat[ev][sv] = true;

67             }

68         }

69         t = 0;

70         Tarjan(1,1);

71         for(i = 1;i <= n;i ++)

72         {

73             for(j = first[i];j != -1;j = edge[j].next)

74             {

75                 v = edge[j].v;

76                 if(Low[v] != Low[i])

77                 cnt[Low[i]] ++;

78             }

79         }

80         ans = 0;

81         for(i = 1;i <= n;i ++)

82         {

83             if(cnt[i] == 1)

84             ans ++;

85         }

86         printf("%d\n",(ans+1)/2);

87     }

88     return 0;

89 }

 

你可能感兴趣的:(Path)