QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#470815 | #5504. Flower Garden | Mathew_Miao | WA | 7ms | 133308kb | C++23 | 5.2kb | 2024-07-10 16:34:57 | 2024-07-10 16:34:58 |
Judging History
answer
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<string>
#include<bitset>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e5+10;
const int MAXM=1.2e6+10;
const int INF=0x3f3f3f3f;
const long long LINF=0x3f3f3f3f3f3f3f3f;
int n,q;
int idx=0;
basic_string <int> gr[MAXM];
inline void add_edge(int x,int y){
gr[x].push_back(y);
}
namespace segtree{
int L[MAXN*4],R[MAXN*4];
int s[MAXN*4],t[MAXN*4];
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
void build(int l,int r,int x){
L[x]=l;
R[x]=r;
if(l==r){
s[x]=l;
t[x]=l;
return ;
}
idx++;
s[x]=idx;
idx++;
t[x]=idx;
int mid=(l+r)>>1;
build(l,mid,ls(x));
build(mid+1,r,rs(x));
add_edge(s[ls(x)],s[x]);
add_edge(s[rs(x)],s[x]);
add_edge(t[x],t[ls(x)]);
add_edge(t[x],t[rs(x)]);
}
inline void build(){
build(1,n*3,1);
}
void edge(int ql,int qr,int to,int x){
int l=L[x],r=R[x];
if(ql<=l&&r<=qr){
if(to>0){
add_edge(s[x],to);
}
else{
add_edge(-to,t[x]);
}
return ;
}
if(qr<l||r<ql){
return ;
}
edge(ql,qr,to,ls(x));
edge(ql,qr,to,rs(x));
}
inline void edge(int ql,int qr,int to){
edge(ql,qr,to,1);
}
}
int dfc=0,cnt=0;
int dfn[MAXM],low[MAXM];
int col[MAXM],siz[MAXM];
int top=0;
int st[MAXM];
bool inst[MAXM];
void tarjan(int x){
dfc++;
dfn[x]=dfc;
low[x]=dfc;
top++;
st[top]=x;
inst[x]=true;
for(int to:gr[x])
{
if(!dfn[to]){
tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(inst[to]){
low[x]=min(low[x],dfn[to]);
}
}
if(low[x]==dfn[x]){
cnt++;
while(st[top]^x)
{
col[st[top]]=cnt;
siz[cnt]+=(st[top]<=n*3);
inst[st[top]]=false;
top--;
}
top--;
col[x]=cnt;
siz[cnt]+=(x<=n*3);
inst[x]=false;
}
}
int out[MAXM];
basic_string <int> tr[MAXM],fr[MAXM];
inline void add_tr(int x,int y){
tr[x].push_back(y);
fr[y].push_back(x);
out[x]++;
}
inline void tarjan(){
for(int i=1;i<=idx;i++)
{
if(!dfn[i]){
tarjan(i);
}
}
for(int i=1;i<=idx;i++)
{
for(int to:gr[i])
{
if(col[i]^col[to]){
add_tr(col[i],col[to]);
}
}
}
}
bool vis[MAXM];
inline bool topo(){
memset(vis,false,sizeof(int)*(cnt+1));
queue <int> q;
for(int i=1;i<=cnt;i++)
{
if(!out[i]){
q.push(i);
}
}
int sum=0;
while(!q.empty())
{
int x=q.front();
q.pop();
if(siz[x]>=n){
continue;
}
vis[x]=true;
sum+=siz[x];
if(sum>n){
return true;
}
for(int f:fr[x])
{
out[f]--;
if(!out[f]){
q.push(f);
}
}
}
return false;
}
int dfs(int x){
if(vis[x]){
return 0;
}
vis[x]=true;
int res=siz[x];
for(int to:tr[x])
{
res+=dfs(to);
}
return res;
}
inline bool check(){
for(int i=1;i<=cnt;i++)
{
if(siz[i]>=n){
memset(vis,false,sizeof(int)*(cnt+1));
if(dfs(i)<=2*n){
return true;
}
}
}
return false;
}
inline void solve(){
scanf("%d%d",&n,&q);
idx=n*3;
segtree::build();
for(int i=1;i<=q;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
idx++;
int to=idx;
segtree::edge(a,b,to);
segtree::edge(c,d,-to);
}
tarjan();
if(topo()){
puts("TAK");
for(int i=1;i<=n*3;i++)
{
if(vis[col[i]]){
putchar('R');
}
else{
putchar('F');
}
}
putchar('\n');
return ;
}
if(check()){
puts("TAK");
for(int i=1;i<=n*3;i++)
{
if(vis[col[i]]){
putchar('R');
}
else{
putchar('F');
}
}
putchar('\n');
return ;
}
puts("NIE");
}
inline void clear(){
memset(dfn,0,sizeof(int)*(idx+1));
memset(low,0,sizeof(int)*(idx+1));
memset(col,0,sizeof(int)*(idx+1));
for(int i=1;i<=idx;i++)
{
gr[i].clear();
}
memset(siz,0,sizeof(int)*(cnt+1));
memset(out,0,sizeof(int)*(cnt+1));
for(int i=1;i<=cnt;i++)
{
tr[i].clear();
fr[i].clear();
}
idx=0;
cnt=0;
}
signed main(){
int t;
scanf("%d",&t);
while(t--)
{
solve();
clear();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 133308kb
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 FFR NIE
result:
wrong answer odpowiedz nie spelnia wymogow!