QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#579019 | #9319. Bull Farm | Hanoist | TL | 0ms | 0kb | C++14 | 3.5kb | 2024-09-21 01:29:47 | 2024-09-21 01:29:47 |
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<functional>
#include<utility>
#include<cassert>
using namespace std;
inline int read(){
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
const int N = 2e3 + 11;
const int M = 2e6 + 11;
int o,cnt,tm,top,stak[N],dfn[N],low[N],col[N][N],pos[N][N];
int n,l,q,m,ecnt[N],head[N][N],f[N][N];
queue<int> Q;
vector<int> buc[2][N];
struct yjx{
int nxt,to;
}e[2][N];
struct wyx{
int a,b,c,res;
}qu[N];
bool cmp(wyx x,wyx y){
return x.c < y.c;
}
int v[N][N];
void init(){
for(int i = 0;i <= n;i++){
ecnt[i] = 0;
for(int j = 1;j <= n;j++) head[i][j] = -1;
}
}
void save(int u,int x,int y){
if(x == y) return;
e[u][++ecnt[u]] = (yjx){head[u][x],y};
head[u][x] = ecnt[u];
}
void tarjan(int u,int now){
int i,temp;
dfn[now] = low[now] = ++tm;
stak[++top] = now;
for(i = head[u][now];~i;i = e[u][i].nxt){
temp = e[u][i].to;
if(!dfn[temp]){
tarjan(u,temp);
low[now] = min(low[now],low[temp]);
}
else if(!col[u + 1][temp]) low[now] = min(low[now],low[temp]);
}
if(dfn[now] == low[now]){
col[u + 1][now] = ++cnt;
while(now != stak[top]){
col[u + 1][now] = cnt;
}
--top;
}
}
inline int solve(int u,int st,int ed){
int i,now,temp;
if(col[u][st] == col[u][ed]) return 1;
while(!Q.empty()) Q.pop();
Q.push(col[u][st]);
while(!Q.empty()){
now = Q.front();
if(col[u][ed] == now){
return 1;
}
for(i = head[u][now];~i;i = e[u][i].nxt){
temp = e[u][i].to;
Q.push(temp);
}
}
return 0;
}
char s[M];
int main(){
int i,j,k,x,y,t;
t = read();
while(t--){
n = read(),l = read(),q = read();
for(i = 1;i <= l;i++){
scanf("%s",s + 1);
for(j = 1;j <= strlen(s + 1);j += 2){
v[i][(j + 1) / 2] = (s[j] - '0') * 50 + (s[j + 1] - '0');
}
}
for(i = 1;i <= q;i++){
scanf("%s",s + 1);
qu[i].a = (s[1] - '0') * 50 + (s[2] - '0');
qu[i].b = (s[3] - '0') * 50 + (s[4] - '0');
qu[i].c = (s[5] - '0') * 50 + (s[6] - '0');
}
sort(qu + 1,qu + q + 1,cmp);
o = 0;
init();
for(i = 1;i <= n;i++) col[0][i] = pos[0][i] = i;
for(i = 1,j = 1;i <= q;i++){
bool up = 0;
while(j <= l && j <= qu[i].c){
up = 1;
for(k = 1;k <= n;k++) save(o,pos[o][v[j][k]],pos[o][k]);
++j;
}
if(up){
for(i = 1;i <= n;i++){
if(!dfn[pos[o][i]]) tarjan(o,pos[o][i]);
}
for(i = 1;i <= n;i++){
for(k = head[o][i];~k;k = e[o][k].nxt){
int temp = e[o][k].to;
if(col[o + 1][i] != col[o + 1][temp]) save(o + 1,col[o + 1][i],col[o + 1][temp]);
}
}
++o;
}
printf("%d\n",solve(o,qu[i].a,qu[i].b));
}
}
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
2 5 2 4 0305040201 0404040404 030300 020500 050102 020501 6 2 4 030603010601 010203060504 030202 060402 050602 060401