nowcoder 提高第六场A题
Solution
60分
因为所有的字母要么全相同要么全不同, 所以两条路径比较字典序只需要比较第一条边就可以, 于是建反图, 在反图上按拓扑序转移就可以.
因为有环, 所以拓扑完入度还是不为0的点答案为Infinity.
#include <queue>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
const int N = 1e6 + 7;
const long long mod = 998244353;
struct Edge {
int v, c; Edge* nxt;
Edge(int _, int __, Edge* ___) : v(_), c(__), nxt(___) { }
};
Edge* head[N];
int du[N];
#define AddEdge(u, v, c) head[u] = new Edge(v, c, head[u])
int vis[N], dis[N], res[N], path[N], len[N];
int main () {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i += 1) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
AddEdge(v, u, c), du[u] += 1;
}
std:: queue<int> Que;
for (int i = 1; i <= n; i += 1)
if (not du[i]) Que.push(i), vis[i] = true;
while (not Que.empty()) {
int u = Que.front(); Que.pop();
for (auto edge = head[u]; edge; edge = edge->nxt) {
int v = edge->v, c = edge->c;
du[v] -= 1;
if (not du[v]) Que.push(v), vis[v] = 1;
if (dis[u] + 1 > dis[v])
dis[v] = dis[u] + 1, path[v] = u, len[v] = c,
res[v] = (res[u] * 29ll % mod + c * 29ll % mod) % mod;
if (dis[u] + 1 == dis[v] and c < len[v])
path[v] = u, len[v] = c,
res[v] = (res[u] * 29ll % mod + c * 29ll % mod) % mod;
}
}
for (int i = 1; i <= n; i += 1)
vis[i] ? printf("%d\n", res[i]) : printf("Infinity\n");
return 0;
}
100分-倍增
剩下的40分难点在于比较路径字典序的大小.
如何找出两条路径第一个不同的位置?
可以用倍增, 具体就是预处理出点u向后走\(1\)个位置路径的hash值, 向后走\(2\)个位置路径的hash值, 向后走\(2^k\)个位置路径的hash值.
考试的时候真的是没有想到呀, 于是就随便写了个玄学复杂度的东西来判路径,
真TM难对拍, 我造了树还把边权设为\(1, 2\)才拍出错误, 最后发现时递推了向后走\(2^i\)个位置的hash值但是没有递推向后走\(2^i\)会走到哪个点.
努力卡常才卡到1.3秒+.
#include <ctype.h>
#include <math.h>
#include <queue>
#include <string>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
const int N = 1e6 + 7;
const long long mod = 998244353;
int L = 20;
using std:: string;
const int BUFSIZE = 1e6;
char inbuf[BUFSIZE], outbuf[BUFSIZE], *si = inbuf, *ti = inbuf, *so = outbuf, *to = outbuf + BUFSIZE;
struct FastIO {
~FastIO() {fwrite(outbuf, 1, so - outbuf, stdout);}
#define gc() (si==ti&&(ti=inbuf+fread(si=inbuf,1,BUFSIZE,stdin),si==ti)?EOF:*si++)
#define pc(c) (so==to?fwrite(outbuf,1,BUFSIZE,stdout),so=outbuf,*so++=c:*so++=c)
inline int rd() {
char c, p = 0; int w;
while (not isdigit(c = gc()));
for (w = c & 15; isdigit(c = gc()); w = w * 10 + (c & 15));
return w;
}
template<typename T>inline void print(T w) {
static char s[20]; int top = 0;
if (w < 0)pc('-'), w = -w; if (w == 0)pc('0');
for (top = 0; w; w /= 10)s[++top] = w % 10;
while (top)pc(s[top--] | '0');
}
int putstr(const char* u) {
int l = strlen(u);
for (int i = 0; i < l; i += 1) pc(u[i]); return 0;
}
} io;
#define read io.rd
struct Edge {
int v, c; Edge* nxt;
Edge() {}
Edge(int _, int __, Edge* ___) : v(_), c(__), nxt(___) { }
} pool[N];
Edge* head[N];
int du[N];
int cnt;
#define new_Edge(a, b, c) (pool[cnt] = Edge(a, b, c), &pool[cnt++])
#define AddEdge(u, v, c) du[v] += 1, head[u] = new Edge(v, c, head[u])
int vis[N], dis[N], res[N], path[N], len[N];
int hash[N][21], np[N][21];
int Pow[21], que[N];
bool Cmp(int u, int v) {
for (int i = L; ~i; i -= 1)
if (hash[u][i] == hash[v][i])
u = np[u][i], v = np[v][i];
return len[u] < len[v];
}
int main () {
int n = read(), m = read(); L = std:: __lg(m);
for (int i = 0, u, v, c; i < m; i += 1) {
u = read(), v = read(), c = read();
AddEdge(v, u, c);
}
Pow[0] = 29;
for (int i = 1; i <= L; i += 1)
Pow[i] = (1ll * Pow[i - 1] * Pow[i - 1]) % mod;
int h = 0;
for (int i = 1; i <= n; i += 1)
if (not du[i]) que[++h] = i, vis[i] = true;
while (h) {
int u = que[h]; h -= 1;
hash[u][0] = len[u], np[u][0] = path[u];
for (int i = 1; i <= L; i += 1)
np[u][i] = np[np[u][i - 1]][i - 1];
for (int i = 1; i <= L; i += 1)
hash[u][i] = (hash[u][i - 1] + hash[np[u][i - 1]][i - 1] * Pow[i - 1] % mod) % mod;
for (auto edge = head[u]; edge; edge = edge->nxt) {
int v = edge->v, c = edge->c;
du[v] -= 1;
if (not du[v]) que[++h] = v, vis[v] = 1;
if (dis[u] + 1 == dis[v])
if ((c == len[v] and Cmp(u, path[v])) or c < len[v])
path[v] = u, len[v] = c,
res[v] = (res[u] * 29ll % mod + c * 29ll % mod) % mod;
if (dis[u] + 1 > dis[v])
dis[v] = dis[u] + 1, path[v] = u, len[v] = c,
res[v] = (res[u] * 29ll % mod + c * 29ll % mod) % mod;
}
}
for (int i = 1; i <= n; i += 1)
vis[i] ? io.print(res[i]), pc('\n') : io.putstr("Infinity\n");
return 0;
}
100-分层图排序
nowcoder 提高第六场A题的更多相关文章
- 2014 HDU多校弟六场J题 【模拟斗地主】
这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216&q ...
- [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)
题目链接:https://ac.nowcoder.com/acm/contest/886/B 题意: 您将获得一个IPv6地址,该地址是128位二进制字符串.请根据以下规则确定其最短的表示: 以十六进 ...
- HDU 4923 Room and Moor (多校第六场C题) 单调栈
Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. ...
- c提高第六次课 文件读取
一.基本概念1.文件分类 普通文件:存放在硬盘中的文件 设备文件:屏幕.键盘等特殊文件 文本文件:ASCII文件,每个字节存放一个字符的ASCII码,打开文件看到的是文本信息 二进制文件:数据按其在内 ...
- Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...
- noi.ac 第五场第六场
t1应该比较水所以我都没看 感觉从思路上来说都不难(比牛客网这可简单多了吧) 第五场 t2: 比较套路的dp f[i]表示考虑前i个数,第i个满足f[i]=i的最大个数 i能从j转移需要满足 j< ...
- [比赛|考试]nowcoder NOIPpj组第二场
nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...
- 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)
第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...
- hdu 4548 第六周H题(美素数)
第六周H题 - 数论,晒素数 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
随机推荐
- [SHOI2012]回家的路 最短路
---题面--- 题解: 吐槽:找了好久的错,换了n种方法,重构一次代码,,,, 最后发现,,, 数组开小了,其实一开始尝试开大了数组,但唯独没有尝试开大手写队列的数组.... 思路: 有两种方法,这 ...
- 51NOD 1594:Gcd and Phi——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1594 参考及详细推导:http://www.cnblogs.com/ri ...
- cf 460 E. Congruence Equation 数学题
cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...
- ES6 Set,WeakSet,Map,WeakMap
1. Set Set是一个集合,里面的值都是唯一的,没有重复的.Set中可以是任何数据类型,并且添加数据时会进行严格比较,重复数据无法加入. 2. WeakSet 弱引用Set.只能存储对象,不能存储 ...
- NOIP2016Day1T2天天爱跑步(LCA+桶)
据说是今年NOIP最难一题了...我还记得当时满怀期待心情点开Day1的题发现T2就不会了于是怀疑人生良久... 啊好像很多大爷都是用线段树合并写的,我怎么什么数据结构都不会啊呜呜呜... 题目大意就 ...
- 【learning】杜教筛求欧拉函数前缀和
我们考虑利用\(\sum\limits_{d|n}\varphi(d)=n\)这一性质来处理这个问题 设\(f(n)=\sum\limits_{i=1}^{n}\varphi(i)\) 那么我们可以得 ...
- HashMap & SparseArray & ArrayMap 简单说明
HashMap 使用有限一维拉链数组存储结构,鉴于所用Entry结构{key, value, nextExtry},Key的hash值用于取余获得所属的数组行下标,通过链表方式顺序存放所有余数相同的各 ...
- 2018 BAT最新《前端必考面试题》
2018 BAT最新<前端必考面试题> 1.Doctype作用? 严格模式与混杂模式如何区分?它们有何意义? (1). 声明位于文档中的最前面,处于 标签之前.告知浏览器的解析器,用什么文 ...
- phalcon安装
参考网站:https://docs.phalconphp.com/zh/latest/reference/tools.html (中文版)cento6.5环境安装:cd ~mkdir phalconc ...
- STL之五:set/multiset用法详解
集合 转载于:http://blog.csdn.net/longshengguoji/article/details/8546286 使用set或multiset之前,必须加入头文件<set&g ...