QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#86657 | #5407. 基础图论练习题 | 2805734199h | 0 | 9ms | 10100kb | C++14 | 4.1kb | 2023-03-10 14:49:31 | 2023-03-10 14:49:35 |
Judging History
answer
#include <bits/stdc++.h>
#define N 5005
#define M 13000005
using namespace std;
const int mod=1e9+7;
int power(int x,int y) {
int ans=1;
while(y) {
if(y&1) ans=1ll*ans*x%mod;
x=1ll*x*x%mod,y>>=1;
}
return ans;
}
int T,n,ans,tot,first[N],nxt[M],ver[M],pw2[N],pw[N];
void add(int s,int e) {
nxt[++tot]=first[s];
first[s]=tot;
ver[tot]=e;
}
bool vis[N];
int dfn[N],totd,low[N],s[N],top,totb,blo[N];
vector<int> b[N];
void tarjan(int now,int fa) {
dfn[now]=low[now]=++totd;
s[++top]=now;
for(int i=first[now];i;i=nxt[i]) {
int v=ver[i];
if(v==fa) continue;
if(!dfn[v]) {
tarjan(v,now);
low[now]=min(low[now],low[v]);
} else if(vis[v]) {
low[now]=min(low[now],dfn[v]);
}
}
if(low[now]>=dfn[now]) {
blo[now]=++totb;
b[totb].push_back(now);
while(top&&s[top]!=now) {
b[totb].push_back(s[top]);
vis[s[top]]=0;
blo[s[top--]]=totb;
}
if(top) vis[s[top--]]=0;
}
}
int ind[N],oud[N],deg[N][N],wou[N],win[N];
vector<int> head[N],hb[N],hbin[N];
void build() {
for(int i=1;i<=n;++i) for(int j=first[i];j;j=nxt[j]) {
if(blo[i]==blo[ver[j]]) {
hb[i].push_back(ver[j]);
hbin[ver[j]].push_back(i);
} else {
head[blo[i]].push_back(blo[ver[j]]);
deg[blo[i]][blo[ver[j]]]++;
ind[blo[ver[j]]]++;
}
}
}
int dep[N],q[N+10],l,r;
void bfs() {
l=1,r=0;
for(int i=1;i<=totb;++i) {
if(!ind[i]) q[++r]=i;
}
while(l<=r) {
int u=q[l++],sz;
sz=head[u].size();
for(int i=0,v;i<sz;++i) {
v=head[u][i];
ind[v]--;
if(!ind[v]) {
dep[v]=dep[u]+1;
q[++r]=v;
}
}
}
}
string str;
void clear() {
for(;tot;--tot) nxt[tot]=0;
for(int i=1;i<=totb;++i) for(int j=1;j<=totb;++j) {
deg[i][j]=0;
}
for(int i=1;i<=n;++i) {
first[i]=0;
b[i].clear();
head[i].clear();
hb[i].clear();
hbin[i].clear();
blo[i]=dfn[i]=dep[i]=ind[i]=wou[i]=win[i]=0;
}
ans=totb=totd=0;
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
cin>>T;
while(T--) {
clear();
cin>>n;
pw[0]=1;
for(int i=1;i<=n;++i) {
pw[i]=(pw[i-1]<<1)%mod;
if(i>1) pw2[i]=power(2,(i-1)*(i-2)>>1);
}
for(int i=2,j,k,c,ed;i<=n;++i) {
cin>>str;
for(j=0;j<(i-1)/4+1;++j) {
ed=i-1-(j<<2);
c=isdigit(str[j])?str[j]-'0':str[j]-'A'+10;
for(k=0;k<4&&k<ed;++k) {
((c>>k)&1)?add(i,(j<<2)+k+1):add((j<<2)+k+1,i);
}
}
}
for(int i=1;i<=n;++i) {
if(!blo[i]) tarjan(i,0);
}
build();
bfs();
for(int i=1;i<=n;++i) {
oud[i]=hb[i].size();
ind[i]=hbin[i].size();
}
for(int i=1;i<=n;++i) {
if(oud[i]==1) {
wou[i]=1;
for(int j=0;j<ind[i];++j) {
if(oud[hbin[i][j]]==1) wou[i]++;
}
}
if(ind[i]==1) {
win[i]=1;
for(int j=0;j<oud[i];++j) {
if(ind[hb[i][j]]==1) win[i]++;
}
}
}
for(int i=1,u,v,res;i<=n;++i) {
u=blo[i];
for(int j=first[i];j;j=nxt[j]) {
v=ver[j];
if(u!=blo[v]) {
if(dep[u]<dep[blo[v]]-1||deg[u][blo[v]]>1) {
res=totb-dep[blo[v]]+dep[u];
} else res=totb;
} else {
int sz;
if(oud[i]==1&&ind[v]==1) {
res=2,l=1,r=0;
vis[i]=vis[v]=1;
q[++r]=i;q[++r]=v;
while(l<=r) {
int x=q[l++],y;
sz=hb[x].size();
for(int k=0;k<sz;++k) {
y=hb[x][k];
ind[y]--;
if(!ind[y]&&!vis[y]) res++,vis[y]=1,q[++r]=y;
}
sz=hbin[x].size();
for(int k=0;k<sz;++k) {
y=hbin[x][k];
oud[y]--;
if(!oud[y]&&!vis[y]) res++,vis[y]=1,q[++r]=y;
}
}
sz=b[u].size();
for(int k=0;k<sz;++k) {
vis[b[u][k]]=0;
oud[b[u][k]]=hb[b[u][k]].size();
ind[b[u][k]]=hbin[b[u][k]].size();
}
if(res<sz) res+=totb;
else res+=totb-1;
} else res=totb+wou[i]+win[v];
// cout<<"totb = "<<totb<<" "<<"res = "<<res<<"woui = "<<wou[i]<<"winv = "<<win[v]<<"\n";
}
// printf("j = %d res = %d\n",j,res);
// printf("i = %d v = %d woui = %d winv = %d res = %d\n",i,v,wou[i],win[v],res);
ans=(ans+1ll*pw2[max(i,v)]*pw[min(i,v)-1]%mod*res)%mod;
}
}
cout<<ans<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 9ms
memory: 10100kb
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:
160373004 13 147483626 718425409 252 0 1983 2 147483626 147483626 475286289 196602 2 475286289 252 14680057 5115 0 475286289 718425409 14680057 196602 196602 14680057 475286289 147483626 475286289 475286289 475286289 196602 147483626 475286289 196602 14680057 475286289 718425409 252 252 252 14680057...
result:
wrong answer 1st numbers differ - expected: '281603732', found: '160373004'
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%