【CF1137C】 Museums Tour 拆点+缩点
https://codeforc.es/contest/1137/problem/C
# 题意
给你n个点,每个点有k天博物馆开放时间的安排表。
有m条单向道路,走过一条边需要一个晚上,经过后就是第二天的意思。
问在无穷大的时间里,可以参观多少不同的博物馆。
# 思路
我们把每个点都拆出k个点,有单向边相连就从(u,i) -> (v, (i+1)%k)。
缩点跑出DAG,然后DP出最多的博物馆参观数。
这里就要考虑直接DP是否能行。假设有一条(u,i) -> (u, j)的边,由于没有自环且是单向边,所以一定可以通过循环,可以从(u,j)再走到(u,i),所以这两个点会缩在一起。
由于这种做法缩点时递归很深,我是通过inline优化的,开O(3)好像也可以。
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
/**********showtime************/
const int maxn = ;
// vector<int>mp[maxn];
int n,m,k;
int getid(int x, int i) {
return (x - ) * k + i + ;
}
struct E{
int u,v;
int nxt;
}edge[maxn];
int gtot = , head[maxn];
void addedge(int u, int v) {
edge[gtot].u = u;
edge[gtot].v = v;
edge[gtot].nxt = head[u];
head[u] = gtot++;
}
set <int> dpmp[maxn];
char str[];
int dp[maxn],a[maxn];
int belong[maxn], dfn[maxn] , low[maxn];
bool vis[maxn];
bool flag[maxn];
// int used[maxn];
int tim, scc_cnt;
//stack<int>st;
queue<int>que;
int st[];
int top = ;
inline void dfs(int u) {
dfn[u] = low[u] = ++tim;
// st.push(u);
st[++top] = u;
for(int i=head[u]; ~i; i=edge[i].nxt){
int v = edge[i].v;
if(!dfn[v]) dfs(v);
if(!belong[v]) low[u] = min(low[u], low[v]);
}
if(dfn[u] == low[u]) {
scc_cnt++;
int now;
while(true) {
now = st[top]; top--;
belong[now] = scc_cnt;
if(flag[now]) {
int id = (now - ) / k + ;
if(vis[id] == ) {
que.push(id);
vis[id] = ;
a[scc_cnt]++;
}
}
if(now == u) break;
}
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = ;
}
}
}
int ans = ; void cal(int s) {
queue<int>que;
dp[s] = a[s];
que.push(s);
ans = max(ans, a[s]);
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = ;
for(int v : dpmp[u]) {
dp[v] = max(dp[v], dp[u] + a[v]);
ans = max(ans, dp[v]);
if(vis[v] == ) {
vis[v] = ;
que.push(v);
}
}
}
}
int main(){
memset(head, -, sizeof(head));
R(n, m, k);
for(int i=; i<=m; i++) {
int u,v;
scanf("%d%d", &u, &v);
for(int j=; j<k; j++) {
addedge(getid(u, j), getid(v, (j+)%k));
}
}
for(int i=; i<=n; i++) {
scanf("%s", str);
for(int j=; j<k; j++) {
flag[getid(i, j)] = (str[j] == '');
}
}
for(int i=; i<=n; i++){
for(int j=; j<k; j++)
if(!dfn[getid(i, j)]) dfs(getid(i, j));
}
for(int i=; i<gtot; i++){
int u = edge[i].u, v = edge[i].v;
if(belong[u] == belong[v]) continue;
dpmp[belong[u]].insert(belong[v]);
}
cal(belong[getid(, )]);
printf("%d\n", ans);
return ;
}
【CF1137C】 Museums Tour 拆点+缩点的更多相关文章
- CF1137C Museums Tour(Tarjan,强连通分量)
好题,神题. 题目链接:CF原网 洛谷 题目大意: 一个国家有 $n$ 个城市,$m$ 条有向道路组成.在这个国家一个星期有 $d$ 天,每个城市有一个博物馆. 有个旅行团在城市 $1$ 出发,当天是 ...
- CF1137C Museums Tour(tarjan+DP)
由于d很小,所以可以把每个点拆成d个点,然后对于边(x,y),连边时连接((x,i),(y,i+1))及((x,d),(y,1)).然后可以对这样连的边跑一遍tarjan缩点.然后直接暴力DP即可.不 ...
- CF1137C Museums Tour
思路 强连通分量的好题 对于每个博物馆,因为时间的限制条件,不好直接统计, 发现d很小,可以建出d层分层图,原图<u,v>的边变成<u,i>到<v,i+1>的边,& ...
- CF1137 C. Museums Tour
CF1137 C. Museums Tour 一般来说的正常思路:看到有向图的第一思路都是缩点(但是要分析一波证明强联通分量中的个体可以拼凑成整体,一般都是边和点可以经过无数次然后贡献只算一次这种类型 ...
- CF 1138 E. Museums Tour
E. Museums Tour 链接 分析: 按时间建出分层图,每个点形如(u,t),表示u在在t个时刻的点,tarjan缩点.每个强连通分量中的点都能经过,然后DAG上dp. 代码: #includ ...
- hdu3488 Tour 拆点+二分图最佳匹配
In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way r ...
- 【Codeforces 1137C】Museums Tour
Codeforces 1137 C 题意:给一个有向图,一周有\(d\)天,每一个点在每一周的某些时刻会开放,现在可以在这个图上从\(1\)号点开始随意地走,问最多能走到多少个开放的点.一个点如果重复 ...
- [CF1137]Museums Tour
link \(\text{Description:}\) 一个国家有 \(n\) 个城市,\(m\) 条有向道路组成.在这个国家一个星期有 \(d\) 天,每个城市有一个博物馆. 有个旅行团在城市 \ ...
- Codeforces 1137C Museums Tour (强连通分量, DP)
题意和思路看这篇博客就行了:https://www.cnblogs.com/cjyyb/p/10507937.html 有个问题需要注意:对于每个scc,只需要考虑进入这个scc的时间即可,其实和从哪 ...
随机推荐
- XSS危害——session劫持(转载)
在跨站脚本攻击XSS中简单介绍了XSS的原理及一个利用XSS盗取存在cookie中用户名和密码的小例子,有些同学看了后会说这有什么大不了的,哪里有人会明文往cookie里存用户名和密码.今天我们就介绍 ...
- 【Intellij】导出 jar 包
需要在 Intellij 导出 jar 包,一时不知道该怎么做,后来总算找到了方法,步骤如下: 1. File → Project Structure... → Artifacts → + → jar ...
- 跟着阿里p7一起学java高并发 - 第19天:JUC中的Executor框架详解1,全面掌握java并发核心技术
这是java高并发系列第19篇文章. 本文主要内容 介绍Executor框架相关内容 介绍Executor 介绍ExecutorService 介绍线程池ThreadPoolExecutor及案例 介 ...
- 开启java之门
一.Java语言概述 Java语言诞生于1995年,由Sun公司推出. 2009年,Sun公司被甲骨文公司收购,所以我们现在访问oracle官网即可:https://www.oracle.com Ja ...
- 浅谈单例模式及其java实现
单例模式是23种设计模式中比较简单的一种,在此聊一下单例模式. 1.什么是设计模式? 对于没有接触过设计模式的人来说,一听到设计模式这四个字就觉得这个东西很高深莫测,一下子就对这个东西产生了恐惧感,其 ...
- c#图片的平移与旋转
1新建文件夹,添加一个图片 2 添加控件 两个button控件 一个image控件 一个Canvas控件 3 代码实现 using System;using System.Collections.Ge ...
- 49.Qt-网络编程之QTCPSocket和QTCPServer(实现简易网络调试助手)
在上章 48.QT-网络通信讲解1,我们学习了网络通信基础后,本章便来实战一篇.源码正在上传中,等下贴地址. PS:支持客户端和服务器,提供源码,并且服务器支持多客户端连入,并且可以指定与个别客户端发 ...
- html的一些基本属性介绍
一.html的属性类型: 1.常见标签属性: a.<h1>:align对其方式 例如:<h1 align="right"> hhhhh</ ...
- java学习-NIO(二)Buffer
当我们需要与 NIO Channel 进行交互时, 我们就需要使用到 NIO Buffer, 即数据从 Buffer读取到 Channel 中, 并且从 Channel 中写入到 Buffer 中.缓 ...
- scrapy 自学入门demo分享
[toc] 本文基于python 3.7.0,win10平台: 2018-08 完整项目代码:https://github.com/NameHewei/python-scrapy 安装 安装pytho ...