• COJ 0578 4019二分图判定


    4019二分图判定
    难度级别: B; 编程语言:不限;运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B
    试题描述

    给定一个具有n个顶点(顶点编号为0,1,……,n-1)的图,要给图上每个顶点染色,并且要使相邻的顶点颜色不同。问是否能最多用2种颜色进行染色?测试数据保证没有重边和自环。

    输入
    第一行包括两个数n和k,分别表示图的顶点数和边数,接下来有k行,每行有两个整数m1和m2,表示顶点m1和m2间有一条无向边。各行的整数间用一个空格分隔。
    输出
    如果能就输出1,否则输出0.
    输入示例
    3 3
    0 1
    0 2
    1 2
    输出示例
    0
    其他说明
    数据范围:1<=n<=1000,发现猜答案的刷题就封号。

    题解:染个色。我记得可以用LCT动态维护二分图可惜我忘了QAQ。。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<cstring>
     7 #define PAU putchar(' ')
     8 #define ENT putchar('
    ')
     9 using namespace std;
    10 const int maxn=1000+10,inf=-1u>>1;
    11 struct ted{int x,y;ted*nxt;}adj[maxn<<1],*fch[maxn],*ms=adj;
    12 void add(int x,int y){
    13     *ms=(ted){x,y,fch[x]};fch[x]=ms++;*ms=(ted){y,x,fch[y]};fch[y]=ms++;return;
    14 }
    15 bool col[maxn];
    16 bool color(int x){
    17     for(ted*e=fch[x];e;e=e->nxt){
    18         int v=e->y;
    19         if(!col[v]){col[v]=!col[x];if(!color(v))return false;}
    20         else if(col[v]==col[x])return false;
    21     }return true;
    22 }
    23 inline int read(){
    24     int x=0,sig=1;char ch=getchar();
    25     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
    26     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
    27     return sig?x:-x;
    28 }
    29 inline void write(int x){
    30     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    31     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
    32     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
    33 }
    34 int n,m;
    35 void init(){
    36     n=read();m=read();
    37     for(int i=1;i<=m;i++)add(read(),read());
    38     return;
    39 }
    40 void work(){
    41     col[0]=1;puts(color(0)?"1":"0");
    42     return;
    43 }
    44 void print(){
    45     return;
    46 }
    47 int main(){init();work();print();return 0;}
  • 相关阅读:
    poj 1573 Robot Motion
    poj 1035 Spell checker
    poj 3080 Blue Jeans
    poj 3468 A Simple Problem with Integers(线段树区间更新)
    poj 3687 Labeling Balls(拓补排序)
    poj 3295 Tautology
    poj 1062 昂贵的聘礼
    hdu 1170 Balloon Comes!
    hdu 1215 七夕节
    OCJP-试题集合 | 对象的比较
  • 原文地址:https://www.cnblogs.com/chxer/p/4669336.html
Copyright © 2020-2023  润新知