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题的更多相关文章

  1. 2014 HDU多校弟六场J题 【模拟斗地主】

    这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216&q ...

  2. [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)

    题目链接:https://ac.nowcoder.com/acm/contest/886/B 题意: 您将获得一个IPv6地址,该地址是128位二进制字符串.请根据以下规则确定其最短的表示: 以十六进 ...

  3. 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. ...

  4. c提高第六次课 文件读取

    一.基本概念1.文件分类 普通文件:存放在硬盘中的文件 设备文件:屏幕.键盘等特殊文件 文本文件:ASCII文件,每个字节存放一个字符的ASCII码,打开文件看到的是文本信息 二进制文件:数据按其在内 ...

  5. Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...

  6. noi.ac 第五场第六场

    t1应该比较水所以我都没看 感觉从思路上来说都不难(比牛客网这可简单多了吧) 第五场 t2: 比较套路的dp f[i]表示考虑前i个数,第i个满足f[i]=i的最大个数 i能从j转移需要满足 j< ...

  7. [比赛|考试]nowcoder NOIPpj组第二场

    nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...

  8. 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)

    第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...

  9. hdu 4548 第六周H题(美素数)

    第六周H题 - 数论,晒素数 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   De ...

随机推荐

  1. bzoj1483: [HNOI2009]梦幻布丁(链表+启发式合并)

    题目大意:一个序列,两种操作. ①把其中的一种数修改成另一种数 ②询问有多少段不同的数如1 2 2 1为3段(1 / 2 2 / 1). 昨晚的BC的C题和这题很类似,于是现学现写居然过了十分开心. ...

  2. linux下,手动切换jdk

    1.首先将自定义的jdk目录安装到alternatives中 seven@ThinkPad:~/srcAndroid/src4..4_r1$ sudo update-alternatives --in ...

  3. poco入门

    源码按照poco.然后看README,进行安装. ./configure make make install #include "Poco/ActiveMethod.h" #inc ...

  4. BZOJ1898: [Zjoi2005]Swamp 沼泽鳄鱼(矩阵乘法)

    1898: [Zjoi2005]Swamp 沼泽鳄鱼 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1898 Description 潘塔 ...

  5. BST POJ - 2309 思维题

    Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, ...

  6. 微服务学习一:idea中springboot集成mybatis

    一直都想学习微服务,这段时间在琢磨这块的内容,个人之前使用eclipse,现在用intellij idea来进行微服务的开发,个人感觉intellij idea比eclipse更简洁更方便,因为int ...

  7. [ssh]ssh系列之一

    1.使用ssh建立sock代理 ssh -D 7070 -f -N user@host -D -f 后台执行 -N 不登陆shell执行

  8. Django请求原理(二)

    1,Web服务器(中间件)收到一个http请求 2,Django在URLconf里查找对应的视图(View)函数来处理http请求 3,视图函数调用相应的数据模型来存取数据.调用相应的模板向用户展示页 ...

  9. C++虚基类的初始化

    #include<iostream> using namespace std; class Base{ public: Base(int sa) { a=sa; cout<<& ...

  10. mysql中设置小数

    decimal Decimal(n,m)表示数值中共有n位数,其中整数n-m位,小数m位.例:decimal(10,6),数值中共有10位数,其中整数占4位,小数占6位. 例:decimal(2,1) ...