zoukankan      html  css  js  c++  java
  • Restore the Permutation by Sorted Segments CodeForces

    We guessed a permutation pp consisting of nn integers. The permutation of length nn is the array of length nn where each element from 11 to nn appears exactly once. This permutation is a secret for you.

    For each position rr from 22 to nn we chose some other index ll (l<rl<r) and gave you the segment pl,pl+1,,prpl,pl+1,…,pr in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly n1n−1 segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order.

    For example, if the secret permutation is p=[3,1,4,6,2,5]p=[3,1,4,6,2,5] then the possible given set of segments can be:

    • [2,5,6][2,5,6]
    • [4,6][4,6]
    • [1,3,4][1,3,4]
    • [1,3][1,3]
    • [1,2,4,6][1,2,4,6]

    Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists).

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1t1001≤t≤100) — the number of test cases. Then tt test cases follow.

    The first line of the test case contains one integer nn (2n2002≤n≤200) — the length of the permutation.

    The next n1n−1 lines describe given segments.

    The ii-th line contains the description of the ii-th segment. The line starts with the integer kiki (2kin2≤ki≤n) — the length of the ii-th segment. Then kiki integers follow. All integers in a line are distinct, sorted in ascending order, between 11 and nn, inclusive.

    It is guaranteed that the required pp exists for each test case.

    It is also guaranteed that the sum of nn over all test cases does not exceed 200200 (n200∑n≤200).

    Output

    For each test case, print the answer: nn integers p1,p2,,pnp1,p2,…,pn (1pin1≤pi≤n, all pipi should be distinct) — any suitable permutation (i.e. any permutation corresponding to the test case input).

    Example

    Input
    5
    6
    3 2 5 6
    2 4 6
    3 1 3 4
    2 1 3
    4 1 2 4 6
    5
    2 2 3
    2 1 2
    2 1 4
    2 4 5
    7
    3 1 2 6
    4 1 3 5 6
    2 1 2
    3 4 5 7
    6 1 2 3 4 5 6
    3 1 3 6
    2
    2 1 2
    5
    2 2 5
    3 2 3 5
    4 2 3 4 5
    5 1 2 3 4 5
    
    Output
    3 1 4 6 2 5 
    3 2 1 4 5 
    2 1 6 3 5 4 7 
    1 2 
    2 5 3 4 1 

    tutorial:

    Let's fix the first element and then try to restore permutation using this information. One interesting fact: if such permutation exists then it can be restored uniquely. Let's remove the first element from all segments containing it (we can use some logarithmic data structure for it). Then we just have a smaller problem but with one important condition: there is a segment consisting of one element (again, if such permutation exists). So, if the number of segments of length 11 is zero or more than one by some reason then there is no answer for this first element. Otherwise, let's place this segment (a single element) in second place, remove it from all segments containing it and just solve a smaller problem again.

    If we succeed with restoring the permutation then we need to check if this permutation really satisfies the given input segments (see the first test case of the example to understand why this case appears). Let's just iterate over all ii from 22 to nn and then over all jj from i1i−1 to 11. If the segment aj,aj+1,,aiaj,aj+1,…,ai is in the list, remove it and go to the next ii. If we can't find the segment for some ii then this permutation is wrong.

    Time complexity: O(n3logn)O(n3log⁡n) (or less, maybe?)


    //#include <bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include<cstring>
    #include <algorithm>
    #include <queue>
    #include<map>
    #include<set>
    #include<vector>
    using namespace std;
    typedef long long ll;
    const ll inf = 1<<30;
    const int mod = 1000000007;
    const int mx = 1e4+1e2; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
    #define clr(a) memset(a, -1, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pai
    //void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    int n, m, k,ans,t;
    
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);    
        cin >> t;
        
        while (t--) {
            cin >> n;
            vector<set<int>>se;
            re(i, 0, n - 1) {
                set <int>cur;
                int k;
                cin >> k;
                re(j, 0, k) {
                    int x; cin >> x; cur.insert(x);
                }
                se.push_back(cur);
            }
            re(fst, 1, n + 1) {
                vector < int>ans;
                bool ok = 1;
                vector<set<int>>cur = se;
                for (auto& it : cur)if (it.count(fst))it.erase(fst);
                ans.push_back(fst);
                re(i, 1, n) {
                    int cnt1 = 0, nxt = -1;
                    for (auto& it : cur)if (it.size() == 1) {
                        ++cnt1; nxt = *it.begin();
                    }
                    if (cnt1 != 1) {
                        ok = 0; break;
                    }
                    for (auto& it : cur)if (it.count(nxt))it.erase(nxt);
                    ans.push_back(nxt);
                }
                if (ok) {
                    set<set<int>>all(se.begin(), se.end());
                    re(i, 1, n) {
                        set<int>se;
                        se.insert(ans[i]);
                        bool find = 0;
                        for (int j = i-1; j >=0; --j)
                        {
                            se.insert(ans[j]);
                            if (all.count(se)) {
                                find = 1;
                                all.erase(se);
                                break;
                            }
                        }
                        if (!find)ok = 0;
                    }
                }
                if (ok) {
                    for (auto it : ans)cout << it << " ";
                    cout << endl; break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    利用js读出xml制作地区联动下拉框
    用EXTJS做一个右下角的泡泡窗口(一)
    用Json.net 解析 QQ城市达人中的地址列表
    用httpwebrequest访问跨域网站时对CookieContainer的处理
    asp.net 中PDF模式下省略水晶报表自己的预览
    asp.net 取得URL信息
    利用WebClient 制作调试Http的post 和 get 工具
    超级网银就是一砣 SHI
    SAP R/3 Testing[2]
    [1.1]用WebService返回歌曲的曲目信息.借鉴[星集工作室 张麟 Dephi版]
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12762811.html
Copyright © 2011-2022 走看看