CodeForces-1343F Restore the Permutation


传送门:CodeForces-1343F

思路分析

枚举以每一个数字为开头的可能情况
确定首数字之后,第二个也就能确定了,这样就逐渐确定了所有的
最后$n^2$枚举所有可能集合,判断时候合法

样例输入

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

样例输出

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

AC代码

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;

const int INF = 0x3f3f3f3f;

int n;
vector<set<int>>s,tmp;

bool judge(vector<int>x) {
    set<set<int> > all;
    for(auto x:s) all.insert(x);
    for(int r=1; r<n; r++) {
        set<int>now;
        for(int l=r; l>=0; l--) {
            now.insert(x[l]);
            if(all.count(now)) {
                all.erase(now);
                break;
            }
        }
    }
    if(!all.empty()) return false;
    else return true;
}

int main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int t;
    read(t);
    while(t--) {
        read(n);
        s.clear();
        for(int i=1; i<=n-1; i++) {
            set<int>w;
            int m;
            read(m);
            while(m--) {
                int x;
                read(x);
                w.insert(x);
            }
            s.push_back(w);
        }
        vector<int>ans;
        for(int i=1; i<=n; i++) {
            int now=i;
            int ok=1;
            tmp=s;
            while(ans.size()<n) {
                int cnt=0;
                ans.push_back(now);
                for(int j=0; j<n-1; j++) if(tmp[j].count(now)) tmp[j].erase(now);
                for(int j=0; j<n-1; j++) if(tmp[j].size()==1) cnt++;
                if(cnt!=1) {
                    ok=0;
                    break;
                }
                for(int j=0; j<n-1; j++) if(tmp[j].size()==1) now=*tmp[j].begin();
            }
            if(ans.size()==n && judge(ans)) break;
            if(!ok) ans.clear();
        }
        for(auto x:ans) cout<<x<<" ";
        cout<<endl;
    }




    return 0;
}


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}