//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 的格式要求是 "插入 [该代码片段的中文名]"

{
    "线段树优化建边+Dinic最大流": {
        "prefix": "acm-3.7Last chance",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=5003+3,M=25e4+3,inf=2e9;",
            "int n,m,x,y,z,O,op,st,ed,X[N],Y[N],Z[N],Ans[N],cnt[N];",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "struct Network_Flow{",
            "\tint o,h,t,maxflow,Q[N*3],dis[N*3],cur[N*3],head[N*3];",
            "\tstruct QAQ{int to,next,flow;}a[M<<1];",
            "\tinline void add_(Re x,Re y,Re flow){a[++o].to=y,a[o].flow=flow,a[o].next=head[x],head[x]=o;}",
            "\tinline void add(Re x,Re y,Re flow){add_(x,y,flow),add_(y,x,0);}",
            "\tinline int bfs(Re st,Re ed){",
            "\t\tfor(Re i=0;i<=ed;++i)dis[i]=0,cur[i]=head[i];",
            "\t\th=1,t=0,dis[Q[++t]=st]=1;",
            "\t\twhile(h<=t){",
            "\t\t\tRe x=Q[h++];",
            "\t\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\t\tif(a[i].flow&&!dis[to=a[i].to]){",
            "\t\t\t\t\tdis[Q[++t]=to]=dis[x]+1;",
            "\t\t\t\t\tif(to==ed)return 1;",
            "\t\t\t\t}",
            "\t\t}",
            "\t\treturn 0;",
            "\t}",
            "\tinline int dfs(Re x,Re flow){",
            "\t\tif(x==ed||!flow)return flow;",
            "\t\tRe tmp=0,to,f;",
            "\t\tfor(Re i=cur[x];i;i=a[i].next){",
            "\t\t\tcur[x]=i;",
            "\t\t\tif(dis[to=a[i].to]==dis[x]+1&&(f=dfs(to,min(a[i].flow,flow-tmp)))){",
            "\t\t\t\ta[i].flow-=f,a[i^1].flow+=f,tmp+=f;",
            "\t\t\t\tif(tmp==flow)break;",
            "\t\t\t}",
            "\t\t}",
            "\t\treturn tmp;",
            "\t}",
            "\tinline void dinic(Re st,Re ed){while(bfs(st,ed))maxflow+=dfs(st,inf);}",
            "\tinline int find(Re x){",
            "\t\tRe last=ed;",
            "\t\twhile(x<=O||x>O+n){",
            "\t\t\tfor(Re i=head[x];i;i=a[i].next)",
            "\t\t\t\tif(a[i^1].to==x&&a[i].to!=last&&a[i].flow){",
            "\t\t\t\t\t--a[i].flow,last=x,x=a[i].to;break;",
            "\t\t\t\t}",
            "\t\t}",
            "\t\treturn x-O;",
            "\t}",
            "}T1;",
            "struct Segment_Tree{",
            "\t#define pl (p<<1)",
            "\t#define pr (p<<1|1)",
            "\t#define mid ((L+R)>>1)",
            "\tstruct QAQ{int in;}tr[N<<2];",
            "\tinline void build(Re p,Re L,Re R){",
            "\t\tif(L==R){tr[p].in=L;return;}",
            "\t\ttr[p].in=++O,build(pl,L,mid),build(pr,mid+1,R);",
            "\t\tT1.add(tr[p].in,tr[pl].in,inf),T1.add(tr[p].in,tr[pr].in,inf);",
            "\t}",
            "\tinline void change(Re p,Re L,Re R,Re l,Re r,Re x,Re w){",
            "\t\tif(l<=L&&R<=r){T1.add(x,tr[p].in,w);return;}",
            "\t\tif(l<=mid)change(pl,L,mid,l,r,x,w);",
            "\t\tif(r>mid)change(pr,mid+1,R,l,r,x,w);",
            "\t}",
            "}TR;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(m),T1.o=1,O=m,TR.build(1,1,m),st=O+n+1,ed=st+1;",
            "\tfor(Re i=1;i<=n;++i){",
            "\t\tin(op);",
            "\t\tif(op<1){",
            "\t\t\tin(x),T1.add(st,O+i,1);",
            "\t\t\twhile(x--)in(y),T1.add(O+i,y,1);",
            "\t\t}",
            "\t\telse if(op<2)in(x),in(y),T1.add(st,O+i,1),TR.change(1,1,m,x,y,O+i,1);",
            "\t\telse{",
            "\t\t\tin(x),in(y),in(z),T1.add(st,O+i,2),X[i]=x,Y[i]=y,Z[i]=z;",
            "\t\t\tT1.add(O+i,x,1),T1.add(O+i,y,1),T1.add(O+i,z,1);",
            "\t\t}",
            "\t}",
            "\tfor(Re j=1;j<=m;++j)T1.add(j,ed,1);",
            "\tT1.dinic(st,ed),printf(\"%d\\n\",T1.maxflow);",
            "\tfor(Re i=T1.head[ed];i;i=T1.a[i].next)",
            "\t\tif(T1.a[i].flow)++cnt[Ans[T1.a[i].to]=T1.find(T1.a[i].to)];",
            "\tfor(Re i=1;i<=n;++i)",
            "\t\tif(X[i]&&cnt[i]==1){",
            "\t\t\tif(Ans[X[i]]==i)Ans[Y[i]]=i;",
            "\t\t\telse Ans[X[i]]=i;",
            "\t\t}",
            "\tfor(Re i=1;i<=m;++i)if(Ans[i])printf(\"%d %d\\n\",Ans[i],i);",
            "}",
            ""
        ],
        "description": "插入 线段树优化建边+Dinic最大流"
    }
}