2020ICPC·小米 網絡選拔賽第一場 G-Tree Projection (構造)

2020ICPC·小米 網絡選拔賽第一場 G-Tree Projection (構造)

題面:

題意:

給定一個整數\(\mathit n\) 以及兩個\(1\dots n\) 的全排列\(A,B\)

請構造一個\(\mathit n\)個節點的無根樹,使其以\(A_1\) 爲根時,全排列\(\mathit A\) 是其一個合法的拓撲序。

使其以\(B_1\) 爲根時,全排列\(\mathit B\) 是其一個合法的dfs序。

輸出:

輔助數組:\(pos_i\) 代表第\(\mathit i\)個數在排列\(\mathit A\) 中的位置。

枚舉\(i\in[2,n]\)

開一個輔助變量\(now\) 代表 \(B_1,\dots ,B_{i-1}\)中拓撲序較小(在排列\(\mathit A\) 中的位置更靠前)的數。

連邊\((B[i],now)\),然後如果\(pos_{B_i}<pos_{now}\) 就更新now。

這樣生成的樹就滿足條件。

證明:

代碼:

int n;
int a[maxn];
int b[maxn];
int posa[maxn];
int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    n = readint();
    repd(i, 1, n) {
        a[i] = readint();
        posa[a[i]] = i;
    }
    repd(i, 1, n) {
        b[i] = readint();
    }
    int now = b[1];
    printf("YES\n");
    repd(i, 2, n) {
        printf("%d %d\n", now, b[i] );
        if (posa[b[i]] < posa[now]) {
            now = b[i];
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章