QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#613652#9449. New School Termucup-team4479#RE 2ms9868kbC++232.4kb2024-10-05 14:25:072024-10-05 14:25:25

Judging History

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

  • [2024-10-05 14:25:25]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:9868kb
  • [2024-10-05 14:25:07]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=5005,M=1000005;
int n,m;
int a[M],b[M];
int fa[N*8];
int siz[N*8];
int getf(int v)
{
    if(v==fa[v]) return v;
    else return fa[v]=getf(fa[v]);
}
void merge(int u,int v)
{
    int fu=getf(u),fv=getf(v);
    if(fu!=fv) fa[fu]=fv;
    return;
}
vector<int>G[N*2];
int col[N*2];
int cnt[2];
void dfs(int u,int color)
{
    col[u]=color;
    cnt[color]++;
    for(int v:G[u])
        if(col[v]==-1) dfs(v,color^1);
    return;
}
bitset<N>f[N*2];
int tot=0;
int rt[N*2],c[N*2][2];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=m;i++)
        cin>>a[i]>>b[i];
    for(int i=1;i<=n*4;i++)
        fa[i]=i;
    for(int i=m;i>=1;i--)
    {
        int u=a[i],v=b[i];
        if(getf(u)==getf(v)||getf(u+n*2)==getf(v+n*2)) continue;
        if(getf(u)==getf(v+n*2)||getf(u+n*2)==getf(v)) continue;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
        fill(col+1,col+n*2+1,-1);
        bool flag=true;
        bitset<N>f;
        f[0]=1;
        for(int u=1;u<=n*2;u++)
        {
            if(col[u]==-1)
            {
                cnt[0]=cnt[1]=0;
                dfs(u,0);
                if(cnt[0]>cnt[1]) swap(cnt[0],cnt[1]);
                f=(f<<cnt[0])|(f<<cnt[1]);
            }
        }
        flag=f[n];
        if(!flag)
        {
            G[u].pop_back();
            G[v].pop_back();
            continue;
        }
        // cerr<<"merge"<<u<<" "<<v<<"\n";
        merge(u,v+n*2);
        merge(u+n*2,v);
    }
    // cerr<<"ed\n";
    fill(col+1,col+n*2+1,-1);
    f[0][0]=1;
    for(int u=1;u<=n*2;u++)
    {
        if(col[u]==-1)
        {
            cnt[0]=cnt[1]=0;
            dfs(u,0);
            rt[++tot]=u;
            // cerr<<"find"<<u<<" "<<cnt[0]<<" "<<cnt[1]<<"\n";
            c[tot][0]=cnt[0],c[tot][1]=cnt[1];
            f[tot]=(f[tot-1]<<cnt[0])|(f[tot-1]<<cnt[1]);
        }
    }
    assert(f[tot][n]);
    fill(col+1,col+n*2+1,-1);
    int ret=n;
    for(int i=tot;i>=1;i--)
    {
        int u=rt[i];
        if(f[i-1][ret-c[i][0]])
        {
            ret-=c[i][0];
            dfs(u,0);
        }
        else
        {
            ret-=c[i][1];
            dfs(u,1);
        }
    }
    for(int u=1;u<=n*2;u++)
        cout<<col[u];
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9864kb

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

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
Runtime Error

input:

1 0

output:


result: