这个题的题意是真的尬,异或值是由该结点相连结点的编号一起异或得来的。
容易想到,先处理叶节点较好,因为叶节点只能由一个父亲,其异或值就是父亲
结点的编号了,然后每次处理一个结点就把与它相连的结点的异或值,度数也跟着
一起处理一次,如果该结点成为了一个新的叶节点(事实上只是度数改变为1了)那么
就加入待处理的队列中即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <cstring>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define MAX (1 << 16) + 5
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
int degree[MAX], s[MAX];
bool vis[MAX];
struct node{
int degree;
int v;
};
struct road {
int to, from;
};
queue<node> q;
stack<road> st;
int main()
{
// freopen("a.txt", "r", stdin);
// freopen("b.txt", "w", stdout);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d%d", °ree[i], &s[i]);
if (degree[i] == 1) {
q.push(node{ degree[i],i });
}
}
while (!q.empty()) {
node t = q.front();
q.pop();
if (vis[t.v] || !degree[t.v])continue;
vis[t.v] = true;
int u = s[t.v];
degree[u]--;
degree[t.v]--;
st.push(road{ t.v,u });
s[u] ^= t.v;
if (degree[u] == 1) {
q.push(node{ degree[u],u });
}
}
cout << st.size() << endl;
while (!st.empty()) {
road t = st.top();
st.pop();
printf("%d %d\n", t.from, t.to);
}
return 0;
}