QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#120258 | #4635. Graph Operation | nowhk | RE | 0ms | 0kb | C++20 | 4.9kb | 2023-07-06 15:57:03 | 2023-07-06 15:57:06 |
Judging History
answer
#include<bits/stdc++.h>
namespace ifzw{
#define ll long long
#define dd double
#define ull unsigned ll
#define LL __int128
#define siz(A) ((int)A.size())
using namespace std;
char gc(){static char buf[1<<16],*s,*t;if(s==t){t=(s=buf)+fread(buf,1,1<<16,stdin);if(s==t)return EOF;}return *s++;}
#define getchar gc
ll read()
{
char c;
ll w=1;
while((c=getchar())>'9'||c<'0')if(c=='-')w=-1;
ll ans=c-'0';
while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0';
return ans*w;
}
void pc(char c,int op)
{
static char buf[1<<16],*s=buf,*t=(buf+(1<<16));
(op||((*s++=c)&&(s==t)))&&(fwrite(buf,1,s-buf,stdout),s=buf);
}
void wt(int x)
{
if(x>9)wt(x/10);
pc('0'+x%10,0);
}
void wts(int x,char op)
{
if(x<0)pc('-',0),x=-x;
wt(x),pc(op,0);
}
char ST;
int n,m;
namespace bf
{
map<array<int,10>,array<int,4> >mp;
array<int,10>A,B;
int id[10][10];
int&get(array<int,10>&C,int x,int y)
{
if(x>y)swap(x,y);
return C[id[x][y]];
}
void solve()
{
int tt=0;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
id[i][j]=tt++;
for(int i=1;i<=m;i++)
{
int a=read(),b=read();
get(A,a,b)=1;
}
for(int i=1;i<=m;i++)
{
int a=read(),b=read();
get(B,a,b)=1;
}
auto D=A;
queue<array<int,10>>q;
q.push(A);
while(!q.empty())
{
A=q.front();q.pop();
for(int x=1;x<=n;x++)
{
for(int y=1;y<=n;y++)
{
for(int a=1;a<=n;a++)
{
for(int b=1;b<=n;b++)
{
if(x==y||x==a||x==b||y==a||y==b||a==b)continue;
if(get(A,x,y)&&get(A,a,b)&&!get(A,x,a)&&!get(A,y,b))
{
get(A,x,y)^=1;
get(A,a,b)^=1;
get(A,x,a)^=1;
get(A,y,b)^=1;
if(!mp.count(A))q.push(A),mp[A]={x,y,a,b};
get(A,x,y)^=1;
get(A,a,b)^=1;
get(A,x,a)^=1;
get(A,y,b)^=1;
}
}
}
}
}
}
//~ cerr<<"!!\n";
//~ for(int i=0;i<siz(A);i++)cerr<<D[i]<<" ";
//~ puts("");
//~ for(int i=0;i<siz(A);i++)cerr<<B[i]<<" ";
//~ puts("");
if(!mp.count(B))
{
puts("-1");
exit(0);
}
else
{
vector<array<int,4> >ans;
A=D;
while(B!=A)
{
auto to=mp[B];
ans.push_back(to);
int x=to[0],y=to[1],a=to[2],b=to[3];
//~ cerr<<get(B,x,y)<<" "<<get(B,a,b)<<" "<<get(B,x,a)<<" "<<get(B,y,b)<<"#\n";
get(B,x,y)^=1;
get(B,a,b)^=1;
get(B,x,a)^=1;
get(B,y,b)^=1;
}
//~ cerr<<"!!\n";
//~ for(int i=0;i<siz(A);i++)cerr<<D[i]<<" ";
//~ puts("");
//~ for(int i=0;i<siz(A);i++)cerr<<B[i]<<" ";
//~ puts("");
//~ assert(A==B);
reverse(ans.begin(),ans.end());
cout<<siz(ans)<<"\n";
for(auto it:ans)
{
for(auto i:it)cout<<i<<" ";
puts("");
}
}
}
}
namespace zj
{
random_device R;
mt19937 G(R());
int rd(int l,int r){return uniform_int_distribution<int>(l,r)(G);};
const int xx=1005;
bitset<xx>fir[xx],sec[xx];
//~ bitset<xx>xf[xx],xs[xx];
vector<array<int,4> >ans;
int d1[xx],d2[xx];
vector<int>rem;
void app(int x,int y,int a,int b)
{
assert(fir[x][y]&&fir[a][b]&&!fir[x][a]&&!fir[y][b]);
fir[x][y]=0;
fir[a][b]=0;
fir[x][a]=1;
fir[y][b]=1;
fir[y][x]=0;
fir[b][a]=0;
fir[a][x]=1;
fir[b][y]=1;
ans.push_back({x,y,a,b});
}
bool ck(int x)
{
if((fir[x]^sec[x]).any())return 1;
return 0;
}
void solve()
{
for(int i=1;i<=m;i++)
{
int a=read(),b=read();
fir[a][b]=fir[b][a]=1;
d1[a]++,d1[b]++;
}
for(int i=1;i<=m;i++)
{
int a=read(),b=read();
sec[a][b]=sec[b][a]=1;
d2[a]++,d2[b]++;
}
int cr=1;
for(int i=1;i<=n;i++)
if(d1[i]!=d2[i])cr=0;
if(!cr)
{
puts("-1");
exit(0);
}
for(int i=1;i<=n;i++)
if(ck(i))rem.push_back(i);
while(siz(rem))
{
int ty=rem[rd(0,siz(rem)-1)];
vector<int>L,R;
for(int i=1;i<=n;i++)
{
if(fir[ty][i]&&!sec[ty][i])L.push_back(i);
if(!fir[ty][i]&&sec[ty][i])R.push_back(i);
}
int turn=100;
while(siz(L)&&(turn--))
{
int to=rd(1,n),A=L[rd(0,siz(L)-1)],B=R[rd(0,siz(R)-1)];
if(to==A||to==B||to==ty)
{
++turn;
continue;
}
if(!fir[to][A]&&fir[to][B])
{
//keyi zuo
if(sec[to][A]||!sec[to][B])
{
int F=ck(to);
app(ty,A,B,to);
L.erase(find(L.begin(),L.end(),A));
R.erase(find(R.begin(),R.end(),B));
int S=ck(to);
if(F&&!S)rem.erase(find(rem.begin(),rem.end(),to));
if(!F&&S)rem.push_back(to);
}
}
}
if(!ck(ty))rem.erase(find(rem.begin(),rem.end(),ty));
}
cout<<siz(ans)<<"\n";
for(auto its:ans)
{
for(auto it:its)cout<<it<<" ";
puts("");
}
}
}
char ED;
int main(){
cerr<<abs(&ST-&ED)/1024.0/1024<<"%\n";
freopen("graph.in","r",stdin);
freopen("graph.out","w",stdout);
n=read(),m=read();
//~ if(n<=5)
//~ {
//~ bf::solve();
//~ return 0;
//~ }
zj::solve();
pc('1',1);
return 0;
}
}signed main(){return ifzw::main();}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
4 2 1 2 3 4 1 3 2 4