Chri_K

网络(network)
#include<iostream>
using namespace std;
const int max_M = 5000005; 
const int max_N = 500005;
int n, m, X[max_M],Y[max_M], P[max_N], preX[max_M], preY[max_M], ans[max_M];
bool flag[max_N];
int main() 
{
    cin>>n>>m;
    cout<<"YES"<<endl;
    for (int i = 1; i <= m; i++) 
    {
        cin>>X[i]>>Y[i];
    }
    for (int i = 1; i < n; i += 2) 
    {
        P[i] = i + 1, P[i + 1] = i;
    }
    if (n%2) 
    {
        P[n] = n;
    }
    for (int i = 1; i <= m; i++) 
    {
        preX[i] = P[X[i]]; 
        preY[i] = P[Y[i]]; 
        if (P[X[i]] == Y[i]) 
        {
            continue;
        }
        if (P[X[i]] == X[i]) 
        {
            P[P[Y[i]]] = P[Y[i]]; 
            P[X[i]] = Y[i];
            P[Y[i]] = X[i];
        }
        else if (P[Y[i]] == Y[i]) 
        {
            P[P[X[i]]]=P[X[i]];
            P[X[i]] = Y[i];
            P[Y[i]] = X[i];
        }
        else 
        {
            int x = X[i];
            int y = Y[i]; 
            int u = P[X[i]]; 
            int v = P[Y[i]];
            P[x] = y;
            P[y] = x; 
            P[u] = v; 
            P[v] = u;
        }
    }
    for (int i = 1; i <= n; i++) 
    {
        if (i <= P[i]) 
        {
            flag[i] = true;
        }
    }
    for (int i = m; i>=1; i--) 
    {
        P[X[i]] = preX[i];
        P[Y[i]] = preY[i]; 
        P[P[X[i]]] = X[i];
        P[P[Y[i]]] = Y[i];
        if (P[X[i]] == Y[i]) 
        {
            if (flag[X[i]]) 
            {
                ans[i] = 0;
            }
            else 
            {
                ans[i] = 1;
            }
        }
        else if (P[X[i]] == X[i]) 
        {
            if (flag[X[i]]) 
            {
                ans[i] = 0;
            }
            else 
            {
                flag[X[i]] = true;
                flag[Y[i]] = false;
                ans[i] = 1;
            }
        }
        else if (P[Y[i]] == Y[i]) 
        {
            if (flag[Y[i]]) 
            {
                ans[i] = 1;
            }
            else 
            {
                flag[Y[i]] = true;
                flag[X[i]] = false;
                ans[i] = 0;
            }
        }
        else 
        {
            int x = X[i];
            int y = Y[i];
            int u = P[X[i]]; 
            int v = P[Y[i]];
            if (flag[x]) 
            {
                ans[i] = 0;
                if (flag[u]) 
                {
                    flag[x] = false;
                    flag[y] = true;
                }
            }
            else 
            {
                ans[i] = 1;
                if (flag[v]) 
                {
                    flag[y] = false;
                    flag[x] = true;
                }
            }
        }
    }
    for (int i = 1; i <= m; i++)
    {
        cout<<ans[i];
    }
    return 0;
}

 

 答案一定是YES

 

posted on 2020-10-17 23:19  Chri_K  阅读(502)  评论(0编辑  收藏  举报