QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#241318#7686. The Phantom Menaceucup-team1134#WA 597ms3668kbC++177.5kb2023-11-06 02:14:082023-11-06 02:14:08

Judging History

你现在查看的是最新测评结果

  • [2024-10-08 14:11:03]
  • hack成功,自动添加数据
  • (/hack/941)
  • [2024-10-08 10:05:28]
  • hack成功,自动添加数据
  • (/hack/940)
  • [2024-10-07 19:51:15]
  • hack成功,自动添加数据
  • (/hack/938)
  • [2024-10-07 19:28:01]
  • hack成功,自动添加数据
  • (/hack/937)
  • [2024-10-07 17:16:32]
  • hack成功,自动添加数据
  • (/hack/936)
  • [2024-10-07 16:53:09]
  • hack成功,自动添加数据
  • (/hack/935)
  • [2024-10-07 16:22:17]
  • hack成功,自动添加数据
  • (/hack/934)
  • [2023-11-06 02:14:08]
  • 评测
  • 测评结果:WA
  • 用时:597ms
  • 内存:3668kb
  • [2023-11-06 02:14:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod1=1000000009,mod2=1000099999,MAX=2000005,INF=1<<30;

struct Rollinghash{
    string S;
    int n;
    int base1;
    int base2;
    vector<ll> h1,h2,ru1,ru2;
    
    void make(string &T,int ba1,int ba2){
        S=T;
        n=S.size();
        h1.assign(n+1,0);
        h2.assign(n+1,0);
        ru1.assign(n+1,0);
        ru2.assign(n+1,0);
        base1=ba1;
        base2=ba2;
        
        ru1[0]=1;
        ru2[0]=1;
        
        for(int i=1;i<=n;i++){
            h1[i]=h1[i-1]*base1+ll(S[i-1]-'a'+1);
            h1[i]%=mod1;
            
            h2[i]=h2[i-1]*base2+ll(S[i-1]-'a'+1);
            h2[i]%=mod2;
            
            ru1[i]=ru1[i-1]*base1%mod1;
            ru2[i]=ru2[i-1]*base2%mod2;
        }
    }
    
    pair<ll,ll> ha(int l,int r){
        pair<ll,ll> res;
        res.fi=(h1[r]-h1[l]*ru1[r-l]%mod1+mod1)%mod1;
        res.se=(h2[r]-h2[l]*ru2[r-l]%mod2+mod2)%mod2;
        
        return res;
    }//開区間
};

struct UF{
    int n;
    vector<int> par,size,edge;
    
    void init(int n_){
        n=n_;
        par.assign(n,-1);
        size.assign(n,1);
        edge.assign(n,0);
        
        for(int i=0;i<n;i++){
            par[i]=i;
        }
    }
    
    int root(int a){
        if(par[a]==a) return a;
        else return par[a]=root(par[a]);
    }
    
    void unite(int a,int b){
        edge[root(a)]++;
        if(root(a)!=root(b)){
            size[root(a)]+=size[root(b)];
            edge[root(a)]+=edge[root(b)];
            par[root(b)]=root(a);
        }
    }
    
    bool check(int a,int b){
        return root(a)==root(b);
    }
};

// https://kopricky.github.io/code/Graph/euler_trail.html

class EulerPath
{
public:
    int V;
    vector<vector<int> > G;
    vector<int> degree, ans;
    EulerPath(int node_size) : V(node_size), G(V), degree(V, 0){}
    map<pair<int,int>,vector<int>> MA;
    void add_edge(int u, int v,int t){
        G[u].push_back(v), degree[u]++, degree[v]--;
        MA[mp(u,v)].push_back(t);
    }
    int judge(){
        int s = -1; bool out = false, in = false;
        for(int i = 0; i < V; i++){
            if(degree[i] == 0) continue;
            if(degree[i] == 1){
                if(out) return -1;
                out = true, s = i;
            }else if(degree[i] == -1){
                if(in) return -1;
                in = true;
            }else return -1;
        }
        return max(s, 0);
    }
    vector<int> solve(){
        int cur = judge();
        if(V == 0 || cur < 0) return {};
        stack<int> cur_path;
        cur_path.push(cur);
        while(!cur_path.empty()){
            if(!G[cur].empty()){
                cur_path.push(cur);
                int nx = G[cur].back();
                G[cur].pop_back();
                cur = nx;
            }else{
                ans.push_back(cur);
                cur = cur_path.top(), cur_path.pop();
            }
        }
        reverse(ans.begin(), ans.end());
        vector<int> E;
        for(int i=0;i+1<si(ans);i++){
            E.push_back(MA[mp(ans[i],ans[i+1])].back());
            MA[mp(ans[i],ans[i+1])].pop_back();
        }
        return E;
    }
};

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    
    ll ha1=rng()%mod1;
    ll ha2=rng()%mod2;
    
    int Q;cin>>Q;
    while(Q--){
        int N,M;cin>>N>>M;
        vector<string> S(N),T(N);
        for(int i=0;i<N;i++) cin>>S[i];
        for(int i=0;i<N;i++) cin>>T[i];
        
        {
            vector<pair<string,int>> A(N),B(N);
            for(int i=0;i<N;i++) A[i]=mp(S[i],i);
            for(int i=0;i<N;i++) B[i]=mp(T[i],i);
            sort(all(A));
            sort(all(B));
            bool ch=true;
            for(int i=0;i<N;i++) ch&=(A[i].fi==B[i].fi);
            
            if(ch){
                for(int i=0;i<N;i++) cout<<A[i].se+1<<" ";
                cout<<endl;
                for(int i=0;i<N;i++) cout<<B[i].se+1<<" ";
                cout<<endl;
                continue;
            }
        }
        
        vector<Rollinghash> rol(2*N);
        for(int i=0;i<N;i++){
            rol[i].make(S[i],ha1,ha2);
        }
        for(int i=0;i<N;i++){
            rol[N+i].make(T[i],ha1,ha2);
        }
        
        vector<int> ans1,ans2;
        
        for(int al=1;al<M;al++){
            vector<pair<pair<ll,ll>,int>> use;
            for(int i=0;i<N;i++){
                use.push_back(mp(rol[i].ha(0,al),0));
                use.push_back(mp(rol[i].ha(al,M),1));
            }
            for(int i=0;i<N;i++){
                use.push_back(mp(rol[N+i].ha(0,M-al),1));
                use.push_back(mp(rol[N+i].ha(M-al,M),0));
            }
            
            sort(all(use));
            use.erase(unique(all(use)),use.end());
            
            int K=si(use);
            
            auto f=[&](pair<pair<ll,ll>,int> x){
                return (int)(lower_bound(all(use),x)-use.begin());
            };
            
            UF uf;uf.init(K);
            EulerPath EP(K);
            
            for(int i=0;i<N;i++){
                int s=f(mp(rol[i].ha(0,al),0)),t=f(mp(rol[i].ha(al,M),1));
                //G[s].push_back(mp(t,i));
                EP.add_edge(s,t,i);
                uf.unite(s,t);
            }
            for(int i=0;i<N;i++){
                int s=f(mp(rol[N+i].ha(0,M-al),1)),t=f(mp(rol[N+i].ha(M-al,M),0));
                //G[s].push_back(mp(t,N+i));
                EP.add_edge(s,t,N+i);
                uf.unite(s,t);
            }
            
            if(uf.size[uf.root(0)]!=K) continue;
            
            auto tr=EP.solve();
            
            if(si(tr)){
                for(int i=0;i<2*N;i++){
                    if(i%2==0) ans1.push_back(tr[i]);
                    else ans2.push_back(tr[i]);
                }
                break;
            }
            /*
            if(si(EP.solve()){
                for(int i=0;i<K;i++) cout<<use[i].se<<" ";
                cout<<endl;
                for(int a:EP.ans) cout<<a<<" ";
                cout<<endl;
            }
            */
            /*
            vector<int> tr;
            
            DFS(0,tr);
            
            for(int i=0;i<K;i++){
                G[i].clear();
                visited[2*i]=visited[2*i+1]=false;
            }
            
            if(si(tr)==N+N){
                for(int i=0;i<2*N;i++){
                    if(i%2==0) ans1.push_back(tr[i]);
                    else ans2.push_back(tr[i]);
                }
                
                break;
            }
             */
        }
        
        if(si(ans1)==0) cout<<-1<<"\n";
        else{
            if(ans1[0]>=N) swap(ans1,ans2);
            for(int a:ans1) cout<<a+1<<" ";
            cout<<endl;
            for(int a:ans2) cout<<a+1-N<<" ";
            cout<<endl;
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

input:

2
3 3
abc
ghi
def
bcd
efg
hia
1 3
abc
def

output:

1 3 2 
1 2 3 
-1

result:

ok 2 cases (2 test cases)

Test #2:

score: 0
Accepted
time: 597ms
memory: 3668kb

input:

1000000
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b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-1
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
-1
-1
-1
1 
1 
-1
-1
-1
1 
1 
-1
1 
1 
-1
...

result:

wrong answer not cyclic isomorphism (test case 4)