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 ...
随机推荐
- Android中WebView的跨域漏洞分析和应用被克隆问题情景还原(免Root获取应用沙盒数据)
一.前言 去年年底支付宝的被克隆漏洞被爆出,无独有偶就是腾讯干的,其实真正了解这个事件之后会发现,感觉是针对支付宝.因为这个漏洞找出肯定花费了很大劲,主要是因为支付宝的特殊业务需要开启了WebView ...
- POJ2074:Line of Sight——题解
http://poj.org/problem?id=2074 题目大意:(下面的线段都与x轴平行)给两条线段,一个点在其中一条线段看另一条线段,但是中间有很多线段阻挡视线.求在线段上最大连续区间使得在 ...
- [bzoj] 2657 ZJOI2012 旅游 || bfs
原题 题意: 一个多边形,三角剖分,求一条对角线最多能经过多少三角形 题解: 因为不涉及坐标之类的,所以根几何肯定一点关系都没有. 我们会发现,对于有共边的两个三角形,可以被同一条线穿过,而这就相当于 ...
- 微软TTS语音引擎编程入门
原文链接地址:http://www.jizhuomi.com/software/135.html 我们都使用过一些某某词霸的英语学习工具软件,它们大多都有朗读的功能,其实这就是利用的Windows ...
- 项目管理---git----快速使用git笔记(七)------coding.net项目管理多人操作的流程规范--合并代码审核
我们在前面已经介绍了coding.net和本地git的基本用法. 但是多人协作开发时情况会复杂得多,所以我们最好有一些规范来保证项目多人开发顺利进行. 比如说 规范一 master代码分支 需要开启 ...
- Codeforces 864E Fire(背包DP)
背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...
- JQuery学习四(过滤选择器)
:first选择第一个元素.$(“div:first”)进行选择第一个<div> :last 选择最后一个最后一个元素 $("div:last")选取最后一个<d ...
- python读书笔记-《A Byte of Python》中文第三版后半部分
编辑器:windows,linux 不要用notepad,缩进糟糕 -------------- 5.18缩进 同一层次的语句必须有相同的缩进.每一组这样的语句称为一个块. i = 5 2 prin ...
- [Jenkins 新插件] 兼容阿里开发手册 (P3C) 的火线插件安装使用教程
一.前言 火线(Fireline)的Jenkins官方插件已经上线,目前火线不仅能检查出安卓代码中的安全类问题和内存泄露问题,还兼容了阿里开源的Java开发规约(P3C项目),本文将以教程的形式帮助大 ...
- RotateAnimation 详解
RotateAnimation 详解 看看新闻网>看引擎>开源产品 其他构造器的旋转也可参考这副图. RotateAnimation旋转坐标系为以旋转点为坐标系(0,0)点.x轴为0度,顺 ...