QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#287468#5407. 基础图论练习题JJ0 38ms18976kbC++204.9kb2023-12-20 16:52:012023-12-20 16:52:02

Judging History

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

  • [2023-12-20 16:52:02]
  • 评测
  • 测评结果:0
  • 用时:38ms
  • 内存:18976kb
  • [2023-12-20 16:52:01]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int c[5005][5005],du[5005];
bool cmp(int x,int y){return du[x]<du[y];}
vector<int>fr[5005],to[5005];
void add(int aa,int bb)
{
	//cout<<aa<<"->"<<bb<<endl;
	fr[bb].push_back(aa);
	to[aa].push_back(bb);
	du[aa]++;
}
void init(int n)
{
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++) c[i][j]=0;
	for(int i=1;i<=n;i++) du[i]=0,fr[i].clear(),to[i].clear();
}
int vis[5005];
vector<int>solve(vector<int> vec)
{
	if(vec.size()==1) return vec;
	//for(int i=0;i<vec.size();i++)
	//cout<<vec[i]<<" ";puts("");
	vector<int>prf;int x=vec.back();vec.pop_back();
	for(int i=0;i<vec.size();i++) vis[vec[i]]=1;
	for(int i=0;i<fr[x].size();i++)
	if(vis[fr[x][i]]) du[fr[x][i]]--;
	for(int i=0;i<vec.size();i++) vis[vec[i]]=0;
	//删掉一个点在其它点的导出子图求强连通分量 
	sort(vec.begin(),vec.end(),cmp);
	long long sum=0;int las=-1;
	for(int i=0;i<vec.size();i++)
	{
		int u=vec[i];sum+=du[u]-las-1;
		//cout<<u<<":"<<du[u]<<endl;
		if(sum==(i-las)*(i-las-1)/2)
		{
			//cout<<u<<endl;
			vector<int>now,tmp;
			for(int j=las+1;j<=i;j++)
			du[vec[j]]-=las+1,now.push_back(vec[j]);
			tmp=solve(now);las=i;sum=0;
			for(int j=tmp.size()-1;j>=0;j--)
			prf.push_back(tmp[j]);
			//cout<<u<<"is fine"<<endl;
		}
	}
	for(int i=0;i<prf.size()/2;i++) swap(prf[i],prf[prf.size()-1-i]);
	//for(int i=0;i<prf.size();i++) cout<<prf[i]<<" ";puts("");
	prf.push_back(x);
	return prf;
}
char s[5005];
int md[3000005],b[5005],M=3000000,ans,tot;
int tv[10005],val[10005],v0[10005],pos[5005],si[5005];
int gval(int i,int j){if(i<j)swap(i,j);return md[(i-2)*(i-1)/2+j-1];}
void getans(vector<int>vec,int ps)
{
	vector<int>prf=solve(vec);int m=vec.size();
	//for(int i=0;i<m;i++) cout<<prf[i]<<" ";puts("");
	//吾有三贡献:哈密顿路径上、哈密顿路径外、到出度更小的连通块 
	for(int i=0;i<m;i++) pos[prf[i]]=i;
	for(int i=0;i<m*2;i++) val[i]=0;
	for(int i=0;i<m;i++)
	{
		int u=prf[i];
		for(int j=0;j<to[u].size();j++)
		{
			int v=pos[to[u][j]];
			if(b[v]!=ps) continue;
			if(v==(i+1)%m) continue;
			//cout<<v<<"~"<<i<<endl;
			if(v<i) val[v]++,val[i]--;
		}
	}
	if(m!=1)
	{
		tv[0]=val[0];v0[0]=(tv[0]==0);
		for(int i=1;i<m;i++) tv[i]=tv[i-1]+val[i];
		for(int i=1;i<m;i++) v0[i]=v0[i-1]+(tv[i]==0);
		ans=(ans+1ll*gval(prf[m-1],prf[0])*(tot+v0[m-1])%mod)%mod;
		//for(int i=0;i<m;i++) cout<<tv[i]<<" ";puts(""); 
		//cout<<prf[m-1]<<" "<<prf[0]<<":"<<tot+v0[m-2]<<endl;
	}
	for(int i=0;i<m-1;i++)
	{
		int u=prf[i];
		for(int j=0;j<to[u].size();j++)
		{
			int v=pos[to[u][j]];
			if(b[v]!=ps) continue;
			if(v==(i+1)%m) continue;
			if(v>i) val[v]++,val[i+m]--;
			else val[v+m]++,val[i+m]--;
		}
		for(int j=0;j<fr[u].size();j++)
		{
			int v=pos[fr[u][j]];
			if(b[v]!=ps) continue;
			if(i==(v+1)%m) continue;
			if(v>i) val[i]--,val[v]++;
			else val[i]--,val[v+m]++;
		}//这里理论上从i位开始前缀和就是对的 
		tv[0]=val[0];v0[0]=(tv[0]==0);
		for(int j=1;j<=m+i;j++) tv[j]=tv[j-1]+val[j];
		for(int j=1;j<=m+i;j++) v0[j]=v0[j-1]+(tv[j]==0);
		//for(int j=0;j<=m+i;j++) cout<<tv[j]<<" ";puts("");
		ans=(ans+1ll*gval(prf[i],prf[i+1])*(tot+v0[m+i-1]-v0[i])%mod)%mod;
		//cout<<prf[i]<<" "<<prf[i+1]<<":"<<tot+v0[m+i-1]-v0[i]<<endl;
	}
	for(int ii=0;ii<m;ii++)
	{
		int i=vec[ii];//cout<<i<<endl;
		for(int j=0;j<to[i].size();j++)
		{
			int va=gval(to[i][j],i);
			//cout<<to[i][j]<<" "<<i<<":"<<va<<endl;
			if(b[to[i][j]]==ps){
				if(pos[to[i][j]]!=(pos[i]+1)%m)
				ans=(ans+1ll*va*tot%mod)%mod;
			}
			else
			{
				if(b[to[i][j]]==ps-1&&si[ps]==1&&si[ps-1]==1)
				ans=(ans+1ll*va*tot%mod)%mod;
				//仍然是链,形态会变成 1->v->u->v+1->n 
				else ans=(ans+1ll*va*(tot-(ps-b[to[i][j]]))%mod)%mod;
			}
		}
	}
}
int main()
{
	int tim;cin>>tim;md[0]=1;
	for(int i=1;i<=M;i++) md[i]=2ll*md[i-1]%mod;
	while(tim--)
	{
		int n;cin>>n;ans=tot=0;init(n);
		for(int i=2;i<=n;i++)
		{
			scanf("%s",s+1);
			for(int j=1;j<=(i+3)/4;j++)
			{
				int val=(s[j]<='9'?s[j]-'0':s[j]-'A'+10);
				for(int k=1;k<=4;k++)
				c[i][(j-1)*4+k]=(val>>k-1)&1;
			}
			for(int j=1;j<i;j++)
			if(c[i][j]) add(i,j);
			else add(j,i);
		}
		vector<int>vec;
		for(int i=1;i<=n;i++) vec.push_back(i);
		sort(vec.begin(),vec.end(),cmp);
		long long sum=0;int las=-1;
		for(int i=0;i<vec.size();i++)
		{
			int uu=vec[i];sum+=du[uu]-las-1;
			//cout<<u<<":"<<du[u]<<endl;
			if(sum==(i-las)*(i-las-1)/2)
			{
				si[++tot]=0;
				for(int j=las+1;j<=i;j++)
				b[vec[j]]=tot,si[tot]++;
				las=i;sum=0;
			}
		}
		sum=0;las=-1;int tmp=0;
		for(int i=0;i<vec.size();i++)
		{
			int uu=vec[i];sum+=du[uu]-las-1;
			//cout<<u<<":"<<du[u]<<endl;
			if(sum==(i-las)*(i-las-1)/2)
			{
				vector<int>now;
				for(int j=las+1;j<=i;j++)
				du[vec[j]]-=las+1,now.push_back(vec[j]);
				getans(now,++tmp);las=i;sum=0;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 38ms
memory: 18976kb

input:

10000
100
1
2
2
8
C0
F0
27
78
AE1
C01
511
D87
EF20
3873
2742
73D0
DC9B0
FB2A3
9C011
9B4E0
95DC00
A7B980
F43531
6A6245
5347BE0
1A6C8A1
88E46D6
64CF3AE
D25F63C1
C894E4C3
1C0AFD73
EC1C3F9A
087CE17C0
22149A380
B28038AF1
B9CA21C7F
D78F5307C1
49045489A2
72C4DE6FD1
7713F40D05
EEE8878EEC1
310E62812B1
DA9D5B...

output:

44015257
13
352453823
428637146
182
0
1983
2
302319119
273158175
891998853
49489
2
801640988
115
3219583
2527
0
471087262
560413560
2902272
40095
39489
3769610
947076164
780131398
220081587
721573443
374182802
65360
269750275
227868047
53520
2162943
119367680
405634000
162
168
235
5245951
268582975
...

result:

wrong answer 1st numbers differ - expected: '281603732', found: '44015257'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%