QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#571439 | #9319. Bull Farm | yukino_yukinoshita | TL | 182ms | 11704kb | C++14 | 3.4kb | 2024-09-17 23:05:13 | 2024-09-17 23:05:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
const int M = 1e6 + 5;
int n, m, l, t[N][N];
int vis[N], flg[N], u1[N], u2[N], tt[N];
struct Qry {int a, b, c, id;} q[M];
bool cmp(Qry x, Qry y) {return x.c < y.c;}
bitset<2048> f[N];
int fa[N];
int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}
void link(int x, int y)
{
for(int i = 1; i <= n; i++)
if(f[i][x]) f[i] |= f[y];
}
void merge(int x, int y)
{
x = find(x), y = find(y);
link(x, y), link(y, x);
fa[y] = x;
}
bool vs[N];
bool g[N][N];
bool bfs(int s, int t)
{
memset(vs, 0, sizeof(vs));
queue<int> que;
que.push(s); vs[s] = 1;
while(!que.empty())
{
int u = que.front(); que.pop();
// printf("u = %d\n", u);
if(u == t) return 1;
for(int i = 1; i <= n; i++) if(g[u][i] && !vs[i])
que.push(i), vs[i] = 1;
}
return 0;
}
void solve()
{
cin >> n >> l >> m;
for(int i = 1; i <= l; i++)
{
string s; cin >> s;
for(int j = 1; j <= n; j++) vis[j] = 0;
int cnt = 0;
for(int j = 1; j <= n; j++)
{
t[i][j] = 50 * (s[2 * j - 2] - '0') + s[2 * j - 1] - '0';
// cout << t[i][j] << " \n"[j == n];
if(vis[t[i][j]])
{
cnt++;
if(cnt == 1) flg[i] = 1, u1[i] = j, u2[i] = vis[t[i][j]];
else flg[i] = 2;
}
vis[t[i][j]] = j;
}
if(flg[i] == 1)
{
for(int j = 1; j <= n; j++) if(!vis[j])
{
tt[i] = j;
break;
}
}
}
// cout << "\n";
for(int i = 1; i <= m; i++)
{
string s; cin >> s;
q[i].a = 50 * (s[0] - '0') + s[1] - '0';
q[i].b = 50 * (s[2] - '0') + s[3] - '0';
q[i].c = 50 * (s[4] - '0') + s[5] - '0';
q[i].id = i;
// cerr << q[i].a << " " << q[i].b << " " << q[i].c << "\n";
}
sort(q + 1, q + m + 1, cmp);
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++) f[i][i] = 1, fa[i] = i;
memset(g, 0, sizeof(g));
string ans; ans.resize(m);
for(int i = 1, cc = 1; i <= m; i++)
{
for(; cc <= q[i].c; cc++)
{
// printf("cc = %d q = %d\n", cc, q[i].c);
if(flg[cc] == 2) continue;
if(flg[cc] == 1)
{
// printf("edge1 %d %d\n", u1[cc], tt[cc]);
// printf("edge2 %d %d\n", u2[cc], tt[cc]);
g[u1[cc]][tt[cc]] = 1;
g[u2[cc]][tt[cc]] = 1;
continue;
}
for(int u = 1; u <= n; u++) if(vis[u] == 0)
{
int st = u;
bool ok = 0;
bitset<2048> to, from;
for(; vis[u] == 0; u = t[cc][u])
{
vis[u] = 1, to |= f[u], from[u] = 1;
if(find(u) != find(st)) merge(st, u);
// printf("edge3 %d %d\n", st, u);
// printf("edge3 %d %d\n", u, st);
g[st][u] = g[u][st] = 1;
}
}
memset (vis, 0, sizeof(vis));
}
// printf("cc = %d\n",)
// ans[q[i].id - 1] = f[q[i].a][q[i].b] + '0';
// printf("%d %d\n", q[i].a, q[i].b);
// for(int I=1; I<=n; ++I) for(int J=1; J<=n; ++J) printf("%d%c", g[I][J], J==n?10:32);
ans[q[i].id - 1] = bfs(q[i].a, q[i].b) + '0';
}
cout << ans << "\n";
for(int i = 1; i <= n; i++) f[i].reset();
for(int i = 1; i <= l; i++) flg[i] = 0;
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T; cin >> T;
while(T--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 11704kb
input:
2 5 2 4 0305040201 0404040404 030300 020500 050102 020501 6 2 4 030603010601 010203060504 030202 060402 050602 060401
output:
1011 0100
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 10524kb
input:
1 3 3 6 020202 030301 030201 020102 030203 010201 010303 020303 010202
output:
010101
result:
ok single line: '010101'
Test #3:
score: 0
Accepted
time: 182ms
memory: 10792kb
input:
200 10 10 5000 01060:04020305080709 0103070:060204050908 09070503080401060:02 050308010204090:0607 03010502040607080:09 03080109020504060:07 06050:09040302080107 07080305010409060:02 030809010:0204060507 0:060908070201050304 060700 090:03 09080: 070405 010703 0:0100 080601 030600 070206 0:0:09 08040...
output:
011110001101101111111111111111111101111111110111011110110110111011010111111111111111111101111111111110111111110111111111111101111111111110111111111111111111110001100111111111111111111111111011101111111111111111111111111111111111111111011011110100111110111111110111111100111111101110111111111101111110...
result:
ok 200 lines
Test #4:
score: -100
Time Limit Exceeded
input:
1 2000 1 1000000 M=:]A@8UAY7W2JJ4KEHIA[HSCQ1ENSC`JXR;F3PJ:_@41P9Z=9HR8P<<:DUXRR9^WOQFL?NZP6S@=J0^WE32=6;\U0?88]Q_RNPUMT6YU<4<S]H?:7OCQYOT4YAV1^764ENWSDBED>M7A:BI>KSIR48JQ9B=N\5T3N4A2aF0@>3TI81<G7;YE>W`NMP<:IT4CI3D0=GZC3I\CLQJQBA9BDIS9SAM55KaVA<Z@D=>:Y?CQHUQ5U3a6UVI8OKX9_FAF^7=5M85;<0;8YPAM<7Z7PP7A=N...