Though the language of the question might sound uneasy. It asks simply if we can go from interval (a,b) to (c,d). The condition of travelling from (a,b) to (c,d) is if c<a<d or c<b<d. Remeber if we can travel from (a,b) to (c,d) that doesnot necessarily mean we can travel from (c,d) to (a,b) . An example of such a case is if(a,b) entirely lies in the interval (c,d). Now the question is divided into two parts (1) adding an interval Here eaceh interval is represented as a node. Like (1,5) is node 1 , (5,11) is node 2. We push (a,b) to vector and check throughout the vector if (a,b) lies between any interval already present in the set of intervals. If present we add a directed edge between interval (a,b) to (c,d). We also check if we can travel from (c,d) to (a,b) [if((intervals[i].ff>a && intervals[i].ff<b )||(intervals[i].ss>a && intervals[i].ss<b))] . If we can also travel from (c,d) to (a,b) then we add a directed edge between (c,d) to (a,b). (2) Second part is the query part where we do a simple DFS/BFS traversal from the given interval(represented as node) to the second interval . If reachable we print YES else we print NO. The code is as follows *** code begins here** #include<iostream> using namespace std; #include<bits/stdc++.h> vector<pair<long long int ,long long int > >v; list<long long int >li[10001]; int main() { long long int q; cin>>q; long long int ad=-1; while(q--) { long long int op,a,b; cin>>op>>a>>b; if(op==1) { ad++; v.push_back(make_pair(a,b)); //cout<<"ad "<<ad<<endl; for(long long int i=0;i<=ad-1;i++) { if((a>v[i].first && a<v[i].second) || (b>v[i].first && b<v[i].second)) { //cout<<"pairing "<<i<<" "<<ad<<endl; li[ad].push_back(i); } if( (v[i].first>a && v[i].first<b) || (v[i].second>a && v[i].second<b) ) { li[i].push_back(ad); } } } else { long long int visited[ad+10]; for(long long int i=0;i<ad+10;i++) visited[i]=0; long long int start=a-1; long long int end=b-1; stack<long long int > s; s.push(a-1); visited[a-1]=1; long long int f=0; while(!s.empty()) { long long int start=s.top(); s.pop(); list<long long int > :: iterator it; for(it=li[start].begin();it!=li[start].end();it++) { //cout<<"*it"<<*it<<endl; if(visited[*it]==0) { // cout<<"visiting "<<*it<<endl; visited[*it]=1; if(*it==end) { cout<<"YES"<<endl; f=1; break; } else { // cout<<"push"<<*it<<endl; s.push(*it); } } } if(f==1) break; } if(f==0) cout<<"NO"<<endl; } } return 0; }
Friday, 15 May 2015
Though the language of the question might sound uneasy. It asks simply if we can go from interval (a,b) to (c,d). The condition of travelling from (a,b) to (c,d) is if c<a<d or c<b<d. Remeber if we can travel from (a,b) to (c,d) that doesnot necessarily mean we can travel from (c,d) to (a,b) . An example of such a case is if(a,b) entirely lies in the interval (c,d). Now the question is divided into two parts (1) adding an interval Here eaceh interval is represented as a node. Like (1,5) is node 1 , (5,11) is node 2. We push (a,b) to vector and check throughout the vector if (a,b) lies between any interval already present in the set of intervals. If present we add a directed edge between interval (a,b) to (c,d). We also check if we can travel from (c,d) to (a,b) [if((intervals[i].ff>a && intervals[i].ff<b )||(intervals[i].ss>a && intervals[i].ss<b))] . If we can also travel from (c,d) to (a,b) then we add a directed edge between (c,d) to (a,b). (2) Second part is the query part where we do a simple DFS/BFS traversal from the given interval(represented as node) to the second interval . If reachable we print YES else we print NO. The code is as follows *** code begins here** #include<iostream> using namespace std; #include<bits/stdc++.h> vector<pair<long long int ,long long int > >v; list<long long int >li[10001]; int main() { long long int q; cin>>q; long long int ad=-1; while(q--) { long long int op,a,b; cin>>op>>a>>b; if(op==1) { ad++; v.push_back(make_pair(a,b)); //cout<<"ad "<<ad<<endl; for(long long int i=0;i<=ad-1;i++) { if((a>v[i].first && a<v[i].second) || (b>v[i].first && b<v[i].second)) { //cout<<"pairing "<<i<<" "<<ad<<endl; li[ad].push_back(i); } if( (v[i].first>a && v[i].first<b) || (v[i].second>a && v[i].second<b) ) { li[i].push_back(ad); } } } else { long long int visited[ad+10]; for(long long int i=0;i<ad+10;i++) visited[i]=0; long long int start=a-1; long long int end=b-1; stack<long long int > s; s.push(a-1); visited[a-1]=1; long long int f=0; while(!s.empty()) { long long int start=s.top(); s.pop(); list<long long int > :: iterator it; for(it=li[start].begin();it!=li[start].end();it++) { //cout<<"*it"<<*it<<endl; if(visited[*it]==0) { // cout<<"visiting "<<*it<<endl; visited[*it]=1; if(*it==end) { cout<<"YES"<<endl; f=1; break; } else { // cout<<"push"<<*it<<endl; s.push(*it); } } } if(f==1) break; } if(f==0) cout<<"NO"<<endl; } } return 0; }
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;
}
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 ≤ n; si ≠ 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 edgesthat 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;
}
Restore Graph
########################editorial########################
finally if answer exist than for the formation of graph connect node with dist x to node with dist x-1, (but alco check that if node with dist x-1 must connect with maximum k no of node for that do hassing of node connected with all nodes )......
################CODE############
Valera had an undirected connected graph without self-loops and multiple edges consisting of n vertices. The graph had an interesting property: there were at most k edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to n.
One day Valera counted the shortest distances from one of the graph vertices to all other ones and wrote them out in array d. Thus, element d[i] of the array shows the shortest distance from the vertex Valera chose to vertex number i.
Then something irreparable terrible happened. Valera lost the initial graph. However, he still has the array d. Help him restore the lost graph.
Input
The first line contains two space-separated integers n and k (1 ≤ k < n ≤ 105). Number n shows the number of vertices in the original graph. Number k shows that at most k edges were adjacent to each vertex in the original graph.
The second line contains space-separated integers d[1], d[2], ..., d[n] (0 ≤ d[i] < n). Number d[i] shows the shortest distance from the vertex Valera chose to the vertex number i.
Output
If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer m (0 ≤ m ≤ 106) — the number of edges in the found graph.
In each of the next m lines print two space-separated integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi), denoting the edge that connects vertices with numbers ai and bi. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them.
Sample test(s)
input
3 2 0 1 1
output
3 1 2 1 3 3 2
input
4 2 2 0 1 3
output
3 1 3 1 4 2 3
input
3 1 0 0 0
output
-1
in this problem 1 st need to decide whether it is possinble to form a graph or not .. for that node with dist 0 (starting )can form k no of connections from from node with dist 1 now all node with dist 1 can form maximum k-1 connections with node of dist k-1 , (sice that noode is already connected with node with dist 0 ) and so on ...
finally if answer exist than for the formation of graph connect node with dist x to node with dist x-1, (but alco check that if node with dist x-1 must connect with maximum k no of node for that do hassing of node connected with all nodes )......
################CODE############
#include<iostream>
using namespace std;
long long int has[10001000];
long long int cnt[10001000];
long long int cp[10001000];
#include<bits/stdc++.h>
int main()
{
long long int n,k;
long long int max_level=0;
cin>>n>>k;
vector<pair<long long int,long long int> > v;
long long int start=0;
for(long long int i=0;i<n;i++)
{
long long int zz;
cin>>zz;
v.push_back(make_pair(zz,i+1));
if(zz==0) start=i+1;
has[zz]++;// count of occurance of dist x
if(zz>max_level) max_level=zz;
}
sort(v.begin(),v.end());
if(has[1]>has[0]*k || has[0]>1 || has[0]==0)
{
cout<<"-1"<<endl;
return 0;
}
cp[0]=0;
cp[1]=1;
long long int kk=2;
for(long long int i=2;i<=n;i++)
{
if(has[i]>has[i-1]*(k-1))
{
// cout<<"here"<<endl;
cout<<"-1"<<endl;
return 0;
}
if(v[i].first!=v[i-1].first) cp[kk++]=i;// cp contain the occurance //start of new dist
}
kk=-1;
long long int index=0;
cout<<n-1<<endl;
for(long long int i=1;i<n;i++)
{
long long int no=v[i].second;
long long int dist=v[i].first;
if(v[i].first!=v[i-1].first)
{
kk++;
index=cp[kk];
}
if(cnt[v[index].second]>=k) index++;// if index no node is //connected with k nodes
cout<<v[index].second<<" "<<v[i].second<<endl;
cnt[v[index].second]++;
cnt[v[i].second]++;
}
return 0;
}
Subscribe to:
Posts (Atom)