**Black and White Tree**

### Problem Statement :

Nikita is making a graph as a birthday gift for her boyfriend, a fellow programmer! She drew an undirected connected graph with N nodes numbered from 1 to N in her notebook. Each node is shaded in either white or black. We define nW to be the number of white nodes, and nB to be the number of black nodes. The graph is drawn in such a way that: No 2 adjacent nodes have same coloring. The value of |nW - nB|, which we'll call D, is minimal. Nikita's mischievous little brother erased some of the edges and all of the coloring from her graph! As a result, the graph is now decomposed into one or more components. Because you're her best friend, you've decided to help her reconstruct the graph by adding K edges such that the aforementioned graph properties hold true. Given the decomposed graph, construct and shade a valid connected graph such that the difference |nW - nB| between its shaded nodes is minimal. Input Format The first line contains 2 space-separated integers, N (the number of nodes in the original graph) and M (the number of edges in the decomposed graph), respectively. The M subsequent lines each contain2 space-separated integers, u and v, describing a bidirectional edge between nodes u and v in the decomposed graph. Constraints 1 <= N <= 2*10^5 0 <= M <= min(5*10^5, (N *(N-1))/2) It is guaranteed that every edge will be between 2 distinct nodes, and there will never be more than 1 edge between any 2 nodes. Your answer must meet the following criteria: The graph is connected and no 2 adjacent nodes have the same coloring. The value of |nB - nW| is minimal. K <= 2*10^5 Output Format You must have K+1 lines of output. The first line contains 2 space-separated integers: D (the minimum possible value of |nB - nW|) and K (the number of edges you've added to the graph), respectively. Each of the K subsequent lines contains 2 space-separated integers, u and v, describing a newly-added bidirectional edge in your final graph (i.e.: new edge u <-> v). You may print any 1 of the possible reconstructions of Nikita's graph such that the value of D in the reconstructed shaded graph is minimal.

### Solution :

` ````
Solution in C :
In C++ :
#include <bits/stdc++.h>
using namespace std;
#define ft first
#define sd second
#define pb push_back
#define all(x) x.begin(),x.end()
#define ll long long int
#define vi vector<int>
#define vii vector<pair<int,int> >
#define pii pair<int,int>
#define vl vector<ll>
#define vll vector<pair<ll,ll> >
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define mp make_pair
#define sc1(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scll1(x) scanf("%lld",&x)
#define scll2(x,y) scanf("%lld%lld",&x,&y)
#define scll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pr1(x) printf("%d\n",x)
#define pr2(x,y) printf("%d %d\n",x,y)
#define pr3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prll1(x) printf("%lld\n",x)
#define prll2(x,y) printf("%lld %lld\n",x,y)
#define prll3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define pr_vec(v) for(int i=0;i<v.size();i++) cout << v[i] << " " ;
#define f_in(st) freopen(st,"r",stdin)
#define f_out(st) freopen(st,"w",stdout)
#define fr(i, a, b) for(i=a; i<=b; i++)
#define fb(i, a, b) for(i=a; i>=b; i--)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int N=200010;
int w[2];
vector <int> v[N];
int a[N], d[N], p[N], c[N], col[N], sz, ccol[2][N];
bool f[N];
queue<int> q[N];
void dfs(int x,int y)
{
f[x]=1;
w[y]++;
col[x] = y;
ccol[y][sz] = x;
for (int i=0;i<v[x].size();i++)
if (!f[v[x][i]])
dfs(v[x][i],(y^1));
else if( col[v[x][i]] != 1 - y )
assert(0);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=0,x,y;i<m;i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
int k=0;
for (int i=1;i<=n;i++)
{
if (!f[i])
{
w[0] = w[1]=0;
dfs(i,0);
c[i] = (w[0] < w[1]);
k+=abs(w[0]-w[1]);
a[abs(w[0]-w[1])]++;
q[abs(w[0]-w[1])].push( i );
}
}
for (int j=1;j<=k;j++)
d[j]=N;
for (int i=1;i<=n;i++)
{
if (a[i])
{
for (int j=0;j+i<=k;j++)
{
if( d[i+j] == N && d[j] != N )
{
d[i+j] = d[j] + 1;
p[i+j] = j;
}
}
for (int j=1;j<=k;j++)
{
if (d[j]>a[i])
{
d[j]=N;
p[j]=0;
}
else
{
d[j]=0;
}
}
}
}
int ans=k, v = 0;
for (int i=0;i<=k;i++)
{
if(d[i]<N)
{
if( ans >= abs(k - 2 * i) )
{
ans = abs(k - 2 * i);
v = i;
}
}
}
memset(f, 0, sizeof f);
while( v != 0 )
{
int diff = v - p[v];
int nd = q[diff].front();
q[diff].pop();
sz ++;
dfs(nd, c[nd]);
v = p[v];
}
for(int i=1; i<=n; i++)
{
if( !f[i] )
{
sz ++;
dfs(i, 1 - c[i]);
}
}
int blk, wht, idb, idw;
blk = wht = idb = idw = -1;
for(int i=1; i<=sz; i++)
{
if( ccol[0][i] )
{
blk = ccol[0][i];
idb = i;
}
if( ccol[1][i] )
{
wht = ccol[1][i];
idw = i;
}
}
cout << ans << " " << sz - 1 << "\n";
if( idb != idw ) cout << blk << " " << wht << "\n";
for(int i=1; i<=sz; i++)
{
if( i != idb && i != idw )
{
if( ccol[0][i] )
{
cout << wht << " " << ccol[0][i] << "\n";
}
else
{
cout << blk << " " << ccol[1][i] << "\n";
}
}
}
return 0;
}
```

## View More Similar Problems

## Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →## No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →## Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →## Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →## Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →## Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →