QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#329783 | #2210. Hamilton Path | Crysfly | WA | 74ms | 35036kb | C++17 | 5.0kb | 2024-02-17 02:28:57 | 2024-02-17 02:28:58 |
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=0;
For(i,2,n){
++idx;
int t=0;
for(int v:e[u]){
++idx;
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;
}
// 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++;
tim*=2;
// set<pii>S;
For(i,1,m){
int u=read(),v=read();
// if(S.count(mkp(u,v))) continue;
// S.insert(mkp(u,v));
e[u].pb(v);
}
bool ok=0;
ll sum=0;
vi o;
For(i,1,n) o.pb(i);
while(o.size() && sum<=m*tim){
int pos=o[rnd()%o.size()];
if(wk(pos)){
ok=1;
o.clear();
// cout<<"ok "<<pos<<"\n";
break;
}
sum+=idx;
vi o2;
for(int x:o) if(!id[x]) o2.pb(x);
o=o2;
}
// if(!ok && o.size()){
// for(int x:o) {
// if(wk(x)){
// ok=1;
// break;
// }
// }
// }
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]!=mn && 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: 12ms
memory: 34800kb
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: 0
Accepted
time: 69ms
memory: 35012kb
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:
ok 148279 numbers
Test #3:
score: 0
Accepted
time: 74ms
memory: 34856kb
input:
90961 2 4 2 1 2 1 2 1 2 1 4 8 4 3 2 1 4 3 1 2 4 1 1 4 1 2 4 2 5 22 1 3 5 2 3 1 3 4 3 4 2 3 2 4 3 2 5 4 5 3 4 3 2 1 1 4 1 2 1 3 2 1 4 5 4 3 1 5 5 3 2 3 4 5 8 22 1 4 3 8 8 6 5 7 5 6 1 3 1 2 8 5 8 3 5 2 6 5 2 5 6 1 6 7 5 4 2 4 4 1 6 3 4 3 8 1 6 5 7 6 10 7 10 8 9 3 10 5 4 6 10 7 10 3 3 1 8 28 7 8 8 5 6 ...
output:
1 21 1 2143 0 0 0 0 1 1 2 12 21 0 0 0 0 2 12 21 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 1 213 0 0 0 1 1 0 0 0 2 123 231 2 12 21 0 0 1 2413 0 0 0 1 1 1 1 2 12 21 0 0 0 0 1 213 0 0 1 132 0 2 12 21 0 0 1 1 1 1 0 0 0 0 1 21 0 2 12 21 1 34152 0 0 1 1 0 0 1 1 0 1 1 0 0 0 0 0 1 1 0 0 0 0 ...
result:
ok 124496 numbers
Test #4:
score: 0
Accepted
time: 69ms
memory: 34864kb
input:
64460 7 24 6 7 1 3 2 1 5 6 7 5 2 5 4 2 3 1 3 4 1 5 7 6 1 7 1 6 7 2 2 7 4 5 4 3 6 5 7 6 3 1 7 5 2 7 1 7 1 2 7 18 6 2 7 5 3 4 4 5 2 1 6 2 7 6 7 6 5 7 7 6 3 2 1 2 7 2 5 2 1 6 5 1 3 6 3 6 2 4 2 1 2 1 1 2 2 1 5 26 5 1 3 1 5 2 1 4 3 5 2 1 3 4 3 5 1 4 3 4 2 1 2 3 1 4 2 1 1 4 2 4 1 2 3 1 5 2 2 1 4 1 3 4 3 1...
output:
1 5672134 0 2 12 21 1 41235 1 15324 2 1764352 7643521 1 1234 1 374219658 1 264531 1 812937456 1 132 1 1 2 12 21 1 6174235 1 134800596 1 273591684 1 321 0 2 12 21 1 625143 1 1 1 213 1 872632014 1 612345 1 65384172 1 193248657 1 786034249 1 85462731 1 231 1 31542 1 57461839...
result:
ok 132329 numbers
Test #5:
score: -100
Wrong Answer
time: 72ms
memory: 35036kb
input:
67484 4 19 1 3 4 3 2 3 2 3 4 3 2 3 3 4 4 1 4 1 2 4 2 3 2 1 4 1 4 1 4 3 4 2 4 1 2 1 3 1 10 23 8 4 6 4 6 5 3 2 10 9 5 4 9 5 7 10 8 1 2 6 5 4 2 9 8 4 6 2 5 7 6 3 2 4 9 4 7 1 10 1 4 8 5 2 1 7 2 10 2 1 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 0 8 22 5 4 5 4 6 2 3 2 6 1 8 5 2 3 5 3 7 4 2 6 2 3 5 4 5 6 5 3 1 ...
output:
1 1342 1 818095235 2 12 21 1 1 1 32617485 1 3142 2 12 21 1 3214567 1 218350734 1 824796351 1 5173264 1 1 1 123 1 623918456 2 12 21 1 2613475 1 491303652 1 983561724 1 123 3 652314987 765231498 876523149 1 1 1 68451732 1 34125876 1 1 2 231 312 2 123 312 1 81457263 1 3214 1...
result:
wrong answer 111493rd numbers differ - expected: '1', found: '0'