Problem link – http://codeforces.com/contest/501/problem/C
This is an interesting graph problem 🙂
Since the graph is acyclic, there will be leave(s) (with degree = 1). Identify those leaves. Consider a leaf i.Its xor value (let it be j) will directly give you its neighbour(since xor of a number is the number itself).Add (i,j) as an edge to the ans vector. Now decrement the degree of the node j and also apply xor to j’s xor sum value to remove i. After removing leaf i from the forest, the forest will still remain a forest . So similarly find the leaves in the current forest (and also the edges which is why we are doing this) and carefully prune the leaves until the forest becomes empty.
#include <vector> | |
#include <list> | |
#include <map> | |
#include <set> | |
#include <deque> | |
#include <queue> | |
#include <stack> | |
#include <bitset> | |
#include <algorithm> | |
#include <functional> | |
#include <numeric> | |
#include <utility> | |
#include <sstream> | |
#include <iostream> | |
#include <iomanip> | |
#include <cstdio> | |
#include <cmath> | |
#include <cstdlib> | |
#include <ctime> | |
#include <cstring> | |
#include <climits> | |
#include <stdlib.h> | |
#include <stdio.h> | |
using namespace std; | |
#define REP(i,n) for(int i=0; i<n; i++) | |
#define FOR(i,st,end) for(int i=st;i<end;i++) | |
#define db(x) cout << (#x) << " = " << x << endl; | |
#define mp make_pair | |
#define pb push_back | |
typedef long long int ll; | |
int main(){ | |
int n,v,s; | |
vector<pair<int,int> > arr,ans; | |
scanf("%d",&n); | |
vector<int>leaves; | |
REP(i,n){ | |
scanf("%d%d",&v,&s); | |
arr.pb(mp(v,s)); | |
if(v==1) | |
leaves.pb(i); | |
} | |
int l=0; | |
while(l<leaves.size()){ | |
if(arr[leaves[l]].first==1){ | |
ans.pb(mp(leaves[l],arr[leaves[l]].second)); | |
arr[arr[leaves[l]].second].first--; | |
if(arr[arr[leaves[l]].second].first==1) | |
leaves.pb(arr[leaves[l]].second); | |
arr[arr[leaves[l]].second].second^=leaves[l]; | |
arr[leaves[l]].first--; | |
} | |
l++; | |
} | |
printf("%d\n",ans.size()); | |
REP(i,ans.size()){ | |
printf("%d %d\n",ans[i].first,ans[i].second); | |
} | |
} | |