QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#129860 | #6507. Recover the String | Sorting# | RE | 7ms | 42820kb | C++ | 5.0kb | 2023-07-23 02:05:39 | 2023-07-23 02:05:51 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
const ll mod=998244353;
vector<int>in[N];
int n,m,k;
int out[N];
int pr[N];
ll w[N];
bool vis[N];
void dfs(int id){
vis[id]=true;
for(auto c:in[id]){
if(!vis[c]) dfs(c);
}
if(in[id].empty()) w[id]=id;
else{
w[id]=0;
for(int j=0; j<2 ;j++){
w[id]=(w[id]+w[in[id][j%in[id].size()]])%mod;
}
}
}
ll f[N],inf[N];
ll pw(ll x,ll y){
if(y==0) return 1;
if(y%2) return x*pw(x,y-1)%mod;
ll res=pw(x,y/2);
return res*res%mod;
}
ll C(ll x,ll y){
return f[x]*inf[y]%mod*inf[x-y]%mod;
}
int find(vector<int>&v,int c){
for(int i=0; i<v.size() ;i++){
if(c==v[i]) return i;
}
return -1;
}
vector<int>cyclic(vector<int>g,int p){
if(p==1) return g;
vector<int>head;
{
for(auto c:in[g[0]]){
bool ok=true;
int d=c;
for(int i=0; i<g.size() ;i++){
int p=find(in[g[i]],c);
if(p==-1) ok=false;
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
}
if(ok && d==c) head.push_back(c);
}
}
int c=head[0];
vector<int>ng;
ng.push_back(c);
for(int i=0; i<g.size() ;i++){
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
ng.push_back(c);
}
ng.pop_back();
return cyclic(ng,p-1);
}
vector<int>karina(vector<int>g,int p){//not guarenteed that g is distinct
/*cout << "karina " << p << endl;
for(auto c:g) cout << c << ' ';
cout << endl;*/
if(p==1) return g;
vector<int>head;
{
for(auto c:in[g[0]]){
bool ok=true;
int d=c;
for(int i=0; i<g.size() ;i++){
int p=find(in[g[i]],c);
if(p==-1) ok=false;
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
}
if(ok) head.push_back(d);
}
}
int c=head[0];
vector<int>ng;
ng.push_back(c);
for(int i=0; i<g.size() ;i++){
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
ng.push_back(c);
}
return karina(ng,p-1);
}
int mem[N];
ll jk[N];
vector<int>winter(vector<int>g,int p){//guarenteed that g is distinct
if(g.size()>p) return karina(g,p);
vector<int>head;
{
for(auto c:in[g[0]]){
bool ok=true;
int d=c;
for(int i=0; i<g.size() ;i++){
int p=find(in[g[i]],c);
if(p==-1) ok=false;
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
}
if(ok) head.push_back(d);
}
}
int c=head[0];
vector<int>ng;
ng.push_back(c);
for(int i=0; i<g.size() ;i++){
if(in[g[i]].size()==2){
c^=in[g[i]][0];
c^=in[g[i]][1];
}
ng.push_back(c);
}
for(auto c:ng) mem[c]=0;
int fl=-1,fr=-1;
for(int i=1; i<ng.size() ;i++){
if(ng[mem[ng[i]]]==ng[i]){
fl=mem[ng[i]];
fr=i;
break;
}
}
if(fl==-1){
return winter(ng,p-1);
}
vector<int>nk,nc;
for(int i=0; i<fl ;i++){
nk.push_back(ng[i]);
}
for(int i=fl; i<fr ;i++) nc.push_back(ng[i]);
for(int i=fr; i<ng.size() ;i++) nk.push_back(ng[i]);
/*cout << "WINTER" << endl;
cout << "NC: ";
for(auto c:nc) cout << c << ' ';
cout << endl;
cout << "NK: ";
for(auto c:nk) cout << c << ' ';
cout << endl;
*/
p--;
if(nc.size()==2 && nk.size()>2){
vector<int>fn;
auto v1=winter(nk,p);
for(int i=0; i<fl+1 ;i++) fn.push_back(v1[i]);
for(int i=fl; i<v1.size() ;i++) fn.push_back(v1[i]);
return fn;
}
auto v2=cyclic(nc,p);
int pr=v2.size();
for(int i=0; i<p ;i++){
v2.push_back(v2[i%pr]);
}
for(int i=0; i<v2.size() ;i++){
jk[fl+i]=v2[i];
}
vector<int>v3;
for(int i=fl-1; i>=0 ;i--){
ll cur=w[ng[i]];
for(int j=1; j<=p-1 ;j++){
cur=(cur+(mod-C(p-1,j))*jk[i+j])%mod;
}
}
for(int i=fr+1; i<ng.size() ;i++){
ll cur=w[ng[i]];
for(int j=0; j<p-1 ;j++){
cur=(cur+(mod-C(p-1,j))*jk[i+j])%mod;
}
jk[i+p-1]=cur;
}
for(int i=0; i<ng.size()+p-1 ;i++){
v3.push_back(jk[i]);
}
return v3;
}
int lb[N];
string greedy(vector<int>s){
for(int i=1; i<=n ;i++) lb[i]=0;
int ptr=0;
string res;
for(auto c:s){
//cout << "haha " << c << endl;
if(lb[c]==0){
lb[c]=++ptr;
}
res+='a'+lb[c]-1;
}
return res;
}
void solve(){
cin >> n >> m;
for(int i=1; i<=n ;i++){
in[i].clear();
out[i]=0;
vis[i]=false;
}
f[0]=1;
for(int i=1; i<=n ;i++) f[i]=f[i-1]*i%mod;
inf[n]=pw(f[n],mod-2);
for(int i=n; i>=1 ;i--) inf[i-1]=inf[i]*i%mod;
for(int i=1; i<=m ;i++){
int u,v;cin >> u >> v;
in[v].push_back(u);
out[u]++;
}
if(n==1){
cout << "a\n";
return;
}
int boss=0;
for(int i=1; i<=n ;i++){
if(out[i]==0){
boss=i;break;
}
}
dfs(boss);
{//compute length
int x=boss;
k=1;
while(!in[x].empty()){
k++;
x=in[x][0];
}
}
if(in[boss].size()==1){
for(int i=1; i<=n ;i++) cout << "a";
cout << '\n';
return;
}
auto v1=winter({in[boss][0],in[boss][1]},k-1);
string s1=greedy(v1);
auto v2=winter({in[boss][1],in[boss][0]},k-1);
string s2=greedy(v2);
cout << min(s1,s2) << '\n';
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
int t;cin >> t;while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 42820kb
input:
3 1 0 5 6 2 4 2 5 5 3 4 3 1 5 1 4 8 11 1 2 1 4 1 6 2 5 3 4 3 6 4 5 4 7 5 8 6 7 7 8
output:
a aba aaba
result:
ok 3 lines
Test #2:
score: -100
Runtime Error
input:
26837 1 0 2 1 2 1 3 2 1 3 2 3 3 2 3 1 1 2 5 5 4 3 5 1 1 2 3 2 5 3 5 6 2 4 2 5 5 3 4 3 1 5 1 4 5 5 1 5 2 1 2 4 4 5 3 4 6 6 1 4 5 3 2 4 4 6 3 6 2 3 4 3 1 3 3 4 2 1 7 8 2 5 1 3 7 1 3 5 7 6 1 2 4 6 6 3 8 11 2 6 2 7 3 7 8 1 6 4 4 5 7 4 7 1 3 8 2 8 1 5 8 10 8 1 4 5 7 8 3 5 3 1 7 3 1 2 5 2 6 4 6 3 9 11 5 2...