P3530 [POI2012]FES-Festival [差分约束][tarjan][floyed]

10 篇文章 0 订阅
6 篇文章 0 订阅

传送门

题解原网 https://blog.csdn.net/qty2001/article/details/77133189 

图论的一道好题 

#include<bits/stdc++.h>
#define N 605
#define M 200050
#define inf 0x7fffffff
using namespace std;
int first[N],next[M],to[M],w[M],tot;
int dis[N][N],n,m1,m2,ans; vector<int> scc[N];
int dfn[N],low[N],sta[N],insta[N],top,sign,sum;
void add(int x,int y,int z){
	next[++tot]=first[x],first[x]=tot,to[tot]=y,w[tot]=z;
}
void dfs(int u){
	dfn[u]=low[u]=++sign;
	sta[++top]=u,insta[u]=1;
	for(int i=first[u];i;i=next[i]){
		int t=to[i];
		if(!dfn[t]) dfs(t),low[u]=min(low[u],low[t]);
		else if(insta[t]&&dfn[t]<low[u]) low[u]=dfn[t];
	}
	if(low[u]==dfn[u]){
		sum++; do{
			insta[sta[top]]=0;
			scc[sum].push_back(sta[top]);
		}while(sta[top--]!=u);
	}
}
int main(){
	scanf("%d%d%d",&n,&m1,&m2);
	for(int i=1;i<=N-1;i++) for(int j=1;j<=N-1;j++) dis[i][j]=-inf;
	for(int i=1;i<=m1;i++){
		int x,y; scanf("%d%d",&x,&y);
		add(x,y,1),add(y,x,-1); 
		dis[x][y]=max(dis[x][y],1),dis[y][x]=max(dis[y][x],-1);
	}
	for(int i=1;i<=m2;i++){
		int x,y; scanf("%d%d",&x,&y);
		add(x,y,0); dis[x][y]=max(dis[x][y],0);
	}
	for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i);
	for(int now=1;now<=sum;now++){
		int siz = scc[now].size();
		for(int k=0;k<siz;k++){ int c = scc[now][k];
			for(int i=0;i<siz;i++){ int a = scc[now][i];
				for(int j=0;j<siz;j++){ int b = scc[now][j];
					if(dis[a][c]==-inf || dis[c][b]==-inf) continue;
					dis[a][b] = max(dis[a][b],dis[a][c]+dis[c][b]);
				}
			}
		}
		int tmp=0;
		for(int i=0;i<siz;i++){
			for(int j=0;j<siz;j++){
				int a = scc[now][i],b = scc[now][j];
				if(dis[a][b]!=-inf) tmp=max(tmp,abs(dis[a][b]));
			}
		} ans += tmp+1;
		for(int i=0;i<siz;i++){
			int a = scc[now][i]; 
			if(dis[a][a]>0) {printf("NIE"); return 0;}
		}
	} printf("%d",ans); return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FSYo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值