QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#614005#9449. New School Termucup-team5071#WA 0ms3836kbC++201.4kb2024-10-05 15:18:542024-10-05 15:20:49

Judging History

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

  • [2024-10-05 15:20:49]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3836kb
  • [2024-10-05 15:18:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn=5010*2;
int f[maxn];
int siz[maxn][2];
int n,m;
int getf(int x){
    if(x<0)return -getf(-x);
    if(x==f[x])return x;
    else return f[x]=getf(f[x]);
}
bool merge(int x,int y){//如果是x!=y将y取反(x>0 y>0)
    if(getf(x)==-getf(y))return false;
    if(getf(x)==getf(y))return true;
    x=getf(x),y=getf(y);
    if(x<0)x=-x,y=-y;
    if(siz[x][0]+(y>0?siz[y][0]:siz[-y][1])>n)return false;
    if(siz[x][1]+(y>0?siz[y][1]:siz[-y][0])>n)return false;
    f[x]=y;
    if(y>0)siz[y][0]+=siz[x][0],siz[y][1]+=siz[x][1];
    else siz[-y][0]+=siz[x][1],siz[-y][1]+=siz[x][0];
    return true;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n*2;i++)f[i]=i,siz[i][0]=1;
    vector<int> ans(n*2+1,0);
    vector<pair<int,int>> q(m);
    for(int i=0;i<m;i++){
        cin>>q[i].first>>q[i].second;
    }
    reverse(q.begin(),q.end());
    for(auto [x,y]:q){
        bool flag=merge(x,-y);

        if(!flag)merge(x,y);
        // cout<<"x="<<x<<" y="<<y<<" merge="<<flag<<endl;
    }
    // for(int i=1;i<=n;i++)cout<<
    vector<int> vis(n*2+1,0);
    for(int i=1;i<=n*2;i++)if(!vis[i]){
        for(int j=i+1;j<=n*2;j++)if(abs(getf(i))==abs(getf(j))){
            if(getf(i)==-getf(j))ans[j]=ans[i]^1;
            else ans[j]=ans[i];
            vis[j]=1;
        }
    }
    for(int i=1;i<=n*2;i++)cout<<ans[i];
}

详细

Test #1:

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

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: 3832kb

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: 3836kb

input:

1 0

output:

00

result:

wrong answer The number of 0s must be equal to that of 1s.