fork download
  1.  
  2. #include<stdio.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. struct node
  8. {
  9. int node_no;
  10. struct node * next;
  11.  
  12. };
  13. int start,visited[100001]={0},v,e,degree[100001]={0},par[100001];
  14. struct node *temp,*node1,*node2,*arr[100001];
  15. void bipartite();
  16. void count();
  17. int main()
  18. {
  19. // cout<<"give the no of vertices\n";
  20. cin>>v;
  21. //cout<<"give the noof edges\n";
  22. // cin>>e;
  23. e=v-1;
  24. for(int i=0;i<v;i++) arr[i]=NULL;
  25. // cout<<"give the graph x and y i.e x---->y \n";
  26. for(int i=0;i<e;i++)
  27. {
  28. int x,y;
  29. cin>>x>>y;
  30. node1 =(struct node *)malloc(sizeof(struct node));
  31. node1->node_no=y-1;
  32. temp=arr[x-1];
  33. arr[x-1]=node1;
  34. par[y-1]=x-1;
  35.  
  36. node1->next=temp;
  37. node1 =(struct node *)malloc(sizeof(struct node));
  38. node1->node_no=x-1;
  39. temp=arr[y-1];
  40. arr[y-1]=node1;
  41. par[x-1]=y-1;
  42.  
  43. node1->next=temp;
  44. degree[y-1]++;
  45. degree[x-1]++;
  46.  
  47.  
  48. }
  49. /* cout<<"graph formed is \n";
  50.   for(int i=0;i<v;i++)
  51.   {
  52.   cout<<i<<"------>>";
  53.   temp=arr[i];
  54.   while(temp!=NULL)
  55.   {
  56.   cout<<temp->node_no<<"--";
  57.   temp=temp->next;
  58.   }
  59.   cout<<endl;
  60.   }
  61.   cout<<"fun calling\n";*/
  62. count();
  63. return 0;
  64.  
  65. }
  66.  
  67.  
  68.  
  69. void count()
  70. {
  71. int i=0;
  72. int coun=0,visited=0,top=-1;
  73. queue<int>q;
  74.  
  75.  
  76. for(int i=0;i<v;i++)
  77. {
  78. if(degree[i]==1)
  79. {
  80. q.push(i);
  81. visited[i]=1;
  82. }
  83. }
  84.  
  85.  
  86.  
  87. while(!q.empty())
  88. {
  89.  
  90.  
  91. int k=q.front();
  92. q.pop();
  93. if(visited[par[k]]==0)
  94. {
  95. degree[par[k]]--;
  96. coun++;
  97. if(degree[par[k]]==1) q.push(par[k]);
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. }
  110. cout<<"count is "<<coun;
  111.  
  112. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void count()’:
prog.cpp:81:14: error: invalid types ‘int[int]’ for array subscript
     visited[i]=1;
              ^
prog.cpp:93:26: error: invalid types ‘int[int]’ for array subscript
         if(visited[par[k]]==0)
                          ^
prog.cpp:71:8: warning: unused variable ‘i’ [-Wunused-variable]
    int i=0;
        ^
prog.cpp:72:25: warning: unused variable ‘top’ [-Wunused-variable]
   int  coun=0,visited=0,top=-1;
                         ^
stdout
Standard output is empty