QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#329785#2210. Hamilton PathCrysflyWA 68ms42956kbC++175.3kb2024-02-17 02:38:052024-02-17 02:38:05

Judging History

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

  • [2024-02-17 02:38:05]
  • 评测
  • 测评结果:WA
  • 用时:68ms
  • 内存:42956kb
  • [2024-02-17 02:38:05]
  • 提交

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,ups;
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,ups=1;
	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]=ups=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));
	}
}

int tr[maxn];
void add(int x,int y){for(;x<=n;x+=x&-x)tr[x]+=y;}
int kth(int k){
	int u=0,sum=0;
	Rep(i,18,0)
		if(u+(1<<i)<=n && sum+tr[u+(1<<i)]<k)
			u+=(1<<i),sum+=tr[u];
	return u+1;
}

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(),tr[i]=vis[i]=1;
	For(i,1,n) if(i+(i&-i)<=n) tr[i+(i&-i)]+=tr[i];
	
	int tim=1;
	while((1<<tim)<n)tim++;
	tim*=4;
//	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;
	
	int rst=n;
	while(rst && sum<=m*tim){
		int pos=rnd()%rst;
		pos=kth(pos);
		if(wk(pos)){
			ok=1;
		//	cout<<"ok "<<pos<<"\n";
			break;
		}
		sum+=idx;
		For(i,1,ups) {
			int u=q[i];
			if(vis[u]) vis[u]=0,--rst,add(u,-1);
		}
	}
//	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
*/

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 42956kb

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: 68ms
memory: 42472kb

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 
0
1
74123568 
2
12 21 
0
1
1 
1
513264 
0
2
213 312 
1
247381965 
1
123 
0
2
12 21 
1
1 
1
12 
2
132 231 
1
41268753 
0
1
526134 
1
413652 
1
12345 
1
31542 
2
12 21 
1
5624713 
1
4675213 
3
202947333 852029431 520294680 
0
1
427915798 
2
213 321 
1
5376412 
1
13...

result:

wrong answer 9th numbers differ - expected: '1', found: '0'