BZOJ2330 [SCOI2011] Candy

2330: [SCOI2011] Candy

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 10025   Solved: 3439
[ Submit][ Status][ Discuss]

Description

There kindergarten N a child, lxhgww teacher and now want to give these kids allocation candy, candy requires every child to be assigned. However, there are kids jealous, always put forward some demands, such as Xiao Ming does not want red candy assigned more than his, so when the distribution of candy, lxhgww need to meet the children of K requirement. Kindergarten candy is always limited, lxhgww want to know how many he needs at least prepare candy, in order to make every child can be assigned candy, and children meet all the requirements.

Input

The first line of input two integers N , K .

Next K lines, showing the relationship of these points need to be met for each row . 3 digits, X- , A , B .

If the X-=. 1 , represents A small friend must be assigned candy and B th children assigned as many candy;

If the X-= 2 represents A small friend assigned candy must be less than the first B th children assigned candy ;

If the X-=. 3 represents the A th children assigned candy must be not less than B th children assigned candy ;

If the X-=. 4 represents A small friend must be assigned to more than candy of B th children assigned candy ;

If the X-=. 5 showing the first A small friend candy must be assigned to more than the first B th children assigned candy;

Output

Output a line indicating lxhgww number of candy teacher at least need to be prepared, if you can not meet all the requirements of the children, it is output -1 .

Sample Input

5 7
1 1 2
2 3 2
4 4 1
3 4 5
5 4 5
2 3 5
4 5 1

Sample Output

11

HINT

【data range】

    For 30% of the data, to ensure that N <= 100

    To 100% of the data, to ensure that N <= 100000

For all data, to ensure that K <= 100000,1 <= X <= 5,1 <= A, B <= N

Source

[ Submit][ Status][ Discuss]

HOME Back

answer

The completion of the constraints differential restraint system, then you can run up the road. But spfa dead, so we need another algorithm.

Because only the right side of 0 and 1, if present in FIG. 1 in a ring, then it must no solution. So no solution sentenced after tarjan obtained SCC. If a number SCC solvable inside certainly equal, when running on a DP longest path on the line DAG.

Time complexity \ (O (n + m) \)

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
    for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;

co int N=1e5+1;
int n,m,num,top,cnt;
int d[N],dfn[N],low[N],st[N],c[N],deg[N],f[N],scc[N];
bool ins[N];
vector<pair<int,int> > e[N],ec[N];
queue<int> q;

il void add(int x,int y,int z){
    e[x].push_back(make_pair(y,z));
}
il void addc(int x,int y,int z){
    ec[x].push_back(make_pair(y,z));
    ++deg[y];
}
void tarjan(int x){
    dfn[x]=low[x]=++num;
    st[++top]=x;
    ins[x]=1;
    for(unsigned i=0;i<e[x].size();++i){
        int y=e[x][i].first;
        if(!dfn[y]){
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(ins[y]) low[x]=min(low[x],dfn[y]);
    }
    if(dfn[x]==low[x]){
        ++cnt;
        int y;
        do{
            y=st[top--];
            ins[y]=0;
            c[y]=cnt;
            ++scc[cnt];
        }while(y!=x);
    }
}
int main(){
    read(n),read(m);
    for(int i=1,o,a,b;i<=m;++i){
        read(o),read(a),read(b);
        if(o==2) add(a,b,1);
        else if(o==3) add(b,a,0);
        else if(o==4) add(b,a,1);
        else if(o==5) add(a,b,0);
        else{
            add(a,b,0);
            add(b,a,0);
        }
    }
    for(int i=1;i<=n;++i) add(0,i,1);
    tarjan(0);
    for(int x=0;x<=n;++x)
        for(unsigned i=0;i<e[x].size();++i){
            int y=e[x][i].first,z=e[x][i].second;
            if(c[x]==c[y]){
                if(!z) continue;
                return puts("-1"),0;
            }
            addc(c[x],c[y],z);
        }
    q.push(c[0]); // edit 1: c[0]
    while(q.size()){
        int x=q.front();
        q.pop();
        for(unsigned i=0;i<ec[x].size();++i){
            int y=ec[x][i].first,z=ec[x][i].second;
            f[y]=max(f[y],f[x]+z);
            if(!--deg[y]) q.push(y);
        }
    }
    ll ans=0;
    for(int i=1;i<=cnt;++i) ans+=(ll)f[i]*scc[i];
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/10959962.html