Sunday, 3 May 2015

                                                    Choosing Capital for Treeland
@@ short explanation -- u are give a graph find the vertex from  which u can visit rest all vertice by minimum no or time inverting edge (inverting since it is a directed graph )@@@ 
The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.
The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a to any other city. For that some roads may have to be inversed.
Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.
Input
The first input line contains integer n (2 ≤ n ≤ 2·105) — the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers si, ti (1 ≤ si, ti ≤ nsi ≠ ti) — the numbers of cities, connected by that road. The i-th road is oriented from city si to city ti. You can consider cities in Treeland indexed from 1 to n.
Output
In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.
Sample test(s)
input
3
2 1
2 3
output
0
2 
input
4
1 4
2 4
3 4
output
2
1 2 3 

######################EDITORIAL########################
Arbitrarily root the tree at some vertex, say vertex 1. Now, all the edges are oriented either up (towards the root) or down (away from it). We will call upwards oriented edges red, and downwards oriented edges green. Now, with a single depth-first search, for each vertex, calculate its distance from the root (in number of edges) and the number of red edges along the path to the root. Also, count the number of red edges in the entire tree.
Actually when we are traversing down the line we are looking for coming back in the  opposite path. Hence all  green branches should be red and all red branches should be turned green.
Now comes the interesting part: Observe that all edges outside the path from the root to vert should turn green, and those on the path should turn red.
The number of edges that need to be flipped if vert is chosen as a capital is given by:
RedEntireTree-- 2*RedOnPath[vert] + RootDistance[vert]
This formula can be interpreted as (length of the path-number of  red along the path)
+(total amount of red -red along the path). The first part of the formula is the number 
of green  edges on the path and the second part of the formula is the number of red edges
that are not in the path . The  summation of the two gives the total changes.

######################EDITORIAL#####################################################
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
list<pair<int,int> > li[10000001];
int red=0;
int blue=0;
int dist[1000001];
int vis[10000001];
int ans[10000001];
int pr[1000000];
int bfs(int start)
 {
  vis[start]=1;
  queue<pair<pair<int,int>,int > > q;
  q.push(make_pair(make_pair(start,0),0));
   
    while(!q.empty())
    {
    int start=q.front().first.first;
    int dd=q.front().first.second;
    int colour=q.front().second;
    q.pop();
    list<pair<int,int>  > :: iterator it;
    for(it=li[start].begin();it!=li[start].end();it++)
    {
    if(it->second==1 && vis[it->first]==0)
    {
    dist[it->first]=dd+1;
    vis[it->first]=1;
    red++;
    pr[it->first]=colour+1;
    q.push(make_pair(make_pair(it->first,dd+1),colour+1));
    }
    else if(vis[it->first]==0)
     {
      pr[it->first]=colour;
      vis[it->first]=1;
      dist[it->first]=dd+1;
      q.push(make_pair(make_pair(it->first,dd+1),colour));
     
     }
     
    }
   
    }
   }

int main()
 {
 
  int n;
  cin>>n;
  int a,b;
   for(int i=0;i<n-1;i++)
    {
      
      cin>>a>>b;
      li[a].push_back(make_pair(b,0));
      li[b].push_back(make_pair(a,1));
     
    }
  bfs(1);
  int min=10000001;
 
  for(int i=1;i<=n;i++)
   {
    ans[i]=red-2*pr[i]+dist[i];
    if(ans[i]<min) min=ans[i];
   }
   
    cout<<min<<endl;
    for(int i=1;i<=n;i++)
     if(ans[i]==min) cout<<i<<" ";
 
return 0;
   
 }

No comments:

Post a Comment