QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#329758 | #2210. Hamilton Path | Crysfly | WA | 72ms | 40504kb | C++17 | 4.8kb | 2024-02-17 02:11:11 | 2024-02-17 02:11:12 |
Judging History
answer
// what is matter? never mind.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define int long long
#define ull unsigned long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 1000000007
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,modint b){return a.x==b.x;}
friend bool operator !=(modint a,modint b){return a.x!=b.x;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 1000005
#define inf 0x3f3f3f3f
mt19937_64 rnd(64);
modint pw[maxn];
int n,m;
vi e[maxn];
bool vis[maxn];
int id[maxn],q[maxn],idx;
bool wk(int u){
// cout<<"wk "<<u<<"\n";
For(i,1,n)id[i]=q[i]=0;
q[1]=u,id[u]=1,idx=1;
For(i,2,n){
int t=0;
for(int v:e[u])
if(!id[v]){
if(t!=0 && t!=v)return 0;
t=v;
}
if(t==0)return 0;
u=t;
// cout<<"u: "<<i<<" "<<u<<"\n";
q[i]=u,id[u]=i,idx=i;
}
// cout<<"q: ";For(i,1,n)cout<<q[i]<<" "; cout<<"\n";
return 1;
}
vector<pii> ans;
modint F(){
modint res=0;
For(i,1,n)res=res*10+q[i];
return res;
}
int mn,mn2;
void chain(){
ans.pb(mkp(q[1],F().x));
if(mn==n) return;
int up=0;
For(i,mn+1,mn2){
for(int v:e[q[i]])
if(id[v]<mn){
if(up==0||up==i)up=i;
else up=-1;
}
}
// cout<<"mn,mn2,up "<<mn<<" "<<mn2<<" "<<up<<"\n";
int qn=q[mn+1];
if(up==0){
if(wk(qn)) ans.pb(mkp(q[1],F().x));
}
else if(up!=-1 && up!=0){
// cout<<"up "<<up<<"\n";
// cerr<<"q2: "<<q[up]<<"\n";
int t=q[up+1];
if(wk(t)) ans.pb(mkp(q[1],F().x));
}
}
modint w[maxn];
int c[maxn];
void circ(){
For(i,0,n+1)c[i]=0;
w[1]=F();
For(i,2,n){
w[i]=w[i-1]-pw[n-1]*q[i-1];
w[i]*=10,w[i]+=q[i-1];
}
For(u,1,n){
for(int v:e[u])
if(id[v]<id[u]) {
if(id[v]==1 && id[u]==n) continue;
c[id[v]+1]++,c[id[u]+1]--;
}
}
modint res=0;
For(i,1,n){
c[i]+=c[i-1];
if(!c[i]) ans.pb(mkp(q[i],w[i].x));
}
}
void work(int O)
{
n=read(),m=read();ans.clear();
// if(O==14){
// cout<<n<<" "<<m<<"\n";
// For(i,1,m){
// int u=read(),v=read();
// cout<<u<<" "<<v<<"\n";
// }exit(0);
// }
For(i,1,n)e[i].clear();
int tim=1;
while((1<<tim)<n)tim*=2;
tim*=5;
For(i,1,m){
int u=read(),v=read();
e[u].pb(v);
}
bool ok=0;
vi o;
For(i,1,n) o.pb(i);
while(o.size() && (tim--)){
int pos=o[rnd()%o.size()];
if(wk(pos)){
ok=1;
// cout<<"ok "<<pos<<"\n";
break;
}
vi o2;
for(int x:o) if(!id[x]) o2.pb(x);
o=o2;
}
if(!ok){
cout<<0<<"\n";
return;
}
mn=n,mn2=n;
for(int v:e[q[n]]){
if(id[v]<mn)mn2=mn,mn=id[v];
else if(id[v]<mn2)mn2=id[v];
}
if(mn==1 && mn!=n)circ();
else chain();
sort(ans.begin(),ans.end());
cout<<ans.size()<<"\n";
for(auto [x,y]:ans)cout<<y<<' ';cout<<"\n";
}
signed main()
{
pw[0]=1;
For(i,1,1000000)pw[i]=pw[i-1]*10;
int T=read();
For(_,1,T)work(_);
return 0;
}
/*
1
5 12
3 5
2 3
4 5
2 5
5 3
1 4
3 2
2 4
1 4
4 1
2 5
4 5
5 3 2 4 1
1 4 5 3 2
1 3 4 2 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 40460kb
input:
1 5 6 3 4 2 5 5 3 1 3 4 2 5 1
output:
2 13425 34251
result:
ok 3 number(s): "2 13425 34251"
Test #2:
score: -100
Wrong Answer
time: 72ms
memory: 40504kb
input:
67777 9 32 6 3 5 2 7 3 7 8 5 2 5 2 7 8 8 2 7 3 8 9 4 3 2 3 4 3 3 1 1 3 8 3 9 8 3 2 5 6 4 5 9 4 6 7 2 8 5 4 5 3 7 8 5 1 6 9 8 3 6 9 7 8 4 1 5 12 3 5 2 3 4 5 2 5 5 3 1 4 3 2 2 4 1 4 4 1 2 5 4 5 2 10 1 2 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 2 1 2 10 28 1 9 5 9 6 1 10 5 8 7 1 4 7 10 7 5 6 8 9 4 2 9 6 4 2 6 1 1...
output:
1 132894567 2 14532 53241 2 12 21 1 594731198 1 74123568 2 12 21 1 6241735 1 1 1 513264 1 7241356 2 213 312 1 247381965 1 123 1 31524 2 12 21 1 1 1 12 2 132 231 1 41268753 1 245378846 1 526134 1 413652 1 12345 1 31542 2 12 21 1 5624713 1 4675213 3 202947333 852029431 52029...
result:
wrong answer 4786th numbers differ - expected: '2', found: '1'