• CodeForces 780 E Underground Lab


    Underground Lab

    题解:

    如果遍历一棵树,我们可以发现最多需要走的步数也不会超过2 * n步。

    所以我们选出一棵树,然后遍历一边这颗树。

    然后把序列分成k块就好了。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
    #define LL long long
    #define ULL unsigned LL
    #define fi first
    #define se second
    #define pb push_back
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define lch(x) tr[x].son[0]
    #define rch(x) tr[x].son[1]
    #define max3(a,b,c) max(a,max(b,c))
    #define min3(a,b,c) min(a,min(b,c))
    typedef pair<int,int> pll;
    const int inf = 0x3f3f3f3f;
    const int _inf = 0xc0c0c0c0;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const LL _INF = 0xc0c0c0c0c0c0c0c0;
    const LL mod =  (int)1e9+7;
    const int N = 2e5 + 100;
    int pre[N];
    int Find(int x){
        if(pre[x] == x) return x;
        return pre[x] = Find(pre[x]);
    }
    vector<int> ans;
    vector<int> c[N];
    vector<int> vc[N];
    void dfs(int o, int u){
        ans.pb(u);
        for(int v : vc[u]){
            if(v == o) continue;
            dfs(u, v);
            ans.pb(u);
        }
    }
    int Ac(){
        int n, m, k, u, v;
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i <= n; ++i) pre[i] = i;
        for(int i = 1; i <= m; ++i){
            scanf("%d%d", &u, &v);
            int fu = Find(u), fv = Find(v);
    //        cout << fu <<  " with " << fv << endl;
            if(fu == fv) continue;
            pre[fu] = fv;
            vc[u].pb(v); vc[v].pb(u);
        }
        dfs(0, 1);
    //    cout << "____" << endl;
        int can = (2*n  + k-1)/k;
        for(int i = 0; i < ans.size(); ++i){
            c[i/can].pb(ans[i]);
        }
        for(int i = 0; i < k; ++i){
            if(c[i].size() == 0) c[i].pb(1);
            printf("%d", c[i].size());
            for(int v : c[i]){
                printf(" %d", v);
            }
            puts("");
        }
        return 0;
    }
    
    int main(){
        Ac();
        return 0;
    }
    View Code
  • 相关阅读:
    编译原理:算符优先分析
    编译原理:自下而上语法分析
    编译原理:实验二 递归下降语法分析
    编译原理:LL(1)文法的判断,递归下降分析程序
    作业9 DFA最小化
    作业8 非确定的自动机NFA确定化为DFA
    作业7 正规式到正规文法与自动机
    作业6 正规文法与正规式
    作业5 词法分析程序的设计与实现
    作业4 文法和语言总结与梳理
  • 原文地址:https://www.cnblogs.com/MingSD/p/10857353.html
Copyright © 2020-2023  润新知