中国大学生计算机系统与程序设计竞赛 CCF-CCSP-2017 串行调度(serial)
2024-08-27 14:33:14
串行调度(serial)
除等价条件, 根据题意设置限制条件,然后求字典序最小拓扑序。
简洁版
#include<bits/stdc++.h>
using namespace std;
const int N=2e4+;
const int M=2e4+;
const int E=8e5+;
template <typename T>
inline void read(T &x){
T f=;char ch=getchar();x=;
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
x*=f;
}
vector<int>lastR[N];int lastW[N];
bitset<M>d[M];
int n,m,pcnt,qcnt,tot,ind[M],to[E],nxt[E],head[M];
inline void add(int x,int y){
if(!x||x==y) return ;
ind[y]++;
to[++tot]=y;nxt[tot]=head[x];head[x]=tot;
}
inline void init(){
read(n);read(m);read(pcnt);read(qcnt);
for(int i=,op,xk,tk;i<=pcnt;i++){
read(op);read(xk);read(tk);
if(op){
if(!lastR[xk].empty()){
for(auto &i:lastR[xk]) add(i,tk);
lastR[xk].clear();
}
else{
add(lastW[xk],tk);
}
lastW[xk]=tk;
}
else{
add(lastW[xk],tk);
lastR[xk].push_back(tk);
}
}
}
priority_queue<int,vector<int>,greater<int> >q;
inline void topo(){
d[]=;
for(int i=;i<=m;i++) d[i]=d[i-]<<;
for(int i=;i<=m;i++) if(!ind[i]) q.push(i);
for(int i=;i<=m;i++){
int x=q.top();q.pop();
printf("%d ",x);
for(int j=head[x];j;j=nxt[j]){
if(!--ind[to[j]]){
q.push(to[j]);
}
d[to[j]]|=d[x];
}
}
puts("");
for(int i=qcnt,x,y;i;i--){
read(x);read(y);
puts(!d[x].test(y)?"YES":"NO");
}
}
int main(){
init();
topo();
return ;
}
纯手写bitset
#include<queue>
#include<vector>
#include<cstdio>
using std::vector;
using std::priority_queue;
const int N=2e4+;
const int M=2e4+;
const int E=8e5+;
template <typename T>
inline void read(T &x){
T f=;char ch=getchar();x=;
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
x*=f;
}
vector<int>lastR[N];int lastW[N];
struct BitSet{//模拟bitset
// unsigned int 以压缩32位
// unsigned long long 可以压缩64位
const static int N = M / + ;
unsigned data[N];
unsigned& operator[](int a){
return data[a];
}
const unsigned& operator[](int a)const{
return data[a];
}
bool get(int a){
return (data[a >> ] >> (a & )) & ;
}
void set(int a){
data[a >> ] |= << (a & );
}
void unset(int a){
data[a >> ] &= ~( << (a & ));
}
BitSet& operator|=(const BitSet& a){
for(int i = ; i < N; i++)
data[i] |= a[i];
return *this;
}
}used[M];
int n,m,pcnt,qcnt,tot,ind[M],to[E],nxt[E],head[M];
inline void add(int x,int y){
if(!x||x==y) return ;
ind[y]++;
to[++tot]=y;nxt[tot]=head[x];head[x]=tot;
}
inline void init(){
read(n);read(m);read(pcnt);read(qcnt);
for(int i=,op,xk,tk;i<=pcnt;i++){
read(op);read(xk);read(tk);
if(op){
if(!lastR[xk].empty()){
for(auto &i:lastR[xk]) add(i,tk);
lastR[xk].clear();
}
else{
add(lastW[xk],tk);
}
lastW[xk]=tk;
}
else{
add(lastW[xk],tk);
lastR[xk].push_back(tk);
}
}
}
priority_queue<int,vector<int>,std::greater<int> >q;
inline void topo(){
for(int i=;i<=m;i++) used[i].set(i);
for(int i=;i<=m;i++) if(!ind[i]) q.push(i);
for(int i=;i<=m;i++){
int x=q.top();q.pop();
printf("%d ",x);
for(int j=head[x];j;j=nxt[j]){
if(!--ind[to[j]]){
q.push(to[j]);
}
used[to[j]]|=used[x];
}
}
puts("");
for(int i=qcnt,x,y;i;i--){
read(x);read(y);
puts(!used[x].get(y)?"YES":"NO");
}
}
int main(){
init();
topo();
return ;
}
最新文章
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(6)-Unity 依赖注入
- oracle 11g RAC安装节点二执行结果错误CRS-5005: IP Address: 192.168.1.24 is already in use in the network
- 数据库知识整理<;七>;
- Twitter算法面试题详解(Java实现)
- HTML CSS 中DIV内容居中汇总
- BZOJ3924 [Zjoi2015]幻想乡战略游戏
- Siri开源了,33%的开发者持疑虑态度,你呢?
- jquery 下拉菜单
- SharePoint自动化系列——Site/Web/List级别的导航菜单
- 如何区分JS中的this?!
- 《Linear Algebra and Its Applications》-chaper6-正交性和最小二乘法- 格拉姆-施密特方法
- ASP.NET MVC4 + Highcharts生成报表
- HDU 4983 Goffi and GCD
- hdu1334-Perfect Cubes
- Spring Boot初探之数据库访问
- 侯哥的Python分享
- [二十二]JavaIO之LineNumberReader
- centos7 yum安装nginx
- ajax请求成功 但是被error拦截
- idea快捷键列表