QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#329725#2210. Hamilton PathCrysflyTL 6ms40768kbC++174.2kb2024-02-17 01:46:032024-02-17 01:46:04

Judging History

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

  • [2024-02-17 01:46:04]
  • 评测
  • 测评结果:TL
  • 用时:6ms
  • 内存:40768kb
  • [2024-02-17 01:46:03]
  • 提交

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(time(0));
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)return 0;
				t=v;
			}
		u=t;
//		cout<<"u: "<<i<<" "<<u<<"\n";
		q[i]=u,id[u]=i,idx=i;
	}
	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;
			}
	}
	if(up!=-1){
	//	cerr<<"q2: "<<q[up]<<"\n";
		int t=q[up];
		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]) c[id[v]+1]++,c[id[u]+1]--;
	}
	c[2]-=1,c[n+1]+=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()
{
	n=read(),m=read();
	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;
			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();
	while(T--)work();
	return 0;
}
/*
1 3 4 2 5
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 40768kb

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
Time Limit Exceeded

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:

2
0 132894567 
4
0 0 132894567 53241 
5
0 0 132894567 21 53241 
6
0 0 132894567 21 53241 594731198 
7
0 0 132894567 21 53241 74123568 594731198 
8
0 0 132894567 21 21 53241 74123568 594731198 
9
0 0 132894567 21 21 53241 6241735 74123568 594731198 
10
0 0 1 132894567 21 21 53241 6241735 74123568 594...

result: