• hdu 3478 Catch--二分图判断


    我觉得,给了初始点的话用bfs方便点,没有则dfs ||可能超片面

    https://vjudge.net/contest/281085?tdsourcetag=s_pcqq_aiomsg#problem/C

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<iostream>
     5 #include<stdlib.h>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<vector>
     9 #include<string>
    10 #include<set>
    11 #include<cctype>
    12 #include<sstream>
    13 #define mem(a) memset(a,0,sizeof(a))
    14 #define LL long long
    15 #define inf 0x3f3f3f3f
    16 using namespace std;
    17 const int N=1e5+10;
    18 int color[N],fa[N];
    19 int n,m,now;
    20 vector<int> link[N];
    21 queue<int> q;
    22 int bfs()
    23 {
    24     memset(color,-1,sizeof(color));
    25     color[now]=1;
    26     while(!q.empty())
    27     {
    28         int k=q.front();
    29         q.pop();
    30      for(size_t i=0;i<link[k].size();i++)
    31      {
    32          int t=link[k][i]; //表示定点k通过i边连接的点
    33          if(color[t]==color[k]) //染色是否相同
    34             return 1;
    35          if(color[t]==-1)
    36          {
    37              q.push(t);
    38              color[t]=1-color[k];//1||0
    39          }
    40      }
    41     }
    42     return 0;
    43 }
    44 int main()
    45 {
    46     int a,b,t;
    47     scanf("%d",&t);
    48     for(int i=1;i<=t;i++)
    49     {
    50      scanf("%d%d%d",&n,&m,&now);
    51 
    52      for(int j=0;j<n;j++)
    53         link[j].clear();
    54 
    55      for(int j=0;j<m;j++)
    56      {
    57          scanf("%d %d",&a,&b);
    58          link[a].push_back(b);
    59          link[b].push_back(a);
    60      }
    61 
    62     while(!q.empty())
    63      q.pop();
    64 
    65     q.push(now);
    66 
    67     if(!bfs()) //不是二分图,输出yes
    68     printf("Case %d: NO
    ",i);
    69     else
    70     printf("Case %d: YES
    ",i);
    71     }
    72   return 0;
    73 }
    View Code
  • 相关阅读:
    Looks like the Spring listener was not configured for your web app!
    数据库--加入字段语句
    Snail—OC学习之数组NSArray
    Primary Key &amp; Index
    王立平--android中让程序终止退出的解决的方法 System.exit(0)
    Scala具体解释---------高速入门Scala
    NOI2014 起床困难综合症
    转自作者:phylips@bmy
    转自 void- man 差分约束系统详解
    精辟!
  • 原文地址:https://www.cnblogs.com/XXrll/p/10323493.html
Copyright © 2020-2023  润新知