QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#212120 | #5407. 基础图论练习题 | Rong7 | 0 | 0ms | 0kb | C++14 | 4.5kb | 2023-10-13 09:22:44 | 2023-10-13 09:22:44 |
answer
#include <bits/stdc++.h>
using namespace std;
int T, n;
int E[5005][5005];
int firs[5005], nex[25000005], to[25000005], tot;
char s[2005];
int idx, dfn[5005], low[5005], bel[5005], sz[5005], cnt, sta[5005], top;
int ct[5005][5005], fw[5005][5005], rd_in_Scc[5005];
vector < int > vet[5005];
bool ins[5005];
long long Ans;
void Clear (){
for (int i = 1;i <= n;++ i)
for (int j = 1;j <= n;++ j)
E[i][j] = fw[i][j] = 0;
for (int i = 1;i <= n;++ i){
bel[i] = 0;
sz[i] = 0;
rd_in_Scc[i] = 0;
vet[i].clear ();
firs[i] = 0;
dfn[i] = low[i] = 0;
}
cnt = 0;
tot = 0;
idx = 0;
Ans = 0;
}
void Add (int u, int v){
E[u][v] = 1;
E[v][u] = - 1;
++ tot;
nex[tot] = firs[u];
firs[u] = tot;
to[tot] = v;
}
int Represent (char a){
if (a >= '0' && a <= '9')
return a - '0';
else
return 10 + a - 'A';
}
void Tarjan (int u){
if (dfn[u] > 0)
return ;
low[u] = dfn[u] = ++ idx;
sta[++ top] = u;
ins[u] = true;
for (int e = firs[u], v;e;e = nex[e]){
v = to[e];
if (dfn[v] == 0){
Tarjan (v);
low[u] = min (low[u], low[v]);
} else
if (ins[v])
low[u] = min (low[u], dfn[v]);
}
if (dfn[u] == low[u]){
int v;
++ cnt;
do {
v = sta[top --];
++ sz[cnt];
bel[v] = cnt;
ins[v] = false;
vet[cnt].push_back (v);
} while (u != v);
for (int p = 0;p < (int) vet[cnt].size ();++ p)
for (int e = firs[vet[cnt][p]];e;e = nex[e])
if (bel[to[e]] == cnt)
++ rd_in_Scc[to[e]];
for (int p = 0;p < (int) vet[cnt].size ();++ p)
++ ct[cnt][rd_in_Scc[vet[cnt][p]]];
for (int i = sz[cnt];i >= 0;-- i)
if (rd_in_Scc[i] == 0)
fw[cnt][i] = i;
else
fw[cnt][i] = fw[cnt][i + 1];
}
}
const long long mod = 1e9 + 7;
long long Qpow (long long a, long long p){
long long Res = 1;
while (p > 0){
if (p & 1)
Res = Res * a % mod;
a = a * a % mod;
p >>= 1;
}
return Res;
}
int main (){
scanf ("%d", &T);
while (T --){
scanf ("%d", &n);
for (int i = 2;i <= n;++ i){
int l = (i - 1) / 4 + 1;
scanf ("\n%s", s + 1);
for (int j = 1;j <= l;++ j){
for (int k = 4 * j - 3, x = Represent (s[j]);k <= 4 * j && k < i;++ k, x >>= 1)
if (x & 1)
Add (i, k);
else
Add (k, i);
}
}
for (int i = 1;i <= n;++ i)
Tarjan (i);
for (int i = 1;i <= n;++ i){
long long w = Qpow (2, (i - 1) * (i - 2) / 2);
for (int j = 1;j < i;++ j){
int u = i, v = j;
if (E[u][v] < 0)
swap (u, v);
if (bel[u] != bel[v]){
if (bel[u] - 1 == bel[v])
(Ans += w * cnt % mod) %= mod;
else
(Ans += w * (cnt - (bel[u] - bel[v])) % mod) %= mod;
} else {
int rep = bel[u];
int p = 0, lim = sz[rep];
-- ct[rep][rd_in_Scc[u]];
++ rd_in_Scc[u];
++ ct[rep][rd_in_Scc[u]];
-- ct[rep][rd_in_Scc[v]];
-- rd_in_Scc[v];
++ ct[rep][rd_in_Scc[v]];
if (ct[rep][rd_in_Scc[u] - 1] == 0)
lim = min (lim, rd_in_Scc[u] - 1);
if (ct[rep][rd_in_Scc[v] + 1] == 0)
lim = min (lim, rd_in_Scc[v] + 1);
while (ct[rep][p] > 0){
p = fw[rep][p + 1];
p = min (p, lim);
}
-- ct[rep][rd_in_Scc[u]];
-- rd_in_Scc[u];
++ ct[rep][rd_in_Scc[u]];
-- ct[rep][rd_in_Scc[v]];
++ rd_in_Scc[v];
++ ct[rep][rd_in_Scc[v]];
(Ans += w * (cnt + p) % mod) %= mod;
}
(w <<= 1) %= mod;
}
}
printf ("%lld\n", Ans);
Clear ();
}
return 0;
}
詳細信息
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:
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%