zoukankan      html  css  js  c++  java
  • 带花树

    带花树模板

      1 #include<queue>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #define N 605
      6 #define M 200010
      7 using namespace std;
      8 int n,m,tot,head[N],match[N],fa[N],flower[N];
      9 bool inflower[N],vis[N];
     10 struct edge{int next,to;}e[M];
     11 queue<int>q;
     12 inline void add(int u,int v)
     13 {
     14     e[++tot]=(edge){head[u],v};
     15     head[u]=tot;
     16     e[++tot]=(edge){head[v],u};
     17     head[v]=tot;
     18 }
     19 int lca(int u,int v)
     20 {
     21     int inpath[N];
     22     memset(inpath,0,sizeof(inpath));
     23     while(1)
     24     {
     25         u=flower[u];
     26         inpath[u]=1;
     27         if(!match[u])break;
     28         u=fa[match[u]];
     29     }
     30     while(1)
     31     {
     32         v=flower[v];
     33         if(inpath[v])return v;
     34         v=fa[match[v]];
     35     }
     36 }
     37 void reset(int u,int anc)
     38 {
     39     while(u!=anc)
     40     {
     41         int v=match[u];
     42         inflower[flower[u]]=1;
     43         inflower[flower[v]]=1;
     44         v=fa[v];
     45         if(flower[v]!=anc)fa[v]=match[u];
     46         u=v;
     47     }
     48 }
     49 void bloom(int u,int v)
     50 {
     51     int t=lca(u,v);
     52     memset(inflower,0,sizeof(inflower));
     53     reset(u,t);reset(v,t);
     54     if(flower[u]!=t)fa[u]=v;
     55     if(flower[v]!=t)fa[v]=u;
     56     for(int i=1;i<=n;i++)
     57     if(inflower[flower[i]])
     58     {
     59         flower[i]=t;
     60         if(!vis[i])
     61         vis[i]=1,q.push(i);
     62     }
     63 }
     64 bool path(int s)
     65 {
     66     memset(vis,0,sizeof(vis));
     67     memset(fa,0,sizeof(fa));
     68     for(int i=1;i<=n;i++)flower[i]=i;
     69     while(!q.empty())q.pop();
     70     q.push(s);vis[s]=1;
     71     while(!q.empty())
     72     {
     73         int u=q.front();q.pop();
     74         for(int i=head[u];i;i=e[i].next)
     75         if(flower[u]!=flower[e[i].to]&&match[u]!=e[i].to)
     76         {
     77             if(e[i].to==s||(match[e[i].to]&&fa[match[e[i].to]]))
     78             bloom(u,e[i].to);
     79             else if(!fa[e[i].to])
     80             {
     81                 fa[e[i].to]=u;
     82                 if(match[e[i].to])
     83                 {
     84                     q.push(match[e[i].to]);
     85                     vis[match[e[i].to]]=1;
     86                 }
     87                 else
     88                 {
     89                     u=e[i].to;
     90                     while(u)
     91                     {
     92                         match[u]=fa[u];
     93                         swap(u,match[fa[u]]);
     94                     }
     95                     return 1;
     96                 }
     97             }
     98         }
     99     }
    100     return 0;
    101 }
    102 void init()
    103 {
    104     int t,x,y;
    105     scanf("%d%d%d",&n,&m,&t);
    106     memset(head,0,sizeof(head));
    107     memset(match,0,sizeof(match));
    108     int ans=tot=0;
    109     for(int i=1;i<=n;i++)
    110     if(!match[i])ans+=path(i);
    111 }
    112 int main()
    113 {
    114     int T;
    115     scanf("%d",&T);
    116     while(T--)init();
    117 }
    View Code
    就让我永远不在这里写什么有意义的话--月下孤狼
  • 相关阅读:
    Codeforces Round #592 (Div. 2)C. The Football Season(暴力,循环节)
    Educational Codeforces Round 72 (Rated for Div. 2)D. Coloring Edges(想法)
    扩展KMP
    poj 1699 Best Sequence(dfs)
    KMP(思路分析)
    poj 1950 Dessert(dfs)
    poj 3278 Catch That Cow(BFS)
    素数环(回溯)
    sort与qsort
    poj 1952 buy low buy lower(DP)
  • 原文地址:https://www.cnblogs.com/xuruifan/p/5187463.html
Copyright © 2011-2022 走看看