uva1439 Exclusive Access 2
感觉这道题读题有点难。。似乎和现实联系的比较密切
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的更多相关文章
- Bus,Exclusive access,memory attribute
指令LDREX,STREX是在armv6中新加的指令,配合AMBA3--AXI中的lock[1:0]信号. 在Atomic Access一节中是这么规定的:ARLOCK[1:0]/AWLOCK[1:0 ...
- ESOURCE_LOCKED - cannot obtain exclusive access to locked queue '2484_0_00163'
早上一运维同事说,一个报盘程序启动的时候报了"ESOURCE_LOCKED - cannot obtain exclusive access to locked queue '2484_0_ ...
- Exclusive access control to a processing resource
A data processing system is provided with multiple processors that share a main memory. Semaphore va ...
- 『Exclusive Access 2 dilworth定理 状压dp』
Exclusive Access 2 Description 给出 N 个点M 条边的无向图,定向得到有向无环图,使得最长路最短. N ≤ 15, M ≤ 100 Input Format 第一行一个 ...
- 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 ...
- bzoj4160: [Neerc2009]Exclusive Access 2
Description 给出 N 个点M 条边的无向图,定向得到有向无环图,使得最长路最短. N ≤ 15, M ≤ 100 Input 第一行一个数M (1≤M≤100). 接下来M行,每行两个大写 ...
- BZOJ.4160.[NEERC2009]Exclusive Access 2(状压DP Dilworth定理)
BZOJ DAG中,根据\(Dilworth\)定理,有 \(最长反链=最小链覆盖\),也有 \(最长链=最小反链划分数-1\)(这个是指最短的最长链?并不是很确定=-=),即把所有点划分成最少的集合 ...
- 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 ...
- 『翻译』Access USB Devices on the Web
https://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web Access USB Devices o ...
随机推荐
- web项目中url-pattern改成'/'后,js、css、图片等静态资源(404)无法访问问题解决办法
感谢http://blog.csdn.net/this_super/article/details/7884383的文章 1.增加静态资源url映射 如Tomcat, Jetty, JBoss, Gl ...
- kvm_虚拟机迁移
virsh domblklist 虚拟机名称 #查看虚拟磁盘文件 一.kvm虚拟机静态迁移 1.静态迁移就是虚拟机在关机状态下,拷贝虚拟机虚拟磁盘文件与配置文件到目标虚拟主机中,实现的迁移. (1)虚 ...
- 利用thrift在c++、java和python之间相互调用
转自:http://blog.csdn.net/andy_yf/article/details/7487384 thrift做为跨语言调用的方案有高效,支持语言较多,成熟等优点:代码侵入较强是其弱点. ...
- PTA 计算平均值
现在为若干组整数分别计算平均值. 已知这些整数的绝对值都小于100,每组整数的数量不少于1个,不大于20个. 输入格式:首先输入K(不小于2,不大于20).接下来每一行输入一组数据(至少有一组数据), ...
- 如何修改hosts文件并生效
hosts文件位置C:\Windows\System32\drivers\etc(可以建立一个.bat 的文件把(start "" C:\Windows\System32\driv ...
- .bashrc等文件中的rc是什么意思
转自: https://blog.csdn.net/u010167269/article/details/52612260 刚刚配置 .bashrc 文件时,突然想,这里面的 rc 是什么意思? 使 ...
- 分布式集群环境下,如何实现session共享四(部署项目测试)
这是分布式集群环境下,如何实现session共享系列的第四篇.在上一篇:分布式集群环境下,如何实现session共享三(环境搭建)中,已经准备好了相关的环境:tomcat.nginx.redis.本篇 ...
- AtCoder Grand Contest 010 F - Tree Game
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...
- D Tree HDU - 4812
https://vjudge.net/problem/HDU-4812 点分就没一道不卡常的? 卡常记录: 1.求逆元忘开longlong 2.把solve中分离各个子树的方法,由“一开始全部加入,处 ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...