hihocoder #1136 : Professor Q's Software 数组解法
来源:互联网 发布:企业组织结构优化 编辑:程序博客网 时间:2024/06/05 06:17
topic link:hihocoder#1136
这题在拓扑排序的基础上加了一点变化,那就是在结点(即题中的module)之间增加了一个信号,用信号将有向图连接起来,增大了构造图的难度,我为了加快速度采用了数组存储图结构。
主要维护以下数组:
1.module
1.sig:sig[i]表示接受信号为i的module链表的表头。我把所有signal的序号加1,这样空出序号0作为结束条件。
2.ind: ind[i]表示module[i]的入度(in-dgree,即有多少个信号指向它)。
3.init:表示professor的初始化信号。
在理解这几个数组的意思后就可以看代码了,好了,废话不多说,上代码:
#include<iostream>#include<queue>#include<memory.h>using namespace std;#define N 100005#define modulo 142857 int zeros[3] = { 0, 0, 0 };int T,n, m,tot, sig[N], ind[N], init[N];struct module{ int next, cnt, e[3]; module(int a = 0, int *c = zeros) :next(a), cnt(0){ int i = 3; while (i--)e[i] = *(c + i); }};void add(module *mod,int s, int* e){ for (int i = 0; i < 3; i++)e[i]++; mod[++tot] = module(sig[s + 1], e), sig[s + 1] = tot;}void solve(module *mod){ queue<int> q; for (int i = 1; i <= n; i++){ for (int j = 0; j < 3; j++){ for (int k = sig[mod[i].e[j]]; k; k = mod[k].next){ ind[k]++; } } } for (int i = 0; i < m; i++){ for (int j = sig[init[i]]; j; j = mod[j].next) mod[j].cnt++; } for (int i = 1; i <= n; i++)if(!ind[i])q.push(i); while (!q.empty()){ int i = q.front(); q.pop(); for (int j = 0; j < 3; j++){ for (int k = sig[mod[i].e[j]]; k; k = mod[k].next){ mod[k].cnt = (mod[k].cnt + mod[i].cnt) % modulo; ind[k]--; if (!ind[k])q.push(k); } } } for (int i = 1; i <= n; i++)cout << mod[i].cnt << ' '; cout << endl;}int main(){ ios::sync_with_stdio(false); cin >> T; while (T--){ cin >> n >> m; module *mod = new module[n+1]; memset(sig, 0, sizeof(sig)); memset(ind, 0, sizeof(ind)); memset(init, 0, sizeof(init)); tot = 0; for (int i = 0; i < m; i++){ cin >> init[i]; init[i]++; } for (int i = 0; i < n; i++){ int s, k, e[3] = { -1,-1,-1 }; cin >> s >> k; for (int j = 0; j < k; j++)cin >> e[j]; add(mod,s, e); } solve(mod); } return 0;}
0 0
- hihocoder #1136 : Professor Q's Software 数组解法
- hihocoder #1136 : Professor Q's Software
- 微软笔试题 HihoCoder#1136: Professor Q's Software 题解
- hihoCoder 1136 Professor Q's Software 微软2016校园招聘在线笔试
- HiHocoder #1136 : Professor Q's Software 微软2016校园招聘在线笔试 【拓扑排序+DP】
- Professor Q's Software Java版本
- 微软笔试-Professor Q's Software
- Professor Q's Software---拓扑排序
- 微软2015实习第二题Professor Q's Software
- 2016微软实习在线笔试题 - Professor Q's Software
- 微软2016校园招聘在线笔试 - 第二题 Professor Q's Software
- 微软线上笔试-2015-4-3(1,2题) Magic Box && Professor Q's Software
- Professor Q's Software——邻接表实现拓扑排序
- 题目2 : Professor Q's Software【微软2016校园招聘在线笔试】
- 微软2016校园招聘笔试(第一场) Professor Q's Software & Recruitment
- A. Professor GukiZ's Robot
- D-Professor GukiZ's Robot
- CF 70D Professor's task题解
- 如何在GITHUB上FORK一个项目来贡献代码以及同步原作者的修改
- 正则表达式
- CF 6A Triangle (判断能否构成三角形)
- 前端学习方法总结
- java基础之多态
- hihocoder #1136 : Professor Q's Software 数组解法
- Android之内存缓存——LruCache的使用及原理
- 10大基础实用算法及其讲解
- Avoid on a Thank-You Email
- Reverse Linked List
- 柴俊理金:6、7午夜黄金,宁贵沥青操作策略
- 工作九年
- linux2.4内核 path_walk流程简析
- HDU4699Editor