QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#627814#9449. New School Termpengpeng_fudan#WA 0ms3664kbC++232.9kb2024-10-10 17:15:472024-10-10 17:15:52

Judging History

你现在查看的是最新测评结果

  • [2024-10-10 17:15:52]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3664kb
  • [2024-10-10 17:15:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int fa[20010],siz[20010];
int sq[20010];
int get(int x){
    return fa[x]==x?x:get(fa[x]);
}
pair<int,int> pre_op[2];
void merge(int x,int y){
    x=get(x),y=get(y);
    if(sq[x]>sq[y])   swap(x,y);
    pre_op[0]=pre_op[1];
    pre_op[1]={x,y};
    fa[x]=y;siz[y]+=siz[x];sq[y]+=sq[x];   
}
void del(){
    fa[pre_op[1].first]=pre_op[1].first;
    siz[pre_op[1].second]-=siz[pre_op[1].first];
    sq[pre_op[1].second]-=sq[pre_op[1].first];
    fa[pre_op[0].first]=pre_op[0].first;
    siz[pre_op[0].second]-=siz[pre_op[0].first];
    sq[pre_op[0].second]-=sq[pre_op[0].first];
}
void solve(){
    int n,m;
    cin>>n>>m;n*=2;
    for(int i=1;i<=2*n;i++) {
        fa[i]=i,siz[i]=(i<=n?1:-1);
        sq[i]=1;
    } 
    vector<int> a(m+1),b(m+1);
    for(int i=1;i<=m;i++){
        cin>>a[i]>>b[i];
    }
    vector<int> ans(n+1);
    auto check=[&]()->bool {
        bitset<20010> bt;
        const int det=10000;
        bt[det]=1;
        for(int i=1;i<=n;i++){
            if(get(i)==i){
                int sq=abs(siz[i]);
                bt=((bt<<sq)|(bt>>sq));
            }
        }
        return bt[det];
    };
    for(int i=m;i>=1;i--){
        // for(int i=1;i<=2*n;i++) cerr<<fa[i]<<' ';
        // cerr<<'\n';
        if(get(a[i])==get(b[i]+n)||get(a[i])==get(b[i]));
        else{
            // cerr<<a[i]<<' '<<b[i]+n<<'\n';
            merge(a[i],b[i]+n);          
            merge(a[i]+n,b[i]); 
            if(get(a[i])==get(a[i]+n)||get(b[i])==get(b[i]+n)){
                del();
                continue;
            }         
            if(check());
            else del();
        }
    }
    vector<pair<int,int>> res;
    int sum=0;
    for(int i=1;i<=n;i++){
        if(get(i)==i){
            int sq=siz[i];
            if(sq<0)    {res.push_back({2*sq,i});sum-=sq;}
            else if(sq>0) {res.push_back({2*sq,i});ans[i]=1;sum+=sq;}    
        }
    }
    // cerr<<'\n';
    // cerr<<sum<<'\n';
    // for(auto [x,y]:res) cerr<<x<<' '<<y<<'\n';
    vector<pair<int,int>> beg(sum+1);
    beg[0]={1,-1};
    for(int i=0;i<(int)res.size();i++){
        for(int j=sum;j>=res[i].first;j--){
            if(beg[j-res[i].first].first==1){
                beg[j]={1,i};
            }
        }
    }
    assert(beg[sum].first);
    int p=sum;
    while(p){
        ans[res[beg[p].second].second]^=1;
        p-=res[beg[p].second].first;
    }
    // cerr<<"?";
    for(int i=1;i<=n;i++){
        if(get(i)<=n){
            ans[i]=ans[get(i)];
        }
        else{
            ans[i]=(1^ans[get(i)-n]);
        }
    }
    for(int i=1;i<=n;i++)   cout<<ans[i];
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int _ =1;
    // cin>>_;
    while(_--)  solve();

    return 0;
}


/*
2 4
1 3
2 4
1 4
1 2
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

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: 0ms
memory: 3664kb

input:

3 7
2 5
1 3
4 6
2 6
4 5
2 4
5 6

output:

110010

result:

ok Output is valid. OK

Test #3:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

1 0

output:

10

result:

ok Output is valid. OK

Test #4:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

1 1
1 2

output:

10

result:

ok Output is valid. OK

Test #5:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

2 3
2 4
3 4
1 2

output:

0110

result:

ok Output is valid. OK

Test #6:

score: 0
Accepted
time: 0ms
memory: 3560kb

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: 0ms
memory: 3664kb

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: 0ms
memory: 3552kb

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:

0010111010

result:

ok Output is valid. OK

Test #9:

score: -100
Wrong Answer
time: 0ms
memory: 3512kb

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.