[SCOI2011]糖果

<body> <center><h1>2330: [SCOI2011]糖果</h1><span class="green">Time Limit: </span>10 Sec&nbsp;&nbsp;<span class="green">Memory Limit: </span>128 MB<br><span class="green">Submit: </span>10025&nbsp;&nbsp;<span class="green">Solved: </span>3439<br>[<a href="submitpage.php?id=2330">Submit</a>][<a href="problemstatus.php?id=2330">Status</a>][<a href="bbs.php?id=2330">Discuss</a>]</center><h2>Description</h2><div class="content"><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">幼儿园里有<font face="Times New Roman">N</font><font face="宋体">个小朋友,</font><font face="Times New Roman">lxhgww</font><font face="宋体">老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果。但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖果的时候,</font><font face="Times New Roman">lxhgww</font><font face="宋体">需要满足小朋友们的</font><font face="Times New Roman">K</font><font face="宋体">个要求。幼儿园的糖果总是有限的,</font><font face="Times New Roman">lxhgww</font><font face="宋体">想知道他至少需要准备多少个糖果,才能使得每个小朋友都能够分到糖果,并且满足小朋友们所有的要求。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <!--EndFragment--></div><h2>Input</h2><div class="content"><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">输入的第一行是两个整数<font face="Times New Roman">N</font><font face="宋体">,</font><font face="Times New Roman">K</font><font face="宋体">。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">接下来<font face="Times New Roman">K</font><font face="宋体">行,表示这些点需要满足的关系,每行</font><font face="Times New Roman">3</font><font face="宋体">个数字,</font><font face="Times New Roman">X</font><font face="宋体">,</font><font face="Times New Roman">A</font><font face="宋体">,</font><font face="Times New Roman">B</font><font face="宋体">。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">如果<font face="Times New Roman">X=1</font><font face="宋体">,&nbsp;表示第</font><font face="Times New Roman">A</font><font face="宋体">个小朋友分到的糖果必须和第</font><font face="Times New Roman">B</font><font face="宋体">个小朋友</font>分到的糖果一样多;</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">如果<font face="Times New Roman">X=2</font><font face="宋体">,&nbsp;</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">表示第<font face="Times New Roman">A</font><font face="宋体">个小朋友分到的糖果必须少于第</font><font face="Times New Roman">B</font><font face="宋体">个小朋友</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">分到的糖果</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">;</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">如果<font face="Times New Roman">X=3</font><font face="宋体">,&nbsp;</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">表示第<font face="Times New Roman">A</font><font face="宋体">个小朋友分到的糖果必须不少于第</font><font face="Times New Roman">B</font><font face="宋体">个小朋友</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">分到的糖果</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">;</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">如果<font face="Times New Roman">X=4</font><font face="宋体">,&nbsp;</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">表示第<font face="Times New Roman">A</font><font face="宋体">个小朋友分到的糖果必须多于第</font><font face="Times New Roman">B</font><font face="宋体">个小朋友</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">分到的糖果</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">;</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">如果<font face="Times New Roman">X=5</font><font face="宋体">,&nbsp;</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">表示第<font face="Times New Roman">A</font><font face="宋体">个小朋友分到的糖果必须不多于第</font><font face="Times New Roman">B</font><font face="宋体">个小朋友</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">分到的糖果;</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p></div><h2>Output</h2><div class="content"><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">输出一行,表示<font face="Times New Roman">lxhgww</font><font face="宋体">老师至少需要准备的糖果数,如果不能满足小朋友们的所有要求,就输出</font><font face="Times New Roman">-1</font><font face="宋体">。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p> <p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 20.25pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p></div><h2>Sample Input</h2> <div class="content"><span class="sampledata">5 7<br> 1 1 2<br> 2 3 2<br> 4 4 1<br> 3 4 5<br> 5 4 5<br> 2 3 5<br> 4 5 1<br> <br> </span></div><h2>Sample Output</h2> <div class="content"><span class="sampledata"> 11<br> <br> </span></div><h2>HINT</h2> <div class="content"><p></p><p>【数据范围】</p> <p>&nbsp;&nbsp;&nbsp; 对于30%的数据,保证 N&lt;=100</p> <p>&nbsp;&nbsp;&nbsp; 对于100%的数据,保证 N&lt;=100000</p> <p>对于所有的数据,保证 K&lt;=100000,1&lt;=X&lt;=5,1&lt;=A, B&lt;=N<br><br> </p><p></p></div><h2>Source</h2> <div class="content"><p><a href="problemset.php?search=Day1">Day1</a></p></div><center>[<a href="submitpage.php?id=2330">Submit</a>][<a href="problemstatus.php?id=2330">Status</a>][<a href="bbs.php?id=2330">Discuss</a>]</center><br> <a href="./"><span class="red">HOME</span></a> <a href="javascript:history.go(-1)"><span class="red">Back</span></a> <hr> </body>

题解

把约束关系建成差分约束系统,然后就可以跑最长路了。但是spfa已死,所以需要另一种算法。

因为边权只有0和1,所以如果图中存在一个环中有1,那么一定无解。所以tarjan求出SCC后判无解。如果有解一个SCC里面的数肯定相等,这时跑一个DAG上的最长路DP就行了。

时间复杂度\(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;
}

posted on 2019-06-01 16:12  autoint  阅读(162)  评论(0编辑  收藏  举报

导航