感觉这道题读题有点难。。似乎和现实联系的比较密切
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. 让ie支持css3的一些htc文件

    1. Dean Edwards的IE7.js (以及 IE8.js, IE9.js)这个玩意估计是试图让IE支持CSS3属性的鼻祖,还算蛮强大,就是性能开销较大,要解析很多文件脚本,给DOM添加大量的 ...

  2. unix网络编程环境配置程序运行

    1 查看ubuntu版本 cat /etc/issue 2 查看版本 cat /proc/version 3 下载 你可以通过其他方式下载 这里下载好了 文件名为unpv13e 4 开始配置 (1)c ...

  3. linux中用无名管道进行文件的读写

    1管道是什么: 水管子大家知道,有两端,在此一端用来读一端用来写,其中一端的输出作为另外一端的输入. 2 函数原型 int pipe(int pipefd[2]);//参数中分别代表的两端 3 例子: ...

  4. 洛谷 - P2335 - 位图 - 简单dp

    https://www.luogu.org/problemnew/show/P2335 假如我们使用dp的话,每次求出一个点的左上方.右上方.左下方.右下方的最近的白点的距离.那么只是n²的复杂度.这 ...

  5. 利用thrift在c++、java和python之间相互调用

    转自:http://blog.csdn.net/andy_yf/article/details/7487384 thrift做为跨语言调用的方案有高效,支持语言较多,成熟等优点:代码侵入较强是其弱点. ...

  6. poj1979【基础bfs/dfs】

    挑战习题搜索-1 题意: 给定起点,然后求一个可以到达的数量,位置"."都可以走.每次应该是上下左右都可以走. 思路: 这题应该DFS更好写,但是BFS也可以写吧. 好久没写了- ...

  7. poj1724【最短路】

    题意: 给出n个城市,然后给出m条单向路,给出了每条路的距离和花费,问一个人有k coins,在不超过money的情况下从1到n最短路径路径. 思路: 我相信很多人在上面那道题的影响下,肯定会想想,在 ...

  8. JAVA多线程(四) Executor并发框架向RabbitMQ推送消息

    github代码地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...

  9. 黑客攻防技术宝典web实战篇:定制攻击自动化习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 指出使用自动技巧在应用程序中枚举标识符时用到的 3 个标识符“触点”. (a) HTTP ...

  10. 网络流24题 一句话题解(updating)

      搭配飞行员问题   最简单的一道题 就是一个二分图匹配   太空飞行计划   最大权闭合子图 什么叫"最大权闭合子图"呢? 就是给定一个有向图,在里面选择一个点集,使得点集中的 ...