我是真的不会写差分约束啊呜呜呜……

BZOJ 2788被权限了。

首先对于第一个限制$x + 1 = y$,可以转化成$x + 1 \leq y \leq x + 1$,

所以连一条$(y, x, -1)$,再连一条$(x, y, 1)$。

第二个状态即为$x \leq y$,连边$(y, x, 0)$。

如果有负环就无解了。

发现在这个图中,每一个强连通分量都互相不干扰,我们可以缩点找出所有的强连通分量,然后找到里面的最长路$ + 1$累加到答案中去。

时间复杂度$O(能过)$。

感觉$POI$的数据挺满的,我的代码跑得挺慢的。

Code:

#include <cstdio>
#include <cstring>
using namespace std; const int N = ;
const int M = 3e5 + ; int n, m1, m2, tot = , head[N], f[N][N];
int ans = , scc = , bel[N], dfsc = , dfn[N], low[N], top = , sta[N];
int sCnt, s[N];
bool vis[N]; struct Edge {
int to, nxt;
} e[M]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline void chkMin(int &x, int y) {
if(y < x) x = y;
} inline int min(int x, int y) {
return x > y ? y : x;
} inline void chkMax(int &x, int y) {
if(y > x) x = y;
} inline void solve() {
int res = ;
for(int i = ; i <= sCnt; i++)
for(int j = ; j <= sCnt; j++) {
int x = s[i], y = s[j];
chkMax(res, f[x][y]);
}
ans += res + ;
} void tarjan(int x) {
dfn[x] = low[x] = ++dfsc;
sta[++top] = x, vis[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if(vis[y]) low[x] = min(low[x], dfn[y]);
} if(low[x] == dfn[x]) {
++scc; sCnt = ;
for(; sta[top + ] != x; --top) {
vis[sta[top]] = ;
bel[sta[top]] = scc;
s[++sCnt] = sta[top];
}
solve();
}
} int main() {
read(n), read(m1), read(m2);
memset(f, 0x3f, sizeof(f));
for(int x, y, i = ; i <= m1; i++) {
read(x), read(y);
add(x, y), add(y, x);
chkMin(f[x][y], ), chkMin(f[y][x], -);
}
for(int x, y, i = ; i <= m2; i++) {
read(x), read(y);
add(y, x);
chkMin(f[y][x], );
} for(int i = ; i <= n; i++) f[i][i] = ;
for(int k = ; k <= n; k++)
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
chkMin(f[i][j], f[i][k] + f[k][j]); for(int i = ; i <= n; i++)
if(f[i][i] < ) {
puts("NIE");
return ;
} for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i); printf("%d\n", ans);
return ;
}

Luogu 3530 [POI2012]FES-Festival的更多相关文章

  1. Luogu 3537 [POI2012]SZA-Cloakroom

    背包. 首先考虑将所有询问离线按照$m$从小到大排序,然后把所有物品按照$a$从小到大排序,对于每一个询问不断加入物品. 设$f_i$表示在组成容量为$i$的背包的所有方案中$b$最小的一个物品的最大 ...

  2. Luogu P3546 [POI2012]PRE-Prefixuffix 神奇的递推+哈希

    设$f[i]$表示切掉前$i$位和后$i$位后,即剩下$s[i+1]到s[n-i]$,的公共前后缀长度.此时我们发现,$f[i-1]$相对于$f[i]$少切了两个$char$,所以有$f[i-1]\l ...

  3. LUOGU P3539 [POI2012]ROZ-Fibonacci Representation

    传送门 解题思路 打了个表发现每次x只会被比x大的第一个fab或比x小的第一个fab表示,就直接写了个爆搜骗分,结果过了.. 代码 #include<iostream> #include& ...

  4. [BZOJ2788][Poi2012]Festival

    2788: [Poi2012]Festival Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 187  Solved: 91[Submit][Statu ...

  5. [Poi2012]Festival 题解

    [Poi2012]Festival 时间限制: 1 Sec  内存限制: 64 MB 题目描述 有n个正整数X1,X2,...,Xn,再给出m1+m2个限制条件,限制分为两类: 1. 给出a,b (1 ...

  6. [Poi2012]Festival 差分约束+tarjan

    差分约束建图,发现要在每个联通块里求最长路,600,直接O(n3) floyed #include<cstdio> #include<cstring> #include< ...

  7. luogu P4744 [Wind Festival]Iron Man

    再次感谢题解区大佬的指点 规定\(pre[i]\)表示前缀\(i\)的前缀和,\(sum[i][j]\)表示区间\([i,j]\)之和 令\(f[i][j]\)表示前i个数选出j段的最大值,\(g[i ...

  8. [POI2012]Festival

    题目大意: 有$n$个正整数$x_1,x_2,\ldots,x_n$,再给出一些限制条件,限制条件分为两类: 1.给出$A,B$,要求满足$X_A+1=X_B$: 2.给出$C,D$,要求满足$X_C ...

  9. bzoj 2788 [Poi2012]Festival 差分约束+tarjan+floyd

    题目大意 有n个正整数X1,X2,...,Xn,再给出m1+m2个限制条件,限制分为两类: 1.给出a,b (1<=a,b<=n),要求满足Xa + 1 = Xb 2.给出c,d (1&l ...

随机推荐

  1. Collection集合存储自定义对象练习

    public class Student { private String name; private int age; public Student() { super(); } public St ...

  2. InnoDB参数详解

    1.查询5.5版本的InnoDB参数并注释:[root@localhost etc]# grep -i innodb my.cnf; t_innodb; otherwise, slaves may d ...

  3. Leetcode Longest Uncommon Subsequence I

    原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-i/#/description 题目: Given a group ...

  4. CF 949C Data Center Maintenance——思路+SCC

    题目:http://codeforces.com/contest/949/problem/C 可以想到可能是每组c有连边的可能. 但别直接给c1.c2连边,那样之后会变得很不好做. 可以把一些限制放在 ...

  5. CentOS7下Supervisor安装与配置

    Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统 ...

  6. docker镜像的导入和导出

    启动命令 docker run -d -p 3000:80 twang2218/gitlab-ce-zh:9.0.3 docker run -d -p 8080:80 gitlab/gitlab-ce ...

  7. MySQL 5.7.10 自动备份、自动清理旧备份集(转)

    1,mysqldump备份脚本 备份脚本为,里面有几个需要注意的参数: (1)--master-data=2 :这个参数可以在搭建从库的时候,记录当前备份的复制点信息. (2)--extended-i ...

  8. commandLink/commandButton/ajax backing bean action/listener method not invoked (转)

    Whenever an UICommand component fails to invoke the associated action method or an UIInputelement fa ...

  9. 2011年浙大:Graduate Admission

    题目描述: It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 app ...

  10. 32位模式下C/C++程序可用最大内存

    关于32位程序申请大内存问题(1.6G). 我在win7 64系统上面测试Visual studio 10 int* Test=new int[1024*1024*200]; int* Test2=n ...