Saturday, 9 May 2015

QUESTION -----

 U HAVE GIVEN A GRAPH FIND CIRCLE IN  THE GRAPH AND COST OF ALL NODES WHICH TAKE PART IN CIRCLE IS ZERO (0) .. AND COST OF REST NODES IS DISTANCE OF NODE FROM ANY NODE OF THE CIRCLE .........
-------------------------------------------CODEFORCE QUESTION---------------------------------------------
  D. Subway
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A subway scheme, classic for all Berland cities is represented by a set of n stations connected by n passages, each of which connects exactly two stations and does not pass through any others. Besides, in the classic scheme one can get from any station to any other one along the passages. The passages can be used to move in both directions. Between each pair of stations there is no more than one passage.
Berland mathematicians have recently proved a theorem that states that any classic scheme has a ringroad. There can be only one ringroad. In other words, in any classic scheme one can find the only scheme consisting of stations (where any two neighbouring ones are linked by a passage) and this cycle doesn't contain any station more than once.
This invention had a powerful social impact as now the stations could be compared according to their distance from the ringroad. For example, a citizen could say "I live in three passages from the ringroad" and another one could reply "you loser, I live in one passage from the ringroad". The Internet soon got filled with applications that promised to count the distance from the station to the ringroad (send a text message to a short number...).
The Berland government decided to put an end to these disturbances and start to control the situation. You are requested to write a program that can determine the remoteness from the ringroad for each station by the city subway scheme.
Input
The first line contains an integer n (3 ≤ n ≤ 3000), n is the number of stations (and trains at the same time) in the subway scheme. Then n lines contain descriptions of the trains, one per line. Each line contains a pair of integers xi, yi (1 ≤ xi, yi ≤ n) and represents the presence of a passage from station xi to station yi. The stations are numbered from 1 to n in an arbitrary order. It is guaranteed that xi ≠ yi and that no pair of stations contain more than one passage. The passages can be used to travel both ways. It is guaranteed that the given description represents a classic subway scheme.
Output
Print n numbers. Separate the numbers by spaces, the i-th one should be equal to the distance of the i-th station from the ringroad. For the ringroad stations print number 0.
Sample test(s)
input
4
1 3
4 3
4 2
1 2
output
0 0 0 0 
input
6
1 2
3 4
6 4
2 3
1 3
3 5
output
0 0 0 1 1 2 

H
-------------------------------------EDITORIAL-------------------------------------------------------------------

FISRT WE NEED TO DETERMINE NODES WHICH TAKE PART IN THE CIRCLE FOR THAT WRITE A RECURSIVE CODE WHICH CHECK  NODES WHICH TAKE PART IN THE CIRCLE ..
  NOTE THAT ALL NODES IN THE RECURSION STACK IS NOTE THE PART OF THE CIRCLE SO THAT WE KEEP TRACK OF PARENT OF ALL NODE TO CHECK THE  NODES WHICH TAKE PART   IN THE CIRCLE ....
  NOW APPLY BFS FROM ANY OF THE NODE IN THE CIRCLE AND FIX DISTANCE  OF REST ALL NODES WHICH IS NOT THE PART OF THE CIRCLE ALSO SET THE DISTANCE OF NODES WHICH ARE PART OF THE CIRCLE=0 ...


-------------------------------------------------CODE---------------------------------------------------------------


#include<iostream>
using namespace std;
#include<bits/stdc++.h>
list<int > li[100001];
int rec[100001];
int visited[1000001];
int f=1;
int par[1000000];
int final=-1;
int final2=-1;
int all[1000001];
int dist[100001];
int vis[10001];
int solv(int start)
 {
  //cout<<start<<endl;
 
   list<int>::iterator it;
   for(it=li[start].begin();it!=li[start].end();it++)
    {
  //    cout<<"start "<<start<<" new "<<*it<<endl;
    
    if(rec[*it]==1  && par[start]!=*it)
    {
    ///cout<<"found circle and "<<*it<<" is a part "<<endl;
    f=0;
    final=start;
    final2=*it;
    return 1;
    }
    else if(!visited[*it])
    {
    if(f==0) return 1;
    //cout<<"rec on "<<*it<<endl;
    par[*it]=start;
    rec[*it]=1;
    visited[*it]=1;
    solv(*it);
    if(f==0) return 1;
    }
     
     
    }
    //cout<<"removing "<<start<<endl;
    if(f==0) return 1;   
    rec[start]=0;
    
// cout<<"removing "<<start<<endl;
    
 }
int main()
 {
  int n;
  cin>>n;
    for(int i=0;i<n;i++)
     {
      int a,b;
      cin>>a>>b;
       li[a].push_back(b);
       li[b].push_back(a);
     }
     for(int i=1;i<=n;i++)
      {
      if(!visited[i])
      {
      visited[i]=1;
      rec[i]=1;
      par[i]=-1;
      int rep=solv(i);
      if(rep==1) break;
      }
      }
      // backtraking....
      int start=final;
      all[final2]=1;
      while(start!=final2)
       {
         all[start]=1;
         start=par[start];
       }
      
      
      
       queue<pair<int ,int > > q;
       q.push(make_pair(final2,0));
       vis[final2]=1;
       while(!q.empty())
        {
         int start=q.front().first;
         int dis=q.front().second;
         q.pop();
           list<int >:: iterator it;
           for(it=li[start].begin();it!=li[start].end();it++)
            
            {
            if(!vis[*it] )
            {
            vis[*it]=1;
            if(all[*it]==1)
 {
  dist[*it]=0;
  q.push(make_pair(*it,0));
 } 
 else
 {
  dist[*it]=dis+1;
  q.push(make_pair(*it,dis+1));
 
 }
             
             
            }
            }
        }
        
        for(int i=1;i<=n;i++)
         {
          if(all[i]==1) cout<<"0"<<" ";
          else 
           cout<<dist[i]<<" ";
         }
      return 0;
 }

No comments:

Post a Comment