zoukankan      html  css  js  c++  java
  • 二分图匹配

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=5971

    把已经告诉你的把能推测的都推测出来有矛盾就“NO”,剩下的跑二分图,矛盾就“NO”,剩下如果还有没有颜色的“NO”。

      1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
      2 #include <cstdio>//sprintf islower isupper
      3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
      4 #include <iostream>//pair
      5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin);
      6 #include <bitset>
      7 //#include <map>
      8 //#include<unordered_map>
      9 #include <vector>
     10 #include <stack>
     11 #include <set>
     12 #include <string.h>//strstr substr
     13 #include <string>
     14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
     15 #include <cmath>
     16 #include <deque>
     17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
     18 #include <vector>//emplace_back
     19 //#include <math.h>
     20 #include <cassert>
     21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
     22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
     23 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
     24 //******************
     25 int abss(int a);
     26 int lowbit(int n);
     27 int Del_bit_1(int n);
     28 int maxx(int a,int b);
     29 int minn(int a,int b);
     30 double fabss(double a);
     31 void swapp(int &a,int &b);
     32 clock_t __STRAT,__END;
     33 double __TOTALTIME;
     34 void _MS(){__STRAT=clock();}
     35 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
     36 //***********************
     37 #define rint register int
     38 #define fo(a,b,c) for(rint a=b;a<=c;++a)
     39 #define fr(a,b,c) for(rint a=b;a>=c;--a)
     40 #define mem(a,b) memset(a,b,sizeof(a))
     41 #define pr printf
     42 #define sc scanf
     43 #define ls rt<<1
     44 #define rs rt<<1|1
     45 typedef vector<int> VI;
     46 typedef long long ll;
     47 const double E=2.718281828;
     48 const double PI=acos(-1.0);
     49 //const ll INF=(1LL<<60);
     50 const int inf=(1<<30);
     51 const double ESP=1e-9;
     52 const int mod=(int)1e9+7;
     53 const int N=(int)1e4+10;
     54 
     55 queue<int>good,bad;
     56 struct node
     57 {
     58     int u,v;
     59 }edge[N];
     60 vector<int>v[N];
     61 vector<int>mp[N];
     62 int mark[N],color[N];
     63 bool FLAG;
     64 
     65 bool bfs(int s)
     66 {
     67     color[s] = 1; //连通分支的起点可以染1也可以染0
     68     queue<int>q;
     69     q.push(s);
     70     while (!q.empty())
     71     {
     72         s = q.front();
     73         q.pop();
     74         for (int i = 0; i < (int)mp[s].size(); i ++) {
     75             int t = mp[s][i];
     76             if (color[t] == 0) color[t] =3-color[s], q.push(t); //如果相邻顶点未被染色,则加入
     77             //队列。
     78             if (color[t] == color[s]) { //如果相邻顶点染相同颜色,则不是二分图。
     79                 return false;
     80             }
     81         }
     82     }
     83     return true;
     84 }
     85 
     86 int main()
     87 {
     88     int n,m,x,y;
     89     while(~sc("%d%d%d%d",&n,&m,&x,&y))
     90     {
     91         FLAG=1;
     92         while(!good.empty())good.pop();
     93         while(!bad.empty())bad.pop();
     94         for(int i=1;i<=n;++i)v[i].clear(),mp[i].clear(),mark[i]=color[i]=0;
     95         for(int i=1;i<=m;++i)
     96         {
     97             int U,V;
     98             sc("%d%d",&U,&V);
     99             edge[i]={U,V};
    100             v[U].push_back(V);
    101             v[V].push_back(U);
    102             mp[U].push_back(V);
    103             mp[V].push_back(U);
    104         }
    105         for(int i=1;i<=x;++i)
    106         {
    107             int tt;
    108             sc("%d",&tt);
    109             mark[tt]=1;
    110             good.push(tt);
    111         }
    112         for(int i=1;i<=y;++i)
    113         {
    114             int tt;
    115             sc("%d",&tt);
    116             mark[tt]=2;
    117             bad.push(tt);
    118         }
    119         bool f=0;
    120         while(!good.empty()||!bad.empty())
    121         {
    122             while(!good.empty())
    123             {
    124                 int now=good.front();good.pop();
    125                 int sz=v[now].size();
    126                 for(int i=sz-1;i>=0;--i)
    127                 {
    128                     int to=v[now][i];v[now].pop_back();
    129                     if(mark[to]==0)
    130                         bad.push(to);
    131                     else if(mark[to]==1)
    132                         f=1;
    133                     else if(mark[to]==2)
    134                         continue;
    135                 }
    136             }
    137             while(!bad.empty())
    138             {
    139                 int now=bad.front();bad.pop();
    140                 int sz=v[now].size();
    141                 for(int i=sz-1;i>=0;--i)
    142                 {
    143                     int to=v[now][i];v[now].pop_back();
    144                     if(mark[to]==0)
    145                         good.push(to);
    146                     else if(mark[to]==1)
    147                         continue;
    148                     else if(mark[to]==2)
    149                         f=1;
    150                 }
    151             }
    152         }
    153         if(f)
    154         {
    155             pr("NO
    ");
    156         }
    157         else
    158         {
    159             for(int i=1;i<=n;++i)
    160             {
    161                 if(mark[i]==0&&color[i]==0&&mp[i].size()&&!bfs(i))
    162                 {
    163                     FLAG=0;
    164                     break;
    165                 }
    166             }
    167             for(int i=1;i<=n;++i)
    168                 if(mark[i]==0&&color[i]==0)
    169                     FLAG=0;
    170             if(FLAG)
    171                 pr("YES
    ");
    172             else
    173                 pr("NO
    ");
    174         }
    175     }
    176     return 0;
    177 }
    178 
    179 /**************************************************************************************/
    180 
    181 int maxx(int a,int b)
    182 {
    183     return a>b?a:b;
    184 }
    185 
    186 void swapp(int &a,int &b)
    187 {
    188     a^=b^=a^=b;
    189 }
    190 
    191 int lowbit(int n)
    192 {
    193     return n&(-n);
    194 }
    195 
    196 int Del_bit_1(int n)
    197 {
    198     return n&(n-1);
    199 }
    200 
    201 int abss(int a)
    202 {
    203     return a>0?a:-a;
    204 }
    205 
    206 double fabss(double a)
    207 {
    208     return a>0?a:-a;
    209 }
    210 
    211 int minn(int a,int b)
    212 {
    213     return a<b?a:b;
    214 }
  • 相关阅读:
    git(重点)
    C#技巧记录——持续更新
    结点和节点的区别
    WebSocketSharp send record_stop without send record_start
    cefsharp 拦截所有请求 解决chunked导致数据接收不完整的问题
    计算mp3长度 毫秒
    pydub分割音频文件
    c# 获取文件信息
    实现一边写代码一边展示网页的效果
    c# webapi swagger Area 多级层次分组 添加header参数
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11745668.html
Copyright © 2011-2022 走看看