//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "航空路线问题  (EK)": {
        "prefix": "acm-3.5MCMF",
        "body": [
            "#include<algorithm>",
            "#include<iostream>",
            "#include<string>",
            "#include<cstdio>",
            "#include<queue>",
            "#include<map>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=103,M=40000,inf=2e9;",
            "int x,y,o=1,n,m,h,t,st,ed,flag,cyf[N],pan[N],pre[N],dis[N],head[N];LL maxcost,maxflow;",
            "struct QAQ{int w,to,next,flow;}a[M<<1];queue<int>Q;string CH,ch[N];map<string,int>ip;",
            "inline void add(Re x,Re y,Re z,Re w){a[++o].flow=z,a[o].w=w,a[o].to=y,a[o].next=head[x],head[x]=o;}",
            "inline void add_(Re a,Re b,Re flow,Re w){add(a,b,flow,w),add(b,a,0,-w);}",
            "inline int SPFA(Re st,Re ed){",
            "\tfor(Re i=0;i<=ed;++i)dis[i]=-inf,pan[i]=0;",
            "\tQ.push(st),pan[st]=1,dis[st]=0,cyf[st]=inf;",
            "\twhile(!Q.empty()){",
            "\t\tRe x=Q.front();Q.pop();pan[x]=0;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif(a[i].flow&&dis[to=a[i].to]<dis[x]+a[i].w){//�· ",
            "\t\t\t\tdis[to]=dis[x]+a[i].w,pre[to]=i;",
            "\t\t\t\tcyf[to]=min(cyf[x],a[i].flow);",
            "\t\t\t\tif(!pan[to])pan[to]=1,Q.push(to);",
            "\t\t\t}",
            "\t}",
            "\treturn dis[ed]!=-inf;",
            "}",
            "inline void EK(Re st,Re ed){",
            "\twhile(SPFA(st,ed)){",
            "\t\tRe x=ed;maxflow+=cyf[ed],maxcost+=(LL)cyf[ed]*dis[ed];",
            "\t\twhile(x!=st){",
            "\t\t\tRe i=pre[x];",
            "\t\t\ta[i].flow-=cyf[ed];",
            "\t\t\ta[i^1].flow+=cyf[ed];",
            "\t\t\tx=a[i^1].to;",
            "\t\t}",
            "\t}",
            "}",
            "inline void dfs1(Re x){",
            "\tpan[x]=1;",
            "\tcout<<ch[x-n]<<endl;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)<=n&&!a[i].flow){dfs1(to+n);break;}",
            "}",
            "inline void dfs2(Re x){",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)<=n&&!a[i].flow&&!pan[to+n])dfs2(to+n);",
            "\tcout<<ch[x-n]<<endl;",
            "}",
            "int main(){",
            "\tcin>>n>>m;st=1,ed=n<<1;",
            "\tfor(Re i=1;i<=n;++i)cin>>ch[i],ip[ch[i]]=i;",
            "\tfor(Re i=2;i<n;++i)add_(i,n+i,1,1);",
            "\tadd_(1,1+n,2,1),add_(n,n+n,2,1);",
            "\twhile(m--){",
            "\t\tcin>>CH;x=ip[CH];",
            "\t\tcin>>CH;y=ip[CH];",
            "\t\tif(x>y)swap(x,y);",
            "\t\tflag|=x==1&&y==n; ",
            "\t\tadd_(x+n,y,1,0);",
            "\t}",
            "\tEK(st,ed);",
            "\tif(maxflow==2)printf(\"%d\\n\",maxcost-2);",
            "\telse if(maxflow==1&&flag){",
            "\t\tprintf(\"2\\n\");",
            "\t\tcout<<ch[1]<<endl<<ch[n]<<endl<<ch[1]<<endl;",
            "\t\treturn 0;",
            "\t}",
            "\telse return !printf(\"No Solution!\\n\");",
            "\tfor(Re i=1;i<=n+2;++i)pan[i+n]=0;",
            "\tdfs1(1+n),dfs2(1+n);",
            "}",
            ""
        ],
        "description": "插入 航空路线问题  (EK)"
    }
}