QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#625346 | #9449. New School Term | as_lyr | WA | 2ms | 11800kb | C++14 | 2.3kb | 2024-10-09 18:48:57 | 2024-10-09 18:48:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=11000,M=1100000;
int n,m;
int u[M],v[M];
pair <int,bool> f[N];
vector <int> e[N][2];
inline pair <int,bool> find(int x){
if(f[x].first==x)
return f[x];
pair <int,bool> pr=find(f[x].first);
return f[x]=make_pair(pr.first,pr.second^f[x].second);
}
bitset <N> bs[N];
inline bool check(int x,int y,bool op){
int sum=0;
vector <int> p;
for(int i=1;i<=n;i++){
if(f[i].first!=i)
continue;
if(i==x)
continue;
int siz[2]={(int)e[i][0].size(),(int)e[i][1].size()};
if(i==y){
siz[0]+=e[x][op^1].size();
siz[1]+=e[x][op].size();
}
sum+=min(siz[0],siz[1]);
p.push_back(siz[0]+siz[1]-min(siz[0],siz[1]));
}
int tot=0;
bs[0]=0;
bs[0][sum]=1;
sort(p.begin(),p.end());
for(int l=0,r=0;l<(int)p.size();l=r+1){
r=l;
while(r<(int)p.size()-1&&p[r+1]==p[l])
r++;
int val=p[l],cnt=r-l+1;
for(int k=0;cnt>(1<<k);k++){
cnt-=1<<k;
tot++;
bs[tot]=bs[tot-1]|(bs[tot-1]<<(val*(1<<k)));
}
tot++;
bs[tot]=bs[tot-1]|(bs[tot-1]<<(val*cnt));
}
return op^bs[tot][n/2];
}
inline void merge(int x,int y){
pair <int,int> prx=find(x),pry=find(y);
x=prx.first,y=pry.first;
if(x==y)
return ;
if(e[x][0].size()+e[x][1].size()>e[y][0].size()+e[y][1].size())
swap(x,y);
bool op=check(x,y,prx.second^pry.second);
f[x]=make_pair(y,op);
for(int o=0;o<2;o++)
for(int a:e[x][o])
e[y][o^op].push_back(a);
}
bool ans[N];
int dp[N][N];
int main(){
scanf("%d%d",&n,&m);
n*=2;
for(int i=m;i;i--)
scanf("%d%d",&u[i],&v[i]);
for(int i=1;i<=n;i++){
f[i]=make_pair(i,0);
e[i][0].push_back(i);
}
for(int i=1;i<=m;i++)
merge(u[i],v[i]);
int tot=0;
dp[0][0]=1;
for(int i=1;i<=n;i++)
if(f[i].first==i){
tot++;
int siz[2]={(int)e[i][0].size(),(int)e[i][1].size()};
for(int j=siz[0];j<=n/2;j++)
dp[tot][j]|=dp[tot-1][j-siz[0]];
for(int j=siz[1];j<=n/2;j++)
dp[tot][j]|=dp[tot-1][j-siz[1]];
}
for(int i=n,x=n/2;i;i--)
if(f[i].first==i){
int siz[2]={(int)e[i][0].size(),(int)e[i][1].size()};
bool op=1;
if(x>=siz[0]&&dp[tot-1][x-siz[0]]){
op=0;
x-=siz[0];
}
else
x-=siz[1];
for(int a:e[i][op])
ans[a]=1;
tot--;
}
for(int i=1;i<=n;i++)
putchar(ans[i]+'0');
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 11800kb
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: 2ms
memory: 9880kb
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: 7800kb
input:
1 0
output:
01
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 0ms
memory: 10104kb
input:
1 1 1 2
output:
01
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 2ms
memory: 10108kb
input:
2 3 2 4 3 4 1 2
output:
1001
result:
ok Output is valid. OK
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 9812kb
input:
3 8 4 6 3 5 1 4 2 4 1 6 1 2 3 4 4 5
output:
011010
result:
wrong answer The division is not minimized.