感觉这道题读题有点难。。似乎和现实联系的比较密切
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. Ruby nokogiri 解析xml的简单实例

    require 'nokogiri'XML_FILE = "C:\\Users\\chenpassion\\Desktop\\20130806.xml"xml = Nokogiri ...

  2. apache日志信息详解

     一.访问日志的格式 Apache内建了记录服务器活动的功能,这就是它的日志功能.下文详细介绍Apache的访问日志.错误日志.以及如何分析日志数据,如何定制Apache日志,如何从日志数据生成统计报 ...

  3. 使用Code First 迁移发布asp.net mvc 程序

    通过Code First 迁移发布asp.net mvc应用程序,与在visual studio 程序包控制器管理台中运行Add-Migration ,Update-Database 一致. 在发布的 ...

  4. Drools6:规则互斥

    在Drools中,有的时候会出现一种情况,一个事实Fact,满足了两个规则. 比如Fact的POJO是下面的 public class Message { public static final in ...

  5. Codeforces404C【构造】

    题意: 一个图有n个点,每一个点最多连接k条直线,给出多有起点到终点的距离,没有环,不能输出重边,输出所有有连接的单向边 思路: 就是简单想-不知道怎么说了,画个图,我们建边,那么距离是 i 就是连距 ...

  6. 瞎比比系列---1st

    A - 项目管理HDU4858 /* 题意: 这个项目有n个节点, 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 0的时候:接下来两个数u v表示给项目u的能量值加上v: 1的时候: 这 ...

  7. bzoj 5496: [2019省队联测]字符串问题【SAM+拓扑】

    有一个想法就是暴力建图,把每个A向有和他相连的B前缀的A,然后拓扑一下,这样的图是n^2的: 考虑优化建图,因为大部分数据结构都是处理后缀的,所以把串反过来,题目中要求的前缀B就变成了后缀B 建立SA ...

  8. bzoj1485: [HNOI2009]有趣的数列(Catalan数)

    1485: [HNOI2009]有趣的数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2105  Solved: 1117[Submit][Stat ...

  9. 黑客攻防技术宝典web实战篇:攻击本地编译型应用程序习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 如果不采用特殊的防御措施,为什么栈缓冲区溢出比堆溢出更容易被攻击者利用? 利用基于栈的溢出 ...

  10. LuoguP2115 [USACO14MAR]破坏Sabotage【二分答案】By cellur925

    本来是想找一道生成树的题做的...结果被洛咕的标签骗到了这题...结果是二分答案与生成树一点mao关系都没有.... 题目大意:给你一个序列,请你删去某一个$l~r$区间的值($2<=i< ...