1 /* 2 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 3 图论/位运算:其实这题很简单。类似拓扑排序,先把度数为1的先入对,每一次少一个度数 4 关键在于更新异或和,精髓:a ^ b = c -> a ^ c = b, b ^ c = a; 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <algorithm> 10 #include <queue> 11 using namespace std; 12 13 const int MAXN = 7e4 + 10; 14 const int INF = 0x3f3f3f3f; 15 int ans[MAXN][2]; 16 int d[MAXN], s[MAXN]; 17 18 int main(void) //Codeforces Round #285 (Div. 2) C. Misha and Forest 19 { 20 // freopen ("C.in", "r", stdin); 21 22 int n; 23 while (scanf ("%d", &n) == 1) 24 { 25 queue<int> Q; 26 for (int i=0; i<n; ++i) 27 { 28 scanf ("%d%d", &d[i], &s[i]); 29 if (d[i] == 1) Q.push (i); 30 } 31 32 int cnt = 0; 33 while (!Q.empty ()) 34 { 35 int u = Q.front (); Q.pop (); 36 if (d[u] == 0) continue; 37 int v = s[u]; 38 ans[++cnt][0] = u; ans[cnt][1] = v; 39 if ((--d[v]) == 1) Q.push (v); 40 s[v] = s[v] ^ u; 41 } 42 43 printf ("%d\n", cnt); 44 for (int i=1; i<=cnt; ++i) 45 { 46 printf ("%d %d\n", ans[i][0], ans[i][1]); 47 } 48 } 49 50 return 0; 51 }
时间: 2024-10-11 05:11:17