cf1682 D. Circular Spanning Tree

题意:

\(1\sim n\) 号节点按编号顺序顺时针放在一个圆周上,给定每个节点的度的奇偶性,要求构造一棵树,树中的边在圆内部不相交(但可以在圆周上相交于端点)

思路:

思路来源:dls

如果奇度点的数量是奇数,或者全是偶度点,则无解

否则可以这样构造:(妙啊!!)

int n; string s;

int f(int x) {
    if(x == 0) x = n;
    if(x == n + 1) x = 1;
    return x;
}

void sol() {
    cin >> n >> s; s = " " + s;

    int sum = 0; for(char c : s) sum += c == '1';
    if(sum & 1 || !sum) return cout << "NO" << endl, void();

    cout << "YES" << endl;
    int rt = 1; while(s[f(rt-1)] != '1') rt++; //根的左边必须是'1'
    for(int i = 1; i <= n; i++) if(i != rt)
        cout << i << ' ', //i肯定是一个端点
        //若i左边的点是'1',则i与根相连;否则i与i左边的点相连
        cout << (s[f(i-1)] == '1' ? rt : f(i-1)) << endl;
}
posted @ 2022-05-23 20:46  Bellala  阅读(139)  评论(2)    收藏  举报