1. 程式人生 > >【Codeforces 501C】Misha and Forest

【Codeforces 501C】Misha and Forest

ORC pre out using http clu problem for turn

【鏈接】 我是鏈接,點我呀:)
【題意】


給你一棵樹
但是每個節點只告訴你出度個數
以及所有和它相連的點的異或和.
讓你還原這棵樹

【題解】


葉子節點的話,他所有節點的異或和就是它那唯一的一個爸爸
因此,弄個拓撲排序,從最下層一直往上面進行拓撲排序,每次找到它的爸爸之後,就將這個兒子刪掉.讓爸爸的出邊遞減。
同時更新爸爸的異或和,直到爸爸沒有兒子為止(也變成葉子節點了)。
(如果某個時刻chu[x]==0,那麽x肯定是根節點了,說明找邊的工作結束了。)

【代碼】

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1<<16;
const long long M = 15e6;

int n;
int chu[N+10],sum[N+10];
int tot = 0;
queue<int> dl;

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    for (int i = 0;i < n;i++){
        cin >> chu[i] >> sum[i];
        tot+=chu[i];
        if (chu[i]==1){
            dl.push(i);
        }
    }
    cout<<tot/2<<endl;
    while (!dl.empty()){
        int x = dl.front();dl.pop();
        if (chu[x]==0) continue;
        chu[x] = 0;
        int y = sum[x];
        cout<<x<<" "<<y<<endl;
        sum[y]^=x;
        chu[y]--;
        if (chu[y]==1){
            dl.push(y);
        }
    }
    return 0;
}

【Codeforces 501C】Misha and Forest