Advertisement
STEFAN_STOYANOV

2785_Himiya za 96

May 19th, 2022 (edited)
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<string>
  6. #include<vector>
  7. #include<stack>
  8. #include<queue>
  9. #include<deque>
  10. #include<cstdio>
  11. #include<fstream>
  12. #include<map>
  13. #include<set>
  14. using namespace std;
  15. const int maxe=(1<<14),maxel=64,maxn=(1<<12),inf=(1<<30);
  16. struct edge
  17. {
  18.     int to,cap,next;
  19.     edge(){}
  20.     edge(int _to,int _cap,int _next)
  21.     {
  22.         to=_to;
  23.         cap=_cap;
  24.         next=_next;
  25.     }
  26. };
  27. edge e[maxe];int bre;
  28. int sum_of_a;
  29. int n,m,allv,a[maxel][maxel],S,T,F,p;
  30. int head[maxn],ptr[maxn],path[maxn],sz=0;
  31. int lvl[maxn];
  32. void read()
  33. {
  34.     cin>>n>>m;
  35.     string s;
  36.     for(int i=1;i<=n;i++)
  37.     {
  38.         cin>>s;
  39.         for(int j=0;j<m;++j)
  40.         {
  41.             switch(s[j])
  42.             {
  43.                 case 'H':a[i][j+1]=1;
  44.                          sum_of_a+=a[i][j+1];
  45.                          break;
  46.                 case 'O':a[i][j+1]=2;
  47.                          sum_of_a+=a[i][j+1];
  48.                          break;
  49.                 case 'N':a[i][j+1]=3;
  50.                          sum_of_a+=a[i][j+1];
  51.                          break;
  52.                 case 'C':a[i][j+1]=4;
  53.                          sum_of_a+=a[i][j+1];
  54.                          break;
  55.             }
  56.         }
  57.     }
  58. }
  59. void add_edge(int from,int to,int cap)
  60. {
  61.     e[bre]=edge(to,cap,head[from]);
  62.     head[from]=bre;bre++;
  63. }
  64. void init_graph()
  65. {
  66.     memset(head,-1,sizeof(head));
  67.     S=0;T=n*m+1;
  68.     int cur,cur1;
  69.     for(int i=1;i<=n;++i)
  70.         for(int j=1;j<=m;++j)
  71.         {
  72.             if(a[i][j])
  73.             {
  74.                 cur=(i-1)*m+j;
  75.                 //if((i%2==1&&j%2==1)||(i%2==0&&j%2==0))
  76.                 if(i%2==j%2)
  77.                 {
  78.                     add_edge(S,cur,a[i][j]);
  79.                     add_edge(cur,S,0);
  80.                     if(1<i)
  81.                     {
  82.                         if(a[i-1][j])
  83.                         {
  84.                             cur1=(i-2)*m+j;
  85.                             add_edge(cur,cur1,1);
  86.                             add_edge(cur1,cur,0);
  87.                         }
  88.                     }
  89.                     if(i<n)
  90.                     {
  91.                         if(a[i+1][j])
  92.                         {
  93.                             cur1=(i)*m+j;
  94.                             add_edge(cur,cur1,1);
  95.                             add_edge(cur1,cur,0);
  96.                         }
  97.                     }
  98.                     if(1<j)
  99.                     {
  100.                         if(a[i][j-1])
  101.                         {
  102.                             cur1=(i-1)*m+j-1;
  103.                             add_edge(cur,cur1,1);
  104.                             add_edge(cur1,cur,0);
  105.                         }
  106.                     }
  107.                     if(j<m)
  108.                     {
  109.                         if(a[i][j+1])
  110.                         {
  111.                             cur1=(i-1)*m+j+1;
  112.                             add_edge(cur,cur1,1);
  113.                             add_edge(cur1,cur,0);
  114.                         }
  115.                     }
  116.                 }
  117.                 else
  118.                 {
  119.                     add_edge(cur,T,a[i][j]);
  120.                     add_edge(T,cur,0);
  121.                 }
  122.             }
  123.         }
  124. }
  125. bool BFS()
  126. {
  127.     memset(lvl,0,sizeof(lvl));
  128.     lvl[T]=1;
  129.     queue<int>q;
  130.     q.push(T);
  131.     int w;
  132.     while(!q.empty())
  133.     {
  134.         w=q.front();
  135.         q.pop();
  136.         for(int i=head[w];i!=-1;i=e[i].next)
  137.             if(lvl[e[i].to]==0&&e[i^1].cap>0)
  138.         {
  139.             lvl[e[i].to]=lvl[w]+1;
  140.             q.push(e[i].to);
  141.         }
  142.     }
  143.     return lvl[S];
  144. }
  145. bool DFS(int ver)
  146. {
  147.     if(ver==T)return true;
  148.     for(int i=ptr[ver];i!=-1;i=ptr[ver]=e[i].next)
  149.         if(lvl[e[i].to]+1==lvl[ver]&&e[i].cap>0)
  150.         {
  151.             if(DFS(e[i].to))
  152.             {
  153.                 if(p>e[i].cap)
  154.                     p=e[i].cap;
  155.                 path[sz]=i;
  156.                 sz++;
  157.                 return true;
  158.             }
  159.         }
  160.     return false;
  161. }
  162. void inline update()
  163. {
  164.     int x;
  165.     for(int i=0;i<sz;++i)
  166.     {
  167.         x=path[i];
  168.         e[x].cap-=p;
  169.         e[x^1].cap+=p;
  170.     }
  171. }
  172. void Dinitz()
  173. {
  174.     while(BFS())
  175.     {
  176.         for(int i=0;i<=n*m+1;++i)
  177.             ptr[i]=head[i];
  178.         sz=0;
  179.         p=inf;
  180.         while(DFS(S))
  181.         {
  182.             F+=p;
  183.             update();
  184.             sz=0;
  185.             p=inf;
  186.         }
  187.     }
  188.     if(sum_of_a%2)
  189.         cout<<"Invalid\n";
  190.     else if(F*2==sum_of_a)
  191.         cout<<"Valid\n";
  192.     else cout<<"Invalid\n";
  193. }
  194. int main()
  195. {
  196.     ios_base::sync_with_stdio(false);
  197.     cin.tie(NULL);
  198.     cout.tie(NULL);
  199.     read();
  200.     init_graph();
  201.     /*cout<<"Initializated graph\n";
  202.     for(int i=0;i<=n*m+1;i++)
  203.     {
  204.         cout<<"i=="<<i<<endl;
  205.         for(int j=head[i];j!=-1;j=e[j].next)
  206.             cout<<e[j].to<<' '<<e[j].cap<<endl;
  207.         cout<<endl;
  208.     }
  209.     cout<<endl;*/
  210.     Dinitz();
  211. return 0;
  212. }
  213. /*
  214. 3 4
  215. HOH.
  216. NCOH
  217. OO..
  218. */
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement