QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#637752 | #9449. New School Term | ucup-team902# | WA | 2ms | 12088kb | C++17 | 2.7kb | 2024-10-13 14:00:18 | 2024-10-13 14:00:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=5e3,M=1e6;
const int mod=998244353;
int add(int x,int y){ return (x+=y)>=mod?x-mod:x; }
int sub(int x,int y){ return (x-=y)<0?x+mod:x; }
int n,m;
int a[M+5],b[M+5];
bool ok[N+5][N*2+5];
int f[N*2+5],sz[N*2+5],sid[N*2+5];
int dp[N*2+5],ms;
void insert(int x){
ms+=x;
x*=2;
for(int j=ms;j>=x;j--)
dp[j]=add(dp[j],dp[j-x]);
}
void del(int x){
if(x==0){
for(int j=0;j<=ms;j++)
dp[j]=1ll*dp[j]*(mod+1)/2%mod;
}
else{
x*=2;
for(int j=x;j<=ms;j++)
dp[j]=sub(dp[j],dp[j-x]);
}
ms-=x/2;
}
int find(int x){ return x==f[x]?x:f[x]=find(f[x]); }
void check(int u,int v){
int fu=find(u),fv=find(v);
int su=sz[fu],sv=sz[fv],tp=sid[u]==sid[v];
if(tp) sv=-sv;
if(ok[su][sv+N]||(fu==fv&&tp)) return;
static int pre[N*2+5];
memcpy(pre,dp,sizeof dp);
del(su); del(abs(sv));
// for(int i=0;i<=ms;i++)
// printf("%d ",dp[i]);
// puts("");
insert(abs(su+sv));
// printf("%d %d %d\n",su,sv,ms);
if(dp[ms]){
// printf("%d %d\n",u,v);
if(su+sv>0){
for(int i=1;i<=n*2;i++)
if(find(i)==fv) sid[i]^=tp;
f[fv]=fu; sz[fu]=su+sv;
}
else{
for(int i=1;i<=n*2;i++)
if(find(i)==fu) sid[i]^=tp;
f[fu]=fv; sz[fv]=-(su+sv);
}
return;
}
else{
memcpy(dp,pre,sizeof dp);
ok[su][sv+N]=1;
}
}
void work(){
static int g[N*2+5],pre[N*2+5][N*2+5];
g[0]=1;
for(int i=1;i<=n*2;i++){
for(int j=0;j<=ms;j++)
if(g[j]) pre[i][j]=j;
if(find(i)==i){
int x=sz[i]*2;
for(int j=ms;j>=x;j--)
if(!g[j]&&g[j-x]){
g[j]=1; pre[i][j]=j-x;
}
}
}
static char ans[N*2+5];
for(int i=1;i<=n*2;i++)
ans[i]='0';
for(int i=n*2,j=ms;i;i--){
if(find(i)==i){
if(pre[i][j]!=j){
for(int u=1;u<=n*2;u++)
if(find(u)==i&&sid[u])
ans[u]='1';
}
else{
for(int u=1;u<=n*2;u++)
if(find(u)==i&&!sid[u])
ans[u]='1';
}
j=pre[i][j];
}
}
printf("%s\n",ans+1);
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d %d",&a[i],&b[i]);
dp[0]=1;
for(int i=1;i<=n*2;i++){
f[i]=i; sz[i]=1; sid[i]=0;
insert(1);
}
for(int i=m;i;i--)
check(a[i],b[i]);
work();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 10148kb
input:
2 4 1 3 2 4 1 4 1 2
output:
1010
result:
ok Output is valid. OK
Test #2:
score: 0
Accepted
time: 1ms
memory: 9992kb
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: 7972kb
input:
1 0
output:
01
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 1ms
memory: 12088kb
input:
1 1 1 2
output:
01
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 0ms
memory: 10064kb
input:
2 3 2 4 3 4 1 2
output:
1001
result:
ok Output is valid. OK
Test #6:
score: 0
Accepted
time: 0ms
memory: 10084kb
input:
3 8 4 6 3 5 1 4 2 4 1 6 1 2 3 4 4 5
output:
101010
result:
ok Output is valid. OK
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 10092kb
input:
4 9 4 7 3 8 1 5 2 7 2 8 6 8 7 8 1 4 1 6
output:
01110110
result:
wrong answer The number of 0s must be equal to that of 1s.