QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#613881 | #9449. New School Term | ucup-team4479# | WA | 1ms | 8004kb | C++23 | 4.1kb | 2024-10-05 14:58:33 | 2024-10-05 15:02:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=5005,M=1000005;
int n,m;
int a[M],b[M];
int fa[N*4];
int siz[N*4];
int getf(int v)
{
if(v==fa[v]) return v;
else return fa[v]=getf(fa[v]);
}
void merge(int u,int v)
{
int fu=getf(u),fv=getf(v);
if(fu!=fv) fa[fu]=fv;
return;
}
vector<int>G[N*2];
int col[N*2];
int cnt[2];
vector<int>pos;
void dfs(int u,int color)
{
pos.emplace_back(u);
col[u]=color;
cnt[color]++;
for(int v:G[u])
if(col[v]==-1) dfs(v,color^1);
return;
}
bitset<N>f[N*2];
int tot=0;
int rt[N*2],c[N*2][2];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
// cin>>n>>m;
mt19937 rnd(time(nullptr));
cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>a[i]>>b[i];
for(int i=1;i<=n*4;i++)
fa[i]=i;
vector<pair<int,int>>val;
for(int i=1;i<=n*2;i++)
val.emplace_back(0,1);
int sum=0;
for(int i=m;i>=1;i--)
{
int u=a[i],v=b[i];
if(getf(u)==getf(v)||getf(u+n*2)==getf(v+n*2)) continue;
if(getf(u)==getf(v+n*2)||getf(u+n*2)==getf(v)) continue;
// cerr<<"try"<<u<<" "<<v<<"\n";
cnt[0]=cnt[1]=0;
pos.clear();
dfs(u,0);
for(int u:pos)
col[u]=-1;
int cu[2]={cnt[0],cnt[1]};
if(cnt[0]>cnt[1]) swap(cnt[0],cnt[1]);
sum-=cnt[0];
val.erase(lower_bound(val.begin(),val.end(),make_pair(cnt[0],cnt[1])));
cnt[0]=cnt[1]=0;
pos.clear();
dfs(v,0);
for(int u:pos)
col[u]=-1;
int cv[2]={cnt[0],cnt[1]};
if(cnt[0]>cnt[1]) swap(cnt[0],cnt[1]);
sum-=cnt[0];
val.erase(lower_bound(val.begin(),val.end(),make_pair(cnt[0],cnt[1])));
cnt[0]=cu[0]+cv[1],cnt[1]=cu[1]+cv[0];
if(cnt[0]>cnt[1]) swap(cnt[0],cnt[1]);
sum+=cnt[0];
val.insert(lower_bound(val.begin(),val.end(),make_pair(cnt[0],cnt[1])),make_pair(cnt[0],cnt[1]));
// cerr<<"sum"<<sum<<'\n';
// cerr<<"ins"<<cu[0]<<" "<<cv[1]<<"\n";
// for(auto [c0,c1]:val)
// cerr<<"find"<<c0<<" "<<c1<<"\n";
if(sum>n)
{
val.erase(lower_bound(val.begin(),val.end(),make_pair(cnt[0],cnt[1])));
sum-=cnt[0];
val.insert(lower_bound(val.begin(),val.end(),make_pair(cu[0],cu[1])),make_pair(cu[0],cu[1]));
sum+=cu[0];
val.insert(lower_bound(val.begin(),val.end(),make_pair(cv[0],cv[1])),make_pair(cv[0],cv[1]));
sum+=cv[0];
continue;
}
f[0]=1;
int ret=n-sum;
bitset<N>f;
f[0]=1;
for(auto [c0,c1]:val)
{
f|=f<<(c1-c0);
if(f[ret]) break;
}
if(!f[ret])
{
val.erase(lower_bound(val.begin(),val.end(),make_pair(cnt[0],cnt[1])));
sum-=cnt[0];
val.insert(lower_bound(val.begin(),val.end(),make_pair(cu[0],cu[1])),make_pair(cu[0],cu[1]));
sum+=cu[0];
val.insert(lower_bound(val.begin(),val.end(),make_pair(cv[0],cv[1])),make_pair(cv[0],cv[1]));
sum+=cv[0];
continue;
}
// cerr<<"merge"<<u<<" "<<v<<"\n";
G[u].emplace_back(v);
G[v].emplace_back(u);
merge(u,v+n*2);
merge(u+n*2,v);
}
fill(col+1,col+n*2+1,-1);
f[0][0]=1;
tot=0;
for(int u=1;u<=n*2;u++)
{
if(col[u]==-1)
{
cnt[0]=cnt[1]=0;
dfs(u,0);
rt[++tot]=u;
c[tot][0]=cnt[0],c[tot][1]=cnt[1];
f[tot]=(f[tot-1]<<cnt[0])|(f[tot-1]<<cnt[1]);
}
}
fill(col+1,col+n*2+1,-1);
int ret=n;
for(int i=tot;i>=1;i--)
{
int u=rt[i];
if(ret>=c[i][0]&&f[i-1][ret-c[i][0]])
{
ret-=c[i][0];
dfs(u,0);
}
else
{
ret-=c[i][1];
dfs(u,1);
}
}
for(int u=1;u<=n*2;u++)
cout<<col[u];
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 7772kb
input:
2 4 1 3 2 4 1 4 1 2
output:
0101
result:
ok Output is valid. OK
Test #2:
score: 0
Accepted
time: 0ms
memory: 7764kb
input:
3 7 2 5 1 3 4 6 2 6 4 5 2 4 5 6
output:
001101
result:
ok Output is valid. OK
Test #3:
score: 0
Accepted
time: 1ms
memory: 5724kb
input:
1 0
output:
10
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 0ms
memory: 7784kb
input:
1 1 1 2
output:
01
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 1ms
memory: 7784kb
input:
2 3 2 4 3 4 1 2
output:
0110
result:
ok Output is valid. OK
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 8004kb
input:
3 8 4 6 3 5 1 4 2 4 1 6 1 2 3 4 4 5
output:
100100
result:
wrong answer The number of 0s must be equal to that of 1s.