题目传送门

https://codeforces.com/contest/1103/problem/C

题解

这个题还算一个有难度的不错的题目吧。

题目给出了两种回答方式:

  1. 找出一条长度 \(\geq \frac nk\) 的路径;
  2. 找出 \(k\) 个简单环,满足长度不是 \(3\) 的倍数,并且每个环至少存在一个点不在别的环中。

很显然题目并不是要你随便挑一种回答方式开始单独研究。最有可能的情况是两种回答方式可以替补。


如果我们随便作出原图的一棵生成树,如果最长的路径长度 \(\geq \frac nk\),那么我们可以直接输出。否则,很容易发现,这棵树上至少有 \(k\) 个叶子。

很容易把 \(k\) 个叶子往第二种回答方式中的 \(k\) 个环上联想。但是第二个回答方式中的几个条件我们还都要满足。

对于“每个环至少存在一个点不在别的环中”这个条件,等价于就是说,每一个叶子不会和其余的叶子有连边。dfs 树可以保证这个性质,同时 dfs 树还可以保证,每一个点出来的非树边一定是返祖边。那么,建立 dfs 树以后,每个环就是每一个叶子和它的一些祖先之间的事情了。

接着考虑环的长度不是 \(3\) 的倍数的这个条件。

首先,我们不妨假设叶子 \(x\) 连向的两条返祖边指向的点分别为 \(x\) 的祖先为 \(f_1\) 和 \(f_2\)。

那么我们得到了三个环 \(x \to f_1 \to x\),\(x\to f_2 \to x\),\(f_1\to f_2 \to x\)。

然后三条路径的长度为别为 \(dep_x - dep_{f_1} + 1\),\(dep_x - dep_{f_2} + 1\),\(dep_{f_1} - dep_{f_2} + 2\)。

如果 \(dep_x - dep_{f_1} + 1\) 和 \(dep_x - dep_{f_2} + 1\) 都是 \(3\) 的倍数(否则输出其中的一个就可以了),那么就是说 \((dep_x - dep_{f_1}) - (dep_x - dep_{f_2}) + 2 = dep_{f_1} - dep_{f_2} + 2\) 模上 \(3\) 应该是 \(2\)。

也就是说,上面的三个环中至少存在一个环的长度不是 \(3\) 的倍数。输出哪一个即可。


