QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#471509 | #5504. Flower Garden | l_rANd | TL | 2ms | 15800kb | C++20 | 3.6kb | 2024-07-10 21:40:52 | 2024-07-10 21:40:53 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int read(){
int ret=0,f=1;char c=getchar();
while(c>57||c<48){if(c=='-')f= -1;c=getchar();}
while(c<=57&&c>=48)ret=(ret<<3)+(ret<<1)+(c-48),c=getchar();
return ret*f;
}
void write(int x){
if(x<0) x= -x,putchar('-');
if(x>9)write(x/10);
putchar(x%10+48);
}
const int maxn=1e5+5;
const int mod =1e9+7;
int n,m;
int pcnt;
int np(){return ++pcnt;}
vector<int>g1[maxn*10],g2[maxn*10];
struct ed{
int h,t,w;
}e[maxn<<6];
int last[maxn*10],ecnt;
void link(int x,int y,bool o){
if(o)swap(x,y);
cerr<<x<<' '<<y<<endl;
e[++ecnt]={last[x],y};
last[x]=ecnt;
}
#define ls x<<1
#define rs x<<1|1
int tr[2][maxn<<2];
void build(int x,int l,int r,bool o){
if(!tr[o][x])tr[o][x]=np();
if(l==r){link(tr[o][x],l,o);return ;}
int mid=l+r>>1;
build(ls,l,mid,o);
build(rs,mid+1,r,o);
link(tr[o][x],tr[o][ls],o);
link(tr[o][x],tr[o][rs],o);
// cerr<<tr[o][x]<<' '<<tr[o][ls]<<' '<<tr[o][rs]<<endl;
}
void upd_link(int x,int l,int r,int nl,int nr,int k,int o){
if(nl<=l&&r<=nr){link(k,tr[o][x],o);return;}
int mid=l+r>>1;
if(nl<=mid)upd_link(ls,l,mid,nl,nr,k,o);
if(mid<nr)upd_link(rs,mid+1,r,nl,nr,k,o);
}
int dfn[maxn*10],low[maxn*10],dcnt,col[maxn*10],ccnt,siz[maxn*10],s[maxn*10],top;
bool vis[maxn*10];
void tarjan(int u){
dfn[u]=low[u]=++dcnt;
s[++top]=u,vis[u]=1;
for(int i=last[u];i;i=e[i].h){
int v=e[i].t;
if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
else if(vis[v])low[u]=min(low[u],low[v]);
}
if(dfn[u]==low[u]){
ccnt++;
while(top){
col[s[top]]=ccnt;
siz[ccnt]+=(s[top]<=n);
vis[s[top]]=0;
if(s[top--]==u)break;
}
}
}
int in[maxn*10];
bool ans[maxn*10];
void clear(){
for(int i=1;i<=n*4;i++)tr[i][0]=tr[i][1]=0;
for(int i=1;i<=pcnt;i++)last[i]=0;
for(int i=1;i<=ccnt;i++)ans[i]=siz[i]=0;
for(int i=1;i<=ccnt;i++)g1[i].clear(),g2[i].clear();
for(int i=1;i<=pcnt;i++)dfn[i]=low[i]=col[i]=0;
ecnt=0,ccnt=0,dcnt=0,pcnt=0;
}
void print(bool x){
if(x)puts("TAK");
else {puts("NIE");return;}
for(int i=1;i<=n;i++)putchar("RF"[ans[col[i]]]);
putchar('\n');
}
void solve(){
clear();
n=read()*3,m=read();pcnt=n;
build(1,1,n,0);build(1,1,n,1);
for(int i=1;i<=m;i++){
int xa=read(),xb=read(),xc=read(),xd=read(),tmp;
upd_link(1,1,n,xc,xd,tmp=np(),0);
upd_link(1,1,n,xa,xb,tmp,1);
}
tarjan(tr[0][1]);
for(int u=1;u<=pcnt;u++){
// cout<<col[u]<<' ';
for(int i=last[u];i;i=e[i].h){
int v=e[i].t;
if(col[u]!=col[v])g1[col[u]].push_back(col[v]),g2[col[v]].push_back(col[u]);
}
}
queue<int>q;
for(int i=1;i<=ccnt;i++)in[i]=g1[i].size();
for(int i=1;i<=ccnt;i++)if(!in[i])q.push(i);
int sum=0;
while(!q.empty()){
int u=q.front();q.pop();
// cerr<<u<<endl;
if(siz[u]>n/3)continue;
sum+=siz[u];ans[u]=1;
if(sum>=n/3){print(1);return;}
for(int v:g2[u])if(!--in[v])q.push(v);
}
for(int i=1;i<=ccnt;i++)if(siz[i]>n/3){
for(int j=1;j<=ccnt;j++)ans[j]=0,in[j]=g2[j].size();
ans[i]=1;sum=0;
for(int j=1;j<=ccnt;j++)if(!in[j])q.push(j);
while(!q.empty()){
int u=q.front();q.pop();
for(int v:g1[u]){ans[v]|=ans[u];if(!--in[v])q.push(v);}
}
for(int j=1;j<=ccnt;j++)sum+=ans[j]*siz[j];
if(sum<=n/3*2){print(1);return;}
}
print(0);
}
signed main(){
int T=read();
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 15800kb
input:
2 1 3 1 1 2 2 1 2 3 3 1 1 3 3 1 3 1 1 2 2 2 2 3 3 3 3 1 1
output:
TAK RRF NIE
result:
ok good!
Test #2:
score: -100
Time Limit Exceeded
input:
10 33333 100000 28701 40192 93418 95143 95902 97908 78378 78461 36823 44196 22268 23996 23977 24786 33315 48829 83965 90411 4923 8445 20235 21177 32543 47454 29598 35414 72477 73049 2014 12632 42163 46466 64305 65518 98825 99552 32331 41625 92772 96224 26500 54122 76990 77126 18249 20335 31165 36080...
output:
NIE NIE