QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#866006 | #9449. New School Term | niucard | WA | 1ms | 3968kb | C++20 | 2.2kb | 2025-01-22 10:32:35 | 2025-01-22 10:32:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353,de=499122177;
int n,m,fa[5010],sum1[5010],sum2[5010],f[5010],g[5010],co[5010];
bool v[5010];
pair<int,int> a[1000010];
vector<int> ve[5010],s;
int get(int x)
{
if(x==fa[x])
return x;
return fa[x]=get(fa[x]);
}
void add(int x,int y)
{
memset(g,0,sizeof g);
for(int i=min(x,y);i<=n;i++)
{
if(i-x>=0)
g[i]=(g[i]+f[i-x])%mod;
if(i-y>=0)
g[i]=(g[i]+f[i-y])%mod;
}
for(int i=0;i<=n;i++)
f[i]=g[i];
}
void del(int x,int y)
{
if(x>y)
swap(x,y);
for(int i=0;i<=n;i++)
{
if(x==y)
g[i]=1LL*f[i+x]*de%mod;
else if(i+x-y>=0)
g[i]=(f[i+x]-g[i+x-y]+mod)%mod;
else
g[i]=f[i+x];
}
for(int i=0;i<=n;i++)
f[i]=g[i];
}
void merge(int x,int y,int xx,int yy)
{
del(sum1[x],sum2[x]),del(sum1[y],sum2[y]);
if(co[xx]==co[yy])
{
for(auto z:ve[y])
co[z]^=1;
sum1[x]+=sum2[y],sum2[x]+=sum1[y];
add(sum1[x],sum2[x]);
if(f[n/2])
{
for(auto z:ve[y])
ve[x].push_back(z);
fa[y]=x;
}
else
{
del(sum1[x],sum2[x]);
sum1[x]-=sum2[y],sum2[x]-=sum1[y];
sum1[x]+=sum1[y],sum2[x]+=sum2[y];
add(sum1[x],sum2[x]);
for(auto z:ve[y])
{
co[z]^=1;
ve[x].push_back(z);
}
fa[y]=x;
}
}
else
{
sum1[x]+=sum1[y],sum2[x]+=sum2[y];
add(sum1[x],sum2[x]);
if(f[n/2])
{
for(auto z:ve[y])
ve[x].push_back(z);
fa[y]=x;
}
else
{
del(sum1[x],sum2[x]);
sum1[x]-=sum1[y],sum2[x]-=sum2[y];
sum1[x]+=sum2[y],sum2[x]+=sum1[y];
add(sum1[x],sum2[x]);
for(auto z:ve[y])
{
co[z]^=1;
ve[x].push_back(z);
}
fa[y]=x;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
n=2*n;
f[0]=1;
for(int i=1;i<=n;i++)
{
fa[i]=i,sum1[i]=1;
add(sum1[i],sum2[i]);
ve[i].push_back(i);
}
for(int i=1;i<=m;i++)
scanf("%d%d",&a[i].first,&a[i].second);
for(int i=m;i;i--)
{
int x=get(a[i].first),y=get(a[i].second);
if(x==y)
continue;
merge(x,y,a[i].first,a[i].second);
}
for(int i=1;i<=n;i++)
{
if(get(i)==i)
s.push_back(i);
}
for(int i=0;i<s.size();i++)
{
if(i+1<s.size())
merge(i+1,i,co[i+1],co[i]);
}
for(int i=1;i<=n;i++)
printf("%d",co[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3968kb
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: 3968kb
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: -100
Wrong Answer
time: 0ms
memory: 3840kb
input:
1 0
output:
00
result:
wrong answer The number of 0s must be equal to that of 1s.