QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#99302 | #5504. Flower Garden | Fanch100 | TL | 4ms | 54708kb | C++14 | 4.7kb | 2023-04-21 21:13:00 | 2023-04-21 21:13:02 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int RN = 100010;
const int N = 500010;
const int M = N*2;
int n, Q;
int head[N], pre[M], ver[M], tot, from[M];
void add(int x,int y){
ver[++tot]=y; pre[tot]=head[x]; head[x]=tot; from[tot]=x;
// printf("x=%d y=%d\n",x,y);
}
int idx;
int spl[N];
int w[N];
int ls[RN<<2], rs[RN<<2];
void build(int &p,int l,int r,bool z){
if (l==r) {
if (!w[l]) w[l]=++idx, spl[idx]=l;
p=w[l];
return;
}
p=++idx;
// printf("p=%d l=%d r=%d\n",p,l,r);
int mid=(l+r)>>1;
build(ls[p],l,mid,z); build(rs[p],mid+1,r,z);
if (!(z&1)) add(ls[p],p),add(rs[p],p);
else add(p,ls[p]),add(p,rs[p]);
}
void update(int p,int l,int r,int pl,int pr,int d,bool z){
if (l<=pl && pr<=r){
if (!(z&1)) add(p,d);
else add(d,p);
return;
}
int mid=(pl+pr)>>1;
if (mid>=l) update(ls[p],l,r,pl,mid,d,z);
if (mid+1<=r) update(rs[p],l,r,mid+1,pr,d,z);
}
int dfn[N], low[N], tp, st[N], top, scc[N], sc, siz[N];
bool ins[N];
vector<int> inc[N];
void tarjan(int x){
dfn[x]=low[x]=++tp;
st[++top]=x; ins[x]=1;
for (int i=head[x]; i; i=pre[i]){
int v=ver[i];
if (!dfn[v]){
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if (ins[v]) low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x]){
sc++;
while(1){
int qh=st[top--];
scc[qh]=sc;
if (spl[qh]) siz[sc]++, inc[sc].push_back(spl[qh]);
ins[qh]=0;
if (qh==x) break;
}
}
}
vector<int> G0[N], G1[N];
set<pair<int,int> > s;
int dg[N];
bool vis[N];
vector<int> ans;
int bfs(int x){
for (int i=1;i<=sc;++i) vis[i]=0; ans.clear();
queue<int> q;
q.push(x);
while(q.size()){
int x=q.front(); q.pop();
if (vis[x]) continue;
vis[x]=1;
for (int i : inc[x]) ans.push_back(i);
for (int v : G0[x]) q.push(v);
}
if (ans.size()<=n/3*2) return 0;
// for (int i=1;i<=sc;++i) vis[i]=0; ans.clear();
vis[x]=0;
q.push(x);
while(q.size()){
int x=q.front(); q.pop();
if (vis[x]) continue;
vis[x]=1;
for (int i : inc[x]) ans.push_back(i);
for (int v : G1[x]) q.push(v);
}
if (ans.size()<=n/3*2) return 1;
return 2;
}
void clear(){for (int i=1;i<=idx;++i) dfn[i]=dg[i]=spl[i]=w[i]=siz[i]=head[i]=0, G0[i].clear(), G1[i].clear(), inc[i].clear(); tp=0; tot=0;}
bool anss[N];
void solve(){
s.clear();
cin>>n>>Q; n*=3;
int nn=n/3;
tot=0; idx=0;
int rt0=0, rt1=0;
build(rt0,1,n,0);
build(rt1,1,n,1);
while(Q--){
int l1,r1,l2,r2; cin>>l1>>r1>>l2>>r2;
++idx;
update(rt0,l1,r1,1,n,idx,0);
update(rt1,l2,r2,1,n,idx,1);
}
for (int i=1;i<=idx;++i){
if (!dfn[i]) tarjan(i);
}
for (int i=1;i<=tot;++i){
if (scc[from[i]]!=scc[ver[i]]){
int x=scc[from[i]], y=scc[ver[i]];
if (s.find({x,y})!=s.end()) continue;
s.insert({x,y});
G0[y].push_back(x); G1[x].push_back(y);
dg[y]++;
// printf("un x=%d y=%d dg=%d\n",x,y,dg[y]);
}
}
// for (int i=1;i<=idx;++i) printf("i=%d sc=%d\n",i,scc[i]);
// for (int i=1;i<=sc;++i) {
// printf("i=%d dg=%d siz=%d\n",i,dg[i],siz[i]);
// for (int j : inc[i]) printf("%d ",j); puts("");
// }
for (int i=1;i<=sc;++i){
if (siz[i]>2*nn){
puts("NIE");
clear();
return;
}
if (siz[i]>=nn){
int tmp=bfs(i);
if (tmp!=2){
puts("TAK");
// printf("tmp=%d i=%d\n",tmp,i);
for (int j=1;j<=n;++j) anss[j]=(tmp^1);
for (int j : ans) anss[j]=tmp;
for (int j=1;j<=n;++j) printf(anss[j]?"F":"R"); puts("");
clear();
return;
}
}
}
// puts("Yes");
for (int i=1;i<=n;++i) anss[i]=1;
queue<int> q; ans.clear();
for (int i=1;i<=n;++i){
if (!dg[i]) q.push(i);
}
while(q.size()){
int x=q.front(); q.pop();
for (int i : inc[x]) ans.push_back(i);
if (ans.size()>=n){
puts("TAK");
clear();
for (int j : ans) anss[j]=0;
for (int j=1;j<=n;++j) printf(anss[j]?"F":"R"); puts("");
return;
}
for (int v : G1[x]){
if (--dg[v]==0) q.push(v);
}
}
clear();
puts("NIE");
}
int main(){
int t; cin>>t;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 54708kb
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...