QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#471484 | #5504. Flower Garden | l_rANd | WA | 0ms | 17948kb | C++20 | 3.6kb | 2024-07-10 21:31:57 | 2024-07-10 21:31:58 |
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,xa,xb,tmp=np(),0);
upd_link(1,1,n,xc,xd,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: 0
Wrong Answer
time: 0ms
memory: 17948kb
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 FRR NIE
result:
wrong answer odpowiedz nie spelnia wymogow!