感觉这道题读题有点难。。似乎和现实联系的比较密切
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. 架构:template

    ylbtech-架构: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech.cnbl ...

  2. nodejs QueryString模块 详解

    QueryString模块 "QueryString" 模块用于实现URL参数字符串与参数对象的互相转换 此类一共包括4个方法: querystring.stringify(obj ...

  3. 转:深度学习与自然语言处理之五:从RNN到LSTM

    原文地址:http://blog.csdn.net/malefactor/article/details/50436735/ 大纲如下: 1.RNN 2.LSTM 3.GRN 4.Attention ...

  4. NYOJ4——ASCII码排序

    ASCII码排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:2  描述:输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符.  输入:第一行输入一 ...

  5. SCUT - 290 - PARCO的因数游戏 - 博弈论

    https://scut.online/p/290 一个 N 个数的取数游戏,Kaildls 和 Parco 轮流操作,每次操作从 N 个数中取一个数 y 并把他变成 y-x(满足 x | y 且x  ...

  6. thrift配置——windows客户端与linux服务端通信(C++)

    windows客户端: 1.首先要安装boost库 下载源文件 2.安装boost之前先要安装python-3.4.0.amd64,很多地方没有说,弄了很久 3.运行bootstrap.bat 生成b ...

  7. P5168 xtq玩魔塔

    传送门 其实就是板子--只要会克鲁斯卡尔重构树和带修莫队就可以了 这么想着的我就调了将近一个下午-- 思路其实比较清晰,然而码量很大,细节贼多-- 不难看出只在最小生成树上走最优,于是建出克鲁斯卡尔重 ...

  8. UltraEdit - 怎么显示文件标签栏和侧边栏

    显示文件标签栏 view -> views/lists -> open Files Tabs 显示侧边栏 view -> views/lists -> File Tree Vi ...

  9. .Net开发人员必备工具下载

    .Net开发人员必备工具下载   本人亲测下载地址: Win8.1破解工具下载: http://pan.baidu.com/s/1eQf2UiQ 可激活版本 Windows Vista Busines ...

  10. Hibernate Could not obtain transaction-synchronized Session for current thread问题处理

    项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...