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

{
    "差分约束系统": {
        "prefix": "acm-3.6糖果",
        "body": [
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+5,M=3e5+5,inf=2e9;",
            "int x,y,n,m,o,op,chu[N],dis[N],pan[N],head[N];long long ans;",
            "struct QAQ{int w,to,next;}a[M];queue<int>Q;",
            "inline void add(Re x,Re y,Re z){a[++o].to=y,a[o].w=z,a[o].next=head[x],head[x]=o;}",
            "inline void in(Re &x){",
            "\tRe fu=0;x=0;char ch=getchar();",
            "\twhile(ch<'0'||ch>'9')fu|=ch=='-',ch=getchar();",
            "\twhile(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();",
            "\tx=fu?-x:x;",
            "}",
            "inline int SPFA(Re st,Re ed){",
            "\tfor(Re i=0;i<=n;++i)dis[i]=-inf,chu[i]=0;",
            "\tdis[st]=0,chu[st]=pan[st]=1,Q.push(st);",
            "\twhile(!Q.empty()){",
            "\t\tx=Q.front(),Q.pop();",
            "\t\tpan[x]=0;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif(dis[to=a[i].to]<dis[x]+a[i].w){",
            "\t\t\t\tdis[to]=dis[x]+a[i].w;",
            "\t\t\t\tif((chu[to]=chu[x]+1)>n+1)return 1;",
            "\t\t\t\tif(!pan[to])pan[to]=1,Q.push(to);",
            "\t\t\t}",
            "\t}",
            "\treturn 0;",
            "}",
            "int main(){",
            "\tin(n),in(m);",
            "\tfor(Re i=n;i>=1;--i)add(0,i,1); //a[i]>=a[0]=1,a[i]-a[0]>=1",
            "\twhile(m--){",
            "\t\tin(op),in(x),in(y);",
            "\t\tif(op<2)add(y,x,0),add(x,y,0);",
            "\t\telse if(op<3)add(x,y,1);//a[x]<a[y]: a[y]-a[x]>=1",
            "\t\telse if(op<4)add(y,x,0);//a[x]>=a[y]: a[x]-a[y]>=0",
            "\t\telse if(op<5)add(y,x,1);//a[x]>a[y]: a[x]-a[y]>=1",
            "\t\telse add(x,y,0);//a[x]<=a[y]: a[y]-a[x]>=0",
            "\t\tif(!(op%2)&&x==y){puts(\"-1\");return 0;}",
            "\t}",
            "\tif(SPFA(0,n))puts(\"-1\");",
            "\telse{",
            "\t\tfor(Re i=1;i<=n;++i)ans+=dis[i];",
            "\t\tprintf(\"%lld\",ans);",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 差分约束系统"
    }
}