时间复杂度 \(O(n+m)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 2.5e5 + 7;
const int M = 5e5 + 7; int n, m, k, nk;
int fz[N][3], f[N], chk[N], vis[N], dep[N]; struct Edge { int to, ne; } g[M << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); } inline void dfs(int x, int fa = 0) {
f[x] = fa, vis[x] = 1, dep[x] = dep[fa] + 1;
int &c = chk[x] = 1;
for fec(i, x, y)
if (!vis[y]) c = 0, dfs(y, x);
else if (y != fa && fz[x][0] < 2) fz[x][++fz[x][0]] = y;
} inline void work() {
dfs(1);
for (int i = 1; i <= n; ++i) if (dep[i] >= nk) {
puts("PATH");
printf("%d\n", dep[i]);
for (int x = i; x; x = f[x]) printf("%d%c", x, " \n"[!f[x]]);
return;
}
puts("CYCLES");
int cnt = 0;
for (int i = 1; i <= n; ++i) if (chk[i]) {
++cnt;
if (dep[fz[i][1]] < dep[fz[i][2]]) std::swap(fz[i][1], fz[i][2]);
if ((dep[i] - dep[fz[i][1]]) % 3 != 2) {
int ans = 0;
for (int x = i; x != f[fz[i][1]]; x = f[x]) ++ans;
printf("%d\n", ans);
for (int x = i; x != f[fz[i][1]]; x = f[x]) printf("%d%c", x, " \n"[x == fz[i][1]]);
} else {
if ((dep[i] - dep[fz[i][2]]) % 3 != 2) {
int ans = 0;
for (int x = i; x != f[fz[i][2]]; x = f[x]) ++ans;
printf("%d\n", ans);
for (int x = i; x != f[fz[i][2]]; x = f[x]) printf("%d%c", x, " \n"[x == fz[i][2]]);
} else {
int ans = 1;
for (int x = fz[i][1]; x != f[fz[i][2]]; x = f[x]) ++ans;
printf("%d\n", ans);
for (int x = fz[i][1]; x != f[fz[i][2]]; x = f[x]) printf("%d ", x);
printf("%d\n", i);
}
}
if (cnt >= k) return;
}
assert(cnt >= k);
} inline void init() {
read(n), read(m), read(k), nk = (n - 1) / k + 1;
int x, y;
for (int i = 1; i <= m; ++i) read(x), read(y), adde(x, y);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

CF1103C Johnny Solving (Codeforces Round #534 (Div. 1)) 思维+构造的更多相关文章

  1. Codeforces Round #534 (Div. 2)

    B. Game with string 题意: 给出一个字符串s只包括小写字母.当轮到一个玩家的时候,他可以选择两个连续且相等的字母并且删除它.当一个玩家没得删的时候他就输了. 题解: 乍一看有点懵, ...

  2. Codeforces Round #534 (Div. 2) Solution

    A. Splitting into digits Solved. #include <bits/stdc++.h> using namespace std; int n; void sol ...

  3. 20191028 Codeforces Round #534 (Div. 1) - Virtual Participation

    菜是原罪. 英语不好更是原罪. \(\mathrm{A - Grid game}\) 题解 \(4 \times 4\) 的格子,两种放法. 发现这两种在一起时候很讨厌,于是强行拆分这个格子 上面 \ ...

  4. Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)

    D. Game with modulo 题目链接:https://codeforces.com/contest/1104/problem/D 题意: 这题是一个交互题,首先一开始会有一个数a,你最终的 ...

  5. CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP

    题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...

  6. Codeforces Round #534 (Div. 1)

    A 构造题 有一个44的方格 每次放入一个横向12或竖向2*1的方格 满了一行或一列就会消掉 求方案 不放最后一行 这样竖行就不会消 然后竖着的放前两行 横着的放第三行 循环放就可以啦 #includ ...

  7. Codeforces Round #534 (Div. 2)D. Game with modulo-1104-D(交互+二分+构造)

    D. Game with modulo time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. [ACM]Codeforces Round #534 (Div. 2)

    A. Splitting into digits Vasya has his favourite number n. He wants to split it to some non-zero dig ...

  9. Codeforces 1104 D. Game with modulo-交互题-二分-woshizhizhang(Codeforces Round #534 (Div. 2))

    D. Game with modulo time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. Linux驱动开发10——内核环形双向链表

    Linux内核环形双向链表本身不实现锁机制,需要驱动本身完成锁机制实现. 1.1.list_head结构体 #include <linux/list.h> struct list_head ...

  2. Hybrid平台

    需求说明 离线包管理平台主要负责对需要接入Hybrid平台的应用进行管理,通过这个平台可以实现对应用的静态资源进行构建.发布.生成离线包,版本控制等,核心场景如下: 将需要做预加载的应用在平台上注册, ...

  3. Mac下安装lightgbm

    Mac下安装lightgbm 1.安装环境 系统 MacOS Mojave 版本10.14.2 Xcode 10.1 $ clang -v Apple LLVM version 10.0.0 (cla ...

  4. 阶段3 1.Mybatis_11.Mybatis的缓存_2 延迟加载和立即加载的概念

    用户关联的account信息,假设一个用户管理的account有100个.那么我们在查询用户的时候那100个关联的信息也被查询出来. 用的时候才去查关联的数据 这两个不同的地方就是查询的时机不同 什么 ...

  5. spotlight监控mysql性能

    spotlight可以监控mysql性能,同监控linux一样配置 目录 1.安装spotlight 2.参数认识 1.安装spotlight spotlight不仅仅只是监控mysql,还可以完成数 ...

  6. win10安装Tensorflow1.9GPU版本

    前言 看到DateWhale出了一篇安装教程(微信公众号DateWhale),决定体验一下Tensorflow1.9的GPU版本..其实一开始装的是2.0,但是tf.Session()就报错了,说是2 ...

  7. 最小二乘法公式推导及Python实现

    机器学习使用线性回归方法建模时,求损失函数最优解需要用到最小二乘法.相信很多朋友跟我一样,想先知道公式是什么,然后再研究它是怎么来的.所以不多说,先上公式. 对于线性回归方程\(f(x) = ax + ...

  8. SpringBoot 单元测试junit test

    pom引用 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...

  9. dotnet sdk 的镜像tag 相关

    https://hub.docker.com/_/microsoft-dotnet-core-sdk/ 微软的dotnet sdk 的 tag 微软貌似改默认镜像 dockerhub 里面的tag与 ...

  10. IDEA导入Junit jar包,在JavaSE的Module中使用Junit测试

    写代码时偶尔想试一下自己的小想法,于是在IDEA中建了一个JavaEE项目.JavaEE项目中只能在main方法中运行代码块,不如单元测试的@Test灵活. 于是在网上找到了Junit的jar包:Do ...