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 ...
随机推荐
- POJ2195:Going Home——题解
http://poj.org/problem?id=2195 题目大意: 有些人和房子,一个人只能进一个房子,人走到房子的路程即为代价. 求所有人走到房子后的最小代价. ——————————————— ...
- BZOJ5249:[九省联考2018]IIIDX——题解
https://www.luogu.org/problemnew/show/P4364#sub https://www.lydsy.com/JudgeOnline/problem.php?id=524 ...
- [Ahoi2005]COMMON 约数研究 【欧拉线性筛的应用】
1968: [Ahoi2005]COMMON 约数研究 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 2939 Solved: 2169 [Submi ...
- bzoj4300: 绝世好题(DP)
按位DP f[i]表示第i位为1的最长子序列 #include<iostream> #include<cstring> #include<cstdlib> #inc ...
- hdu 5625
Clarke and chemistry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?
参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...
- POJ 3281 最大流
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17251 Accepted: 7643 Descripti ...
- idea 创建多模块时模块类无法引入
我的原因是类的位置方的不对,由于刚搭建的项目,本来只想做个测试,就直接在java下创建类,然而这居然是个深坑,模块引入了也无法引入这个模块的类. 解决方法:创建com.***.***包后的类可以正常引 ...
- [LeetCode] 18. 4Sum ☆☆
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 应用Xml.Linq读xml文件
c#提供了System.Xml.Linq操作xml文件,非常方便,本文主要介绍如何应用System.Xml.Linq读取xml文件. xml文本 <?xml version="1.0& ...