QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#86641#5407. 基础图论练习题Random_Code0 0ms0kbC++115.6kb2023-03-10 14:36:112023-03-10 14:36:29

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-10 14:36:29]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-03-10 14:36:11]
  • 提交

answer

#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define mod 1000000007
#define F first
#define S second
#define ll int
#define N 5010
using namespace std;
struct SCC{
	ll n,cnt,cnt2,bel[N],dfn[N],lw[N];
	bool vis[N],inq[N];
	vector<ll> vt[N];
	stack<ll> st;
	void init(ll _n)
	{
		ll i;
		n=_n;
		cnt=cnt2=0;
		for(i=0;i<=n*2;i++)
		{
			vt[i].clear();
			bel[i]=dfn[i]=lw[i]=vis[i]=inq[i]=0;
		}
		while(!st.empty())
		{
			st.pop();
		}
		return;
	}
	void addedge(ll x,ll y)
	{
		vt[x].push_back(y);
		return;
	}
	void dfs(ll x)
	{
		vis[x]=inq[x]=true;
		st.push(x);
		dfn[x]=lw[x]=++cnt;
		ll i;
		for(i=0;i<vt[x].size();i++)
		{
			if(!vis[vt[x][i]])
			{
				dfs(vt[x][i]);
				lw[x]=min(lw[x],lw[vt[x][i]]);
			}
			else if(inq[vt[x][i]])
			{
				lw[x]=min(lw[x],dfn[vt[x][i]]);
			}
		}
		if(lw[x]==dfn[x])
		{
			do
			{
				i=st.top();
				st.pop();
				inq[i]=false;
				bel[i]=cnt2;
			}while(i!=x);
			cnt2++;
		}
		return;
	}
	void solve()
	{
		ll i;
		for(i=0;i<n;i++)
		{
			if(!vis[i])
			{
				dfs(i);
			}
		}
		return;
	}
}gscc;
ll n,ans[N][N],bel[N],deg[N],sid[N],tot,nxt[N],toid[N],chid[N],rt,bannxt,bantp,dfn[N],lw[N],cnt,res;
bool hved[N][N],vis[N];
string s;
vector<ll> vt[N],nt[N],hvnd[N],curnd,chns;
vector<ll> dfschain(ll x)
{
	ll i,j;
	vis[x]=true;
	vector<ll> ret;
	for(i=0;i<vt[x].size();i++)
	{
		if(!vis[vt[x][i]])
		{
			vector<ll> nw=dfschain(vt[x][i]);
			for(j=0;j<nw.size();j++)
			{
				ret.push_back(nw[j]);
			}
		}
	}
	ret.push_back(x);
	return ret;
}
void dfs(ll x)
{
	vis[x]=true;
	dfn[x]=lw[x]=++cnt;
	ll i;
	if(nxt[x]!=-1&&bannxt!=x)
	{
		ll y=nxt[x];
		if(!vis[y])
		{
			dfs(y);
			lw[x]=min(lw[x],lw[y]);
		}
		lw[x]=min(lw[x],dfn[y]);
	}
	if(bantp==x||((!vt[x].empty())&&vt[x][0]==nxt[x]))
	{
		if(vt[x].size()>1)
		{
			ll y=vt[x][1];
			if(!vis[y])
			{
				dfs(y);
				lw[x]=min(lw[x],lw[y]);
			}
			lw[x]=min(lw[x],dfn[y]);
		}
	}
	else if(!vt[x].empty())
	{
		ll y=vt[x][0];
		if(!vis[y])
		{
			dfs(y);
			lw[x]=min(lw[x],lw[y]);
		}
		lw[x]=min(lw[x],dfn[y]);
	}
	if(toid[x]<vt[x].size()&&toid[x]>0)
	{
		ll y=vt[x][toid[x]];
		if(!vis[y])
		{
			dfs(y);
			lw[x]=min(lw[x],lw[y]);
		}
		lw[x]=min(lw[x],dfn[y]);
	}
	res+=(lw[x]==dfn[x]);
	return;
}
void update(ll x,ll y)
{
	ll i;
	for(i=0;i<curnd.size();i++)
	{
		vis[curnd[i]]=false;
	}
	bannxt=-1,bantp=x;
	cnt=res=0;
	dfs(rt);
	ans[min(x,y)][max(x,y)]+=res-1;
	return;
}
void updatenxt(ll x,ll y)
{
	ll i;
	for(i=0;i<curnd.size();i++)
	{
		vis[curnd[i]]=false;
	}
	bannxt=x,bantp=-1;
	cnt=res=0;
	dfs(rt);
	if(!vis[y])
	{
		cnt=0;
		dfs(y);
	}
	ans[min(x,y)][max(x,y)]+=res-1;
	return;
}
bool cmp(ll x,ll y)
{
	return chid[x]<chid[y];
}
void solve(ll id)
{
	ll i,j;
	rt=hvnd[id][0];
	curnd=hvnd[id];
	for(i=0;i<curnd.size();i++)
	{
		vis[curnd[i]]=false;
	}
	chns=dfschain(rt);
	reverse(chns.begin(),chns.end());
	for(i=0;i<chns.size();i++)
	{
		nxt[chns[i]]=(i==chns.size()-1?-1:chns[i+1]);
		chid[chns[i]]=i;
	}
	for(i=0;i<curnd.size();i++)
	{
		sort(vt[curnd[i]].begin(),vt[curnd[i]].end(),cmp);
		toid[curnd[i]]=0;
	}
	for(i=0;i<curnd.size();i++)
	{
		if(!vt[curnd[i]].empty())
		{
			update(curnd[i],vt[curnd[i]][0]);
		}
	}
	for(i=0;i+1<chns.size();i++)
	{
		for(j=0;j<curnd.size();j++)
		{
			while(toid[curnd[j]]<vt[curnd[j]].size()&&(chid[vt[curnd[j]][toid[curnd[j]]]]<=i||vt[curnd[j]][toid[curnd[j]]]==nxt[curnd[j]]))
			{
				toid[curnd[j]]++;
			}
		}
		updatenxt(chns[i],chns[i+1]);
	}
	return;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll i,j,k,T;
	cin>>T;
	while(T--)
	{
		cin>>n;
		for(i=0;i<n;i++)
		{
			vt[i].clear();
		}
		for(i=1;i<n;i++)
		{
			cin>>s;
			for(j=0;j<s.size();j++)
			{
				ll curnum=(s[j]>='0'&&s[j]<='9')?s[j]-'0':s[j]-'A'+10;
				for(k=0;k<4;k++)
				{
					hved[i][j*4+k]=(curnum>>k)&1;
				}
			}
		}
		gscc.init(n);
		for(i=0;i<n;i++)
		{
			for(j=0;j<i;j++)
			{
				if(hved[i][j])
				{
					vt[i].push_back(j);
					gscc.addedge(i,j);
				}
				else
				{
					vt[j].push_back(i);
					gscc.addedge(j,i);
				}
			}
		}
		gscc.solve();
		tot=gscc.cnt2;
		for(i=0;i<n;i++)
		{
			bel[i]=gscc.bel[i];
			hvnd[bel[i]].push_back(i);
			for(j=0;j<i;j++)
			{
				ans[j][i]=tot;
			}
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j<vt[i].size();j++)
			{
				if(bel[i]!=bel[vt[i][j]])
				{
					nt[bel[i]].push_back(bel[vt[i][j]]);
					deg[bel[vt[i][j]]]++;
				}
			}
		}
		queue<ll> q;
		for(i=0;i<tot;i++)
		{
			if(deg[i]==0)
			{
				q.push(i);
				sid[i]=0;
			}
		}
		while(!q.empty())
		{
			ll x=q.front();
			q.pop();
			for(i=0;i<nt[x].size();i++)
			{
				deg[nt[x][i]]--;
				if(deg[nt[x][i]]==0)
				{
					sid[nt[x][i]]=sid[x]+1;
					q.push(nt[x][i]);
				}
			}
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j<i;j++)
			{
				if(bel[i]!=bel[j])
				{
					ans[j][i]-=abs(sid[bel[i]]-sid[bel[j]]);
					if(abs(sid[bel[i]]-sid[bel[j]])==1&&hvnd[bel[i]].size()==1&&hvnd[bel[j]].size()==1)
					{
						ans[j][i]++;
					}
				}
			}
		}
		for(i=0;i<n;i++)
		{
			vector<ll> nw;
			for(j=0;j<vt[i].size();j++)
			{
				if(bel[i]==bel[vt[i][j]])
				{
					nw.push_back(vt[i][j]);
				}
			}
			vt[i]=nw;
		}
		for(i=0;i<tot;i++)
		{
			solve(i);
		}
		ll curpw=1;
		long long res=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<i;j++)
			{
				res=(res+(long long)ans[j][i]*(long long)curpw)%mod;
				curpw=(curpw+curpw)%mod;
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

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:

281603732
11
285212672
371842543
90
0
2514
2
268503039
268435455
719477795
32801
-1
719476771
227
2621567
2624
0
719476275
371842543
2097159
41087
34815
3114626
719478307
502774846
719476427
719476259
719476259
67592
268697653
719497763
36863
2744447
719476259
371842543
816
171
115
5769215
268435455...

result:


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%