QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#129883 | #6507. Recover the String | Sorting | Compile Error | / | / | C++ | 5.0kb | 2023-07-23 02:33:52 | 2023-07-23 02:33:53 |
Judging History
你现在查看的是最新测评结果
- [2023-08-10 23:21:45]
- System Update: QOJ starts to keep a history of the judgings of all the submissions.
- [2023-07-23 02:33:53]
- 评测
- 测评结果:Compile Error
- 用时:0ms
- 内存:0kb
- [2023-07-23 02:33:52]
- 提交
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
answer.code: In function ‘std::vector<int>& winter(std::vector<int>, int)’: answer.code:113:37: error: cannot bind non-const lvalue reference of type ‘std::vector<int>&’ to an rvalue of type ‘std::vector<int>’ 113 | if(g.size()>p) return karina(g,p); | ~~~~~~^~~~~ answer.code:172:24: warning: reference to local variable ‘fn’ returned [-Wreturn-local-addr] 172 | return fn; | ^~ answer.code:168:28: note: declared here 168 | vector<int>fn; | ^~ answer.code:199:16: warning: reference to local variable ‘v3’ returned [-Wreturn-local-addr] 199 | return v3; | ^~ answer.code:182:20: note: declared here 182 | vector<int>v3; | ^~