QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#383505#2210. Hamilton PathKevin5307WA 71ms22084kbC++233.0kb2024-04-09 15:00:462024-04-09 15:00:46

Judging History

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

  • [2024-04-09 15:00:46]
  • 评测
  • 测评结果:WA
  • 用时:71ms
  • 内存:22084kb
  • [2024-04-09 15:00:46]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
const ll mod=1e9+7;
const ll base=10;
ll ksm(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=ans*a%mod;
		b>>=1;
		a=a*a%mod;
	}
	return ans;
}
const ll inv10=ksm(10,mod-2);
int u[1001000],v[1001000];
vector<int> G[500500],rG[500500];
int nxt[500500],lst[500500];
int deg[500500],ind[500500];
int vis[500500];
ll pw[500500];
void dfs(int u)
{
	if(~nxt[u]) return ;
	if(deg[u]>1) return ;
	vis[u]=1;
	int v=-1;
	for(auto v:rG[u])
		deg[v]--;
	for(auto w:G[u])
		if(!vis[w])
			v=w;
	if(v==-1) return ;
	if(~lst[v]) return ;
	nxt[u]=v;
	lst[v]=u;
	dfs(v);
}
int delta[500500];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	pw[0]=1;
	for(int i=1;i<500500;i++)
		pw[i]=pw[i-1]*base%mod;
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=m;i++)
			cin>>u[i]>>v[i];
		for(int i=1;i<=n;i++)
		{
			G[i].clear();
			rG[i].clear();
		}
		for(int i=1;i<=m;i++)
		{
			deg[u[i]]++;
			G[u[i]].pb(v[i]);
			rG[v[i]].pb(u[i]);
		}
		memset(nxt,-1,sizeof(int)*(n+10));
		memset(lst,-1,sizeof(int)*(n+10));
		memset(vis,0,sizeof(int)*(n+10));
		memset(delta,0,sizeof(int)*(n+10));
		for(int i=1;i<=n;i++)
			if(deg[i]==1)
				dfs(i);
		int cnt=0;
		for(int i=1;i<=n;i++)
			if(nxt[i]==-1)
				cnt++;
		if(cnt>1)
			cout<<"0\n";
		else
		{
			vector<int> ord;
			int cur=1;
			for(int i=1;i<=n;i++)
				if(lst[i]==-1)
					cur=i;
			ord.pb(cur);
			while(sz(ord)<n)
				ord.pb(nxt[ord.back()]);
			lst[ord[0]]=ord.back();
			nxt[ord.back()]=ord[0];
			for(int i=0;i<n;i++)
				ind[ord[i]]=i;
			for(int i=1;i<=n;i++)
				for(auto j:G[i])
					if(j!=nxt[i])
					{
						int a=ind[i],b=ind[j];
						if(a<b)
						{
							delta[0]++;
							delta[n-b]--;
							delta[n-a]++;
							delta[n]--;
						}
						else
						{
							delta[n-a]++;
							delta[n-b]--;
						}
					}
			for(int i=1;i<=n;i++)
				delta[i]+=delta[i-1];
			int ans=0;
			for(int i=0;i<n;i++)
				if(!delta[i])
					ans++;
			cout<<ans<<'\n';
			ll hsh=0;
			for(int i=0;i<n;i++)
				hsh=(hsh*base+ord[i])%mod;
			vector<pair<int,ll>> vec;
			for(int i=0;i<n;i++)
			{
				if(!delta[i])
					vec.pb(ord[(n-i)%n],hsh);
				hsh=((hsh-ord[n-i-1]+mod)*inv10%mod+ord[n-i-1]*pw[n-1])%mod;
			}
			srt(vec);
			for(auto pr:vec)
				cout<<pr.second<<" ";
			cout<<'\n';
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 20084kb

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: 71ms
memory: 22084kb

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 
1
53241 
2
12 21 
0
0
0
0
1
1 
0
0
0
0
0
0
0
1
1 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1 
0
0
0
1
1 
0
0
0
0
0
0
0
0
0
1
1 
0
0
0
0
0
1
1 
0
0
0
0
0
0
0
0
0
0
0
1
1 
0
0
0
0
1
1 
0
0
0
0
0
0
1
1 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1 
1
1 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 3rd numbers differ - expected: '2', found: '1'