QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411449 | #4000. Dynamic Reachability | ChrysanthBlossom | TL | 4ms | 11824kb | C++17 | 4.6kb | 2024-05-15 13:53:11 | 2024-05-15 13:53:13 |
Judging History
answer
#include<bits/stdc++.h>
#include<string>
#define ri register int
#define ll long long
#define mkp make_pair
using namespace std;
const int maxn=1e5+7;
int n,m,q;
struct opt{
int op,u,v;
int id;
}qry[maxn<<1];
vector<pair<int,int> >edge;
const int B=32;
bool tongxing[maxn<<1];
namespace Small_G{
int ed[65][65];
ll fr[65];
bool vis[65];
void clear(){
memset(ed,0,sizeof(ed));
for(ri i=1;i<=64;i++){
fr[i]=0;
}
}
void del(int u,int v){
ed[u][v]--;
// cout<<"del:"<<u<<' '<<v<<endl;
}
void add(int u,int v){
ed[u][v]++;
// cout<<"add:"<<u<<' '<<v<<endl;
}
int n;
bool find(int u,int goal){
if(u==goal)return 1;
vis[u]=1;
for(ri v=1;v<=n;v++){
if(ed[u][v]&&!vis[v]){
if(find(v,goal))return 1;
}
}
return 0;
}
bool kd(int u,int v){
// cout<<"query:"<<u<<' '<<v<<endl;
memset(vis,0,sizeof(vis));
return find(u,v);
}
}
struct my_queue{
int num[maxn<<1];
int head,tail;
void clear(){
head=1,tail=0;
}
void push(int x){
num[++tail]=x;
}
void pop(){
++head;
}
int front(){
return num[head];
}
bool empty(){
return tail<head;
}
}qq;
int dfn[maxn<<1],low[maxn<<1];
int tim;
vector<int>st;
int belong[maxn];
int pid;
vector<int>scc[maxn];
ll kd[maxn<<1];
int cnt,head[maxn<<1],to[maxn<<2],nxt[maxn<<2];
int d[maxn<<1];
void add(int u,int v){
++cnt;
d[v]++;
to[cnt]=v;
nxt[cnt]=head[u];
head[u]=cnt;
}
void clear(){
memset(head,0,sizeof(head));
memset(d,0,sizeof(d));
memset(kd,0,sizeof(kd));
memset(belong,0,sizeof(belong));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
for(ri i=n+1;i<=pid;i++){
scc[i].clear();
}
tim=cnt=0;
pid=n;
}
void tarjan(int u){
dfn[u]=low[u]=++tim;
st.emplace_back(u);
for(ri e=head[u];e;e=nxt[e]){
int v=to[e];
if(belong[v]){
continue;
}
if(dfn[v]){
low[u]=min(low[u],dfn[v]);
continue;
}
tarjan(v);
low[u]=min(low[u],low[v]);
}
if(low[u]!=dfn[u])return;
pid++;
while(st.back()!=u){
int tmp=st.back();
belong[tmp]=pid;
kd[pid]|=kd[tmp];
scc[pid].emplace_back(tmp);
st.pop_back();
}
int tmp=st.back();
belong[tmp]=pid;
kd[pid]|=kd[tmp];
scc[pid].emplace_back(tmp);
st.pop_back();
}
bool is_operated[maxn<<1];
int new_id[maxn];
vector<int>pnt;
void solve(int l,int r){
for(ri i=l;i<=r;i++){
if(qry[i].op==1)is_operated[qry[i].id]=1;
new_id[qry[i].u]=new_id[qry[i].v]=1;
}
pnt.clear();
for(ri i=1;i<=n;i++){
if(new_id[i]){
pnt.emplace_back(i);
new_id[i]=pnt.size();
}
}
clear();
for(ri i=0;i<edge.size();i++){
if(tongxing[i]&&!is_operated[i]){
add(edge[i].first,edge[i].second);
}
}
for(ri i=1;i<=n;i++){
if(new_id[i]){
kd[i]=1<<new_id[i]-1;
}
}
for(ri i=1;i<=n;i++){
if(dfn[i]==0){
tarjan(i);
}
}
for(ri u=1;u<=n;u++){
for(ri e=head[u];e;e=nxt[e]){
int v=to[e];
if(belong[u]!=belong[v]){
add(belong[u],belong[v]);
}
}
}
qq.clear();
for(ri i=n+1;i<=pid;i++){
if(d[i]==0){
qq.push(i);
}
}
while(!qq.empty()){
int u=qq.front();qq.pop();
for(ri e=head[u];e;e=nxt[e]){
int v=to[e];
kd[v]|=kd[u];
d[v]--;
if(d[v]==0)qq.push(v);
}
}
for(ri i=n+1;i<=pid;i++){
for(auto x:scc[i]){
kd[x]=kd[i];
}
}
Small_G::clear();
Small_G::n=pnt.size();
for(auto p:pnt){
for(auto x:pnt){
if(x!=p&&(((kd[p]>>new_id[x]-1)&1))){
Small_G::add(new_id[x],new_id[p]);
}
}
}
for(ri i=0;i<edge.size();i++){
if(is_operated[i]&&tongxing[i])Small_G::add(new_id[edge[i].first],new_id[edge[i].second]);
}
for(ri i=l;i<=r;i++){
if(qry[i].op==1){
if(tongxing[qry[i].id]){
tongxing[qry[i].id]=0;
Small_G::del(new_id[qry[i].u],new_id[qry[i].v]);
}
else{
tongxing[qry[i].id]=1;
Small_G::add(new_id[qry[i].u],new_id[qry[i].v]);
}
}
else{
cout<<(Small_G::kd(new_id[qry[i].u],new_id[qry[i].v])?"YES\n":"NO\n");
}
}
memset(new_id,0,sizeof(new_id));
memset(is_operated,0,sizeof(is_operated));
}
signed main(){
ios::sync_with_stdio(0);
cin>>n>>m>>q;
for(ri i=1;i<=m;i++){
int u,v;
cin>>u>>v;
edge.emplace_back(mkp(u,v));
}
memset(tongxing,1,sizeof(tongxing));
for(ri i=1;i<=q;i++){
cin>>qry[i].op;
if(qry[i].op==1){
cin>>qry[i].id;
qry[i].id--;
qry[i].u=edge[qry[i].id].first;
qry[i].v=edge[qry[i].id].second;
}
else{
cin>>qry[i].u>>qry[i].v;
}
}
for(ri i=1;i<=q/B;i++){
solve((i-1)*B+1,i*B);
}
if(q%B)solve(q/B*B+1,q);
return 0;
}
/*
5 6 7
1 2
1 3
2 4
3 4
3 5
4 5
2 1 5
2 2 3
1 3
1 4
2 1 4
1 3
2 1 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 11824kb
input:
5 6 7 1 2 1 3 2 4 3 4 3 5 4 5 2 1 5 2 2 3 1 3 1 4 2 1 4 1 3 2 1 5
output:
YES NO NO YES
result:
ok 4 lines
Test #2:
score: -100
Time Limit Exceeded
input:
50000 100000 100000 36671 44121 25592 44321 13226 46463 13060 25694 14021 20087 22881 38333 34655 47774 22868 26462 31154 48710 27491 32365 5874 47497 17622 28600 1886 14193 22315 23656 14973 22704 1335 25384 22612 34915 2852 48213 23334 25519 24342 28784 6238 36125 14598 39494 33069 34250 2123 3059...
output:
NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO ...