QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#190666 | #2830. Data Structure | qzez | WA | 2ms | 16120kb | C++14 | 4.4kb | 2023-09-29 12:11:38 | 2023-09-29 12:11:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
if(x.empty())return out<<"[]";
out<<'['<<x[0];
for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
return out<<']';
}
template<typename T>
ostream& operator << (ostream &out,const pair<T,T>&x){
return out<<x.first;
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
cerr<<x<<' ',debug(y...);
}
const int N=2e5+10;
int n,m,in[N],out[N];
vector<int>ept,ok,only[N];
vector<pair<int,int> >to[N];
void addedge(int u,int v,int i){
// debug("edges",u,v);
out[u]++,in[v]++;
to[u].push_back({v,i});
to[v].push_back({u,-i});
}
#define v e.first
#define w e.second
int vis[N];
bool judge(int u,int fa=0){
vis[u]=1;
for(auto e:to[u])if(v^fa){
if(judge(v,u))return 1;
}
return out[u]==2;
}
void cover(int u){
if(vis[u])return;
vis[u]=1;
for(auto e:to[u])cover(v);
}
int k,id[N],p[N];
void find(int u,int las=0){
if(vis[u])return;
vis[u]=1,id[++k]=u,p[k]=las;
for(auto e:to[u])find(v,w);
}
vector<pair<int,int> >ans;
void opt(int x,int y){
ans.push_back({abs(x),abs(y)});
}
#define Assert(x) for(int _=x;!_;);
bool solve1(int s){
k=0,find(s);
// for(int i=1;i<=k;i++)debug(id[i],only[id[i]]);
// return 1;
// debug(only[s]);
// Assert(!only[id[1]].empty());
p[1]=only[id[1]].back(),only[id[1]].pop_back();
// Assert(!only[id[k]].empty());
p[k+1]=only[id[k]].back(),only[id[k]].pop_back();
// debug(ary(id,1,k),ary(p,1,k+1));
vector<int>t{1};
for(int i=1;i<=k;i++)if(out[id[i]]==2)t.push_back(i);
// debug("t",t);
for(int len=t.size(),i=1;i<len;i++){
int l=t[i-1],r=t[i],id=-1;
for(int i=l+1;i<r;i++)if(in[::id[i]]==2)id=i;
if(ept.empty())return 0;
int x=ept.back();
ept.pop_back();
opt(p[r],x);
if(~id){
for(int i=l;i<id;i++)opt(p[i+1],p[i]);
for(int i=r-1;i>id;i--)opt(p[i],p[i+1]);
int x=p[id],y=p[id+1];
opt(x,y),ept.push_back(x);
}else{
for(int i=r-1;i>=l;i--)opt(p[i],p[i+1]);
ept.push_back(p[l]);
}
p[r]=abs(x);
// debug(l,r,ans);
}
int l=t.back(),r=k+1,id=-1;
for(int i=l+1;i<r;i++)if(in[i]==2)id=i;
if(~id){
for(int i=l;i<id;i++)opt(p[i+1],p[i]);
for(int i=r-1;i>id;i--)opt(p[i],p[i+1]);
int x=p[id],y=p[id+1];
opt(x,y),ept.push_back(x);
}else{
if(p[k]>0){
for(int i=l;i<r;i++)opt(p[i+1],p[i]);
ept.push_back(p[r]);
}else{
for(int i=r;i>l;i--)opt(p[i-1],p[i]);
ept.push_back(p[l]);
}
}
return 1;
}
bool solve2(int s){
// debug("ok1");
int x=-1,y=-1;
for(auto e:to[s])if(w>0)x=v,y=w;
// Assert(find(to[s].begin(),to[s].end(),make_pair(x,y))!=to[s].end());
// Assert(find(to[x].begin(),to[x].end(),make_pair(s,-y))!=to[x].end());
to[s].erase(find(to[s].begin(),to[s].end(),make_pair(x,y)));
to[x].erase(find(to[x].begin(),to[x].end(),make_pair(s,-y)));
out[s]--,in[x]--;
// debug(s,x,to[s],to[x]);
if(ept.empty())return 0;
int z=ept.back();
ept.pop_back();
opt(y,z);
only[s].push_back(z);
only[x].push_back(y);
return solve1(s);
}
#undef v
#undef w
bool get(){
for(int i=1,x,u,v;i<=m;i++){
scanf("%d",&x);
if(!x)ept.push_back(i);
else if(x==1)scanf("%d",&u),only[u].push_back(i);
else if(scanf("%d%d",&u,&v),u!=v)addedge(v,u,i);
else ok.push_back(u);
}
// for(int i=1;i<=n;i++)debug("to",i,to[i]);
fill(vis+1,vis+1+n,0);
for(int x:ok)vis[x]=1;
vector<int>t[3];
for(int i=1;i<=n;i++){
if(!vis[i]&&to[i].size()<=1){
if(judge(i))t[1].push_back(i);
else t[0].push_back(i);
}
}
// debug(ary(t,0,2));
for(int i=1;i<=n;i++){
if(!vis[i]&&out[i]==2)t[2].push_back(i),cover(i);
}
for(int i=1;i<=n;i++){
if(!vis[i])t[2].push_back(i),cover(i);
}
// debug(ary(t,0,2));
fill(vis+1,vis+1+n,0);
// debug("ok1");
for(int i:t[0]){
if(!solve1(i))return 0;
}
for(int i:t[1]){
if(!solve1(i))return 0;
}
for(int i:t[2]){
if(!solve2(i))return 0;
}
printf("%d\n",(int)ans.size());
for(auto x:ans)printf("%d %d\n",x.first,x.second);
return 1;
}
void clr(){
ept.clear(),ans.clear(),ok.clear();
for(int i=1;i<=n;i++){
only[i].clear(),to[i].clear();
in[i]=out[i]=0;
}
}
int main(){
for(;~scanf("%d%d",&n,&m);clr())if(!get())puts("-1");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16084kb
input:
2 3 2 1 2 2 1 2 0 1 1 2 1 1 3 4 2 1 3 2 2 3 1 1 1 2
output:
3 2 3 1 3 2 1 0 -1
result:
ok 3 cases passed. max #moves/#balls = 1.500000000
Test #2:
score: 0
Accepted
time: 0ms
memory: 16120kb
input:
1 2 1 1 1 1 1 3 1 1 0 1 1 1 4 1 1 1 1 0 0 1 1 2 1 1 1 2 2 1 1 0 1 3 0 0 2 1 1
output:
1 1 2 1 1 3 1 1 2 0 0 0
result:
ok 6 cases passed. max #moves/#balls = 1.000000000
Test #3:
score: 0
Accepted
time: 2ms
memory: 16116kb
input:
2 4 1 1 1 2 1 2 1 1 2 5 1 1 1 2 0 1 1 1 2 2 6 0 1 1 0 1 1 1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3 1 6 2 5 3 4 3 5 6 1 3 4 7 3 6 7 2 5 4 8 3 1 2 3 6 4 5 3 1 3 5 6 2 7 3 3 7 2 4 6 8 3 1 5 4 6 2 3 3 4 7 1 5 2 3 3 1 6 5 7 4 8 3 4 6 ...
result:
wrong answer src.top() != dst.top() [Case 93, Move 3]