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
Balanced Brackets
A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra
View Solution →Equal Stacks
ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of
View Solution →Game of Two Stacks
Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f
View Solution →Largest Rectangle
Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle
View Solution →Simple Text Editor
In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,
View Solution →Poisonous Plants
There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan
View Solution →