感觉这道题读题有点难。。似乎和现实联系的比较密切
1.每个process的两个资源可以顺序反一下
2.p->q,q->s不可以同时进行
p->q,p->s可以

输出最长等待链
输出每个process的资源调用顺序 (注意按输入顺序输出,并不意味着按输入顺序先后执行,只是输出方便看)

把资源看成点,一个process就成了链接两个点的无向边
任务就成了把无向边定向,使其不存在圈,且最长路(也即最长等待链最短)

创造性思维:把结点分成p层,编号为0,1,2...使同层结点间没有边;对任意边u-v,定向位从层编号小的点指向层编号大的点。
则定向后的图肯定没有圈,且最长路所含点数不超过p。所以p越小越好(直观上)

可以证明p取得最小值时,最长路恰好包含p个结点,且这个结果是所有定向方案中最优的。 (证明:从定向方案构造分层图。先把所有路径的起点作为第0层。 (没证!!!))

这样问题转化为结点分层问题。也就是色数问题:将图中结点染成尽量小的颜色,使相邻结点颜色不同。
(色数问题见紫书P286)
O(3^K)k<=15.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
// Here n is the number of resources, m is the number of processes (n in the problem statement)
const int maxn = ;
const int maxm = + ;
int n, m, u[maxm], v[maxm], G[maxn][maxn];
int ind[<<maxn], d[<<maxn], best[<<maxn], label[maxn]; bool independent(int mask) {
for(int i = ; i < maxn; i++) if(mask & (<<i))
for(int j = ; j < maxn; j++) if(mask & (<<j))
if(i != j && G[i][j]) return false;
return true;
} // How many colors are needed to color the set 'mask'
int dp(int mask) {
int& ans = d[mask];
if(ans >= ) return ans;
if(mask == ) return ;
ans = maxn+;
for(int s = mask; s; s = (s-)&mask)
if(ind[s]) {
int v = dp(mask^s) + ;
if(v < ans) { ans = v; best[mask] = s; }
}
return ans;
} // mark the set 'mask' with color c
void mark(int mask, int c) {
for(int i = ; i < maxn; i++)
if(mask & (<<i)) label[i] = c;
} int main() {
while(scanf("%d", &m) == ) {
memset(G, , sizeof(G));
int useful = ;
for(int i = ; i < m; i++) {
char r1[], r2[];
scanf("%s%s", r1, r2);
u[i] = r1[]-'L', v[i] = r2[]-'L';
G[u[i]][v[i]] = ;
useful |= (<<u[i]);
useful |= (<<v[i]);
} // find the independent sets
memset(ind, , sizeof(ind));
for(int s = useful; s; s = (s-)&useful)
if(independent(s)) ind[s] = true; // dp
memset(d, -, sizeof(d));
int ans = dp(useful);
printf("%d\n", ans-); // construct the answer
int s = useful, k = ;
while(s) {
mark(s, k++);
s ^= best[s];
}
for(int i = ; i < m; i++) {
if(label[u[i]] < label[v[i]]) swap(u[i], v[i]);
printf("%c %c\n", 'L'+u[i], 'L'+v[i]);
}
}
return ;
}

uva1439 Exclusive Access 2的更多相关文章

  1. Bus,Exclusive access,memory attribute

    指令LDREX,STREX是在armv6中新加的指令,配合AMBA3--AXI中的lock[1:0]信号. 在Atomic Access一节中是这么规定的:ARLOCK[1:0]/AWLOCK[1:0 ...

  2. ESOURCE_LOCKED - cannot obtain exclusive access to locked queue '2484_0_00163'

    早上一运维同事说,一个报盘程序启动的时候报了"ESOURCE_LOCKED - cannot obtain exclusive access to locked queue '2484_0_ ...

  3. Exclusive access control to a processing resource

    A data processing system is provided with multiple processors that share a main memory. Semaphore va ...

  4. 『Exclusive Access 2 dilworth定理 状压dp』

    Exclusive Access 2 Description 给出 N 个点M 条边的无向图,定向得到有向无环图,使得最长路最短. N ≤ 15, M ≤ 100 Input Format 第一行一个 ...

  5. InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's

    InvalidOperationException: Operations that change non-concurrent collections must have exclusive acc ...

  6. bzoj4160: [Neerc2009]Exclusive Access 2

    Description 给出 N 个点M 条边的无向图,定向得到有向无环图,使得最长路最短. N ≤ 15, M ≤ 100 Input 第一行一个数M (1≤M≤100). 接下来M行,每行两个大写 ...

  7. BZOJ.4160.[NEERC2009]Exclusive Access 2(状压DP Dilworth定理)

    BZOJ DAG中,根据\(Dilworth\)定理,有 \(最长反链=最小链覆盖\),也有 \(最长链=最小反链划分数-1\)(这个是指最短的最长链?并不是很确定=-=),即把所有点划分成最少的集合 ...

  8. embody the data item with the ability to control access to itself

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Such communication needs have long b ...

  9. 『翻译』Access USB Devices on the Web

    https://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web Access USB Devices o ...

随机推荐

  1. web.xml配置之<context-param>

    <context-param>的作用和用法: 1.<context-param>配置是是一组键值对,比如: <context-param>        <p ...

  2. EF提高性能

    实体框架 5 性能注意事项 作者:David Obando.Eric Dettinger 等 发布时间:2012 年 4 月 1.简介 对象关系映射框架是一种在面向对象的应用程序中提供数据访问抽象的便 ...

  3. 【旧文章搬运】Windows句柄分配算法(一)

    原文发表于百度空间,2009-04-04========================================================================== 分析了Wi ...

  4. Oracle的db.properties文件

    转自:https://blog.csdn.net/lssqk/article/details/79133829

  5. Redis的相关命令

    Redis的相关命令 redis程序的命令 /usr/bin/redis-benchmark /usr/bin/redis-check-aof /usr/bin/redis-check-rdb /us ...

  6. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

  7. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时6

    课时6 线性分类器损失函数与最优化(上) 多类SVM损失:这是一个两分类支持向量机的泛化 SVM损失计算了所有不正确的例子,将所有不正确的类别的评分,与正确类别的评分之差加1,将得到的数值与0作比较, ...

  8. bzoj 4199: [Noi2015]品酒大会【后缀数组+单调栈+并查集】

    用SA求出height数组,然后发现每个height值都有一个贡献区间(因为点对之间要依次取min) 用单调栈处理出区间,第一问就做完了 然后用并查集维护每个点的贡献(?),从大到小枚举height, ...

  9. P5166 xtq的口令

    传送门 这题要是搞懂在干什么其实不难(虽然某个花了几个小时才搞明白的家伙似乎没资格这么说--) 假设所有人都没有听到老师的命令,我们从左到右考虑,对于当前的人,如果它没有观察者,那么肯定要让它听到老师 ...

  10. Luogu P2170选学霸【并查集+背包】By cellur925

    题目传送门 开始看到本题完全认为就是个彻头彻尾的并查集,只要把实力相当的人都并到一个集合中,最后再找一共有多少联通块即可. 后来发现这是大错特错的qwq.因为选了一个集合中的某人,那这个集合中所有人就 ...