QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#637795 | #9449. New School Term | ucup-team902# | RE | 1ms | 10112kb | C++17 | 2.7kb | 2024-10-13 14:06:50 | 2024-10-13 14:06:50 |
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+5],ms;
void insert(int x){
ms+=x;
for(int j=n;j>=x;j--)
dp[j]=add(dp[j],dp[j-x]);
}
void del(int x){
if(x==0){
for(int j=0;j<=n;j++)
dp[j]=1ll*dp[j]*(mod+1)/2%mod;
}
else{
for(int j=x;j<=n;j++)
dp[j]=sub(dp[j],dp[j-x]);
}
ms-=x;
}
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+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/2]){
// 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{
ms-=abs(su+sv);
ms+=su+abs(sv);
memcpy(dp,pre,sizeof dp);
ok[su][sv+N]=1;
}
}
void work(){
static int g[N+5],pre[N*2+5][N+5];
g[0]=1;
for(int i=1;i<=n*2;i++){
for(int j=0;j<=ms/2;j++)
if(g[j]) pre[i][j]=j;
if(find(i)==i){
int x=sz[i];
for(int j=ms/2;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/2;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: 1ms
memory: 10064kb
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: 10052kb
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: 5884kb
input:
1 0
output:
01
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 1ms
memory: 9988kb
input:
1 1 1 2
output:
01
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 0ms
memory: 10024kb
input:
2 3 2 4 3 4 1 2
output:
1001
result:
ok Output is valid. OK
Test #6:
score: 0
Accepted
time: 1ms
memory: 10108kb
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: 0
Accepted
time: 1ms
memory: 9996kb
input:
4 9 4 7 3 8 1 5 2 7 2 8 6 8 7 8 1 4 1 6
output:
10101001
result:
ok Output is valid. OK
Test #8:
score: 0
Accepted
time: 1ms
memory: 10060kb
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: 0
Accepted
time: 1ms
memory: 10112kb
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:
110110001001
result:
ok Output is valid. OK
Test #10:
score: -100
Runtime Error
input:
12 153 1 24 16 18 7 14 1 16 20 21 9 14 21 22 4 5 17 24 4 12 5 17 13 24 14 15 12 23 12 16 8 11 14 24 9 16 2 5 6 19 11 17 4 22 4 7 6 16 7 20 8 15 5 24 2 10 10 21 21 24 1 12 11 19 18 21 18 24 12 17 13 22 7 9 13 23 4 9 11 13 15 21 5 7 2 4 15 16 17 19 11 16 11 20 7 8 4 15 13 14 6 18 2 19 9 13 23 24 4 21 ...