JZOJ 4822. 【NOIP2016提高A组集训第1场10.29】完美标号

来源:互联网 发布:属下知罪by枯目剧透 编辑:程序博客网 时间:2024/05/16 11:31

Problem

Description

给定M个二元组(A_i, B_i),求X_1, …, X_N满足:对于任意(A_i, B_i),有|X_{A_i} - X_{B_i}| = 1成立。

Input

第1行,2个整数N、M。
第2行到第M + 1行,2个整数A_i和B_i。

Output

第1行,1个字符串,”YES”表示有解,”NO”表示无解。
第2行,N个整数,X_1, X_2, …, X_N,无解则不输出。
要求|X_i| <= 1,000,000,000,任意一解均可。

Sample Input

输入1:
3 3
1 2
2 3
3 1
输入2:
6 5
1 2
2 3
3 4
4 1
5 6

Sample Output

输出1:
NO
输出2:
YES
0 1 0 1 -99 -100

Data Constraint

对于40%的数据,1 <= N <= 10。
对于100%的数据,1 <= N <= 10,000,0 <= M <= 100,000,1 <= A_i, B_i <= N。

Solution

01染色,如果一条边的两个点染成同一个色,就无解。否则有解。
看一下N的范围,如果太大就不要用递归,否则会炸掉。

Code

#include<iostream>#include<cstdio>#include<cstring>#define fo(i,a,b) for(i=a;i<=b;i++)#define N 10010#define LL long longusing namespace std;int n,m,i,j,x,y,o,tot;LL a[N],ans[N];bool bz[N];int main(){    scanf("%d%d",&n,&m);tot=0;    memset(ans,255,sizeof(ans));    o=0;    fo(i,1,m)    {        scanf("%d%d",&x,&y);        if (!bz[x] && !bz[y])        {            bz[x]=bz[y]=1;            ans[x]=0;            ans[y]=1;        }        if (!bz[x] && bz[y])        {            bz[x]=1;            ans[x]=!ans[y];        }        if (bz[x] && !bz[y])        {            bz[y]=1;            ans[y]=!ans[x];        }        if (bz[x] && bz[y] && (ans[x]==ans[y]))        {            o=1;            break;        }    }    if (!o)    {        printf("YES\n");        fo(i,1,n) printf("%d ",ans[i]);    }    else printf("NO");}
3 0
原创粉丝点击