QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#624768 | #9449. New School Term | jager59 | WA | 1ms | 7816kb | C++17 | 2.3kb | 2024-10-09 16:35:35 | 2024-10-09 16:35:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=10005,M=1e6+5;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x*f;
}
int n,m,a[M],b[M],fa[N],s1[N],s2[N],p[N],tot,t[N],len,pre[N],ans[N],G[N];
vector<pair<int,int>>e[N];
inline void dfs(int x){
for(auto[y,c]:e[x])ans[y]=ans[x]^c,dfs(y);
}
inline int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
bitset<5005>f,g[N];
int main(){
n=read();m=read();
for(int i = 1;i<=m;i++)a[i]=read(),b[i]=read();
for(int i = 1;i<=n*2;i++)fa[i]=i,s1[i]=1,s2[i]=0;
for(int i = m;i>=1;i--){
int x=find(a[i]),y=find(b[i]);
if(x==y)continue;
int w = 0,ggg=(G[a[i]]^G[b[i]]);
// cerr<<ggg<<endl;
tot=len=0;
for(int j = 1;j<=2*n;j++){
if(fa[j]==j && j!=x && j!=y){
w+=max(s1[j],s2[j])-min(s1[j],s2[j]);
p[++tot]=max(s1[j],s2[j])-min(s1[j],s2[j]);
}
}
if(ggg==0){
w+=max(s1[x]+s2[y],s1[y]+s2[x])-min(s1[x]+s2[y],s1[y]+s2[x]);
p[++tot]=max(s1[x]+s2[y],s1[y]+s2[x])-min(s1[x]+s2[y],s1[y]+s2[x]);
}
else{
w+=max(s1[x]+s1[y],s2[y]+s2[x])-min(s1[x]+s1[y],s2[y]+s2[x]);
p[++tot]=max(s1[x]+s1[y],s2[y]+s2[x])-min(s1[x]+s1[y],s2[y]+s2[x]);
}
sort(p+1,p+tot+1);
for(int l = 1,r;l<=tot;l=r+1){
r=l;
while(r<=tot&&p[l]==p[r])r++;
r--;
int now = r-l+1;
for(int j = 0;j<=12;j++){
if(now>=(1<<j))now-=(1<<j),t[++len]=(1<<j)*p[l];
}
if(now)t[++len]=now*p[l];
}
f.reset();
f[0]=1;
for(int j = 1;j<=len;j++)f|=(f<<t[j]);
// cerr<<"?"<<w<<"??"<<endl;
if(!(f[w/2] ^ ggg)){
s1[y]+=s1[x],s2[y]+=s2[x],e[y].push_back({x,0});
// cerr<<y<<' '<<x<<' '<<0<<endl;
for(int j = 1;j<=2*n;j++)if(fa[j]==x)fa[j]=y;
}
else {
s1[y]+=s2[x],s2[y]+=s1[x],e[y].push_back({x,1});
// cerr<<y<<' '<<x<<' ' <<1<<endl;
for(int j = 1;j<=2*n;j++)if(fa[j]==x)fa[j]=y,G[j]^=1;
}
}
g[0][0]=1;
int id = 0;
for(int i = 1;i<=2*n;i++){
if(fa[i]==i){
++id;
g[id]=(g[id]<<s1[i])|(g[id]<<s2[i]);
}
}
int now = n;
for(int i = 2*n;i>=1;i--){
if(fa[i]==i){
if(now-s1[i]>=0&&g[id-1][now-s1[i]]){
dfs(i);
now-=s1[i];
}
else {
ans[i]=1;
now-=s2[i];
dfs(i);
}
--id;
}
}
for(int i = 1;i<=2*n;i++)putchar(ans[i]^48);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 7788kb
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: 1ms
memory: 7780kb
input:
3 7 2 5 1 3 4 6 2 6 4 5 2 4 5 6
output:
100101
result:
ok Output is valid. OK
Test #3:
score: 0
Accepted
time: 0ms
memory: 5736kb
input:
1 0
output:
01
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 1ms
memory: 7728kb
input:
1 1 1 2
output:
10
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 0ms
memory: 7704kb
input:
2 3 2 4 3 4 1 2
output:
0110
result:
ok Output is valid. OK
Test #6:
score: 0
Accepted
time: 1ms
memory: 7784kb
input:
3 8 4 6 3 5 1 4 2 4 1 6 1 2 3 4 4 5
output:
010101
result:
ok Output is valid. OK
Test #7:
score: 0
Accepted
time: 1ms
memory: 7816kb
input:
4 9 4 7 3 8 1 5 2 7 2 8 6 8 7 8 1 4 1 6
output:
01010110
result:
ok Output is valid. OK
Test #8:
score: 0
Accepted
time: 1ms
memory: 7812kb
input:
5 16 3 6 9 10 2 7 1 10 1 5 2 10 3 5 5 6 3 4 2 5 4 5 3 8 4 7 6 8 1 6 7 10
output:
1101000101
result:
ok Output is valid. OK
Test #9:
score: -100
Wrong Answer
time: 1ms
memory: 7724kb
input:
6 13 4 5 2 9 3 8 4 8 4 11 10 12 3 4 3 9 5 11 2 8 5 10 5 8 1 11
output:
110111101001
result:
wrong answer The number of 0s must be equal to that of 1s.