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 拆点+缩点的更多相关文章

  1. CF1137C Museums Tour(Tarjan,强连通分量)

    好题,神题. 题目链接:CF原网 洛谷 题目大意: 一个国家有 $n$ 个城市,$m$ 条有向道路组成.在这个国家一个星期有 $d$ 天,每个城市有一个博物馆. 有个旅行团在城市 $1$ 出发,当天是 ...

  2. CF1137C Museums Tour(tarjan+DP)

    由于d很小,所以可以把每个点拆成d个点,然后对于边(x,y),连边时连接((x,i),(y,i+1))及((x,d),(y,1)).然后可以对这样连的边跑一遍tarjan缩点.然后直接暴力DP即可.不 ...

  3. CF1137C Museums Tour

    思路 强连通分量的好题 对于每个博物馆,因为时间的限制条件,不好直接统计, 发现d很小,可以建出d层分层图,原图<u,v>的边变成<u,i>到<v,i+1>的边,& ...

  4. CF1137 C. Museums Tour

    CF1137 C. Museums Tour 一般来说的正常思路:看到有向图的第一思路都是缩点(但是要分析一波证明强联通分量中的个体可以拼凑成整体,一般都是边和点可以经过无数次然后贡献只算一次这种类型 ...

  5. CF 1138 E. Museums Tour

    E. Museums Tour 链接 分析: 按时间建出分层图,每个点形如(u,t),表示u在在t个时刻的点,tarjan缩点.每个强连通分量中的点都能经过,然后DAG上dp. 代码: #includ ...

  6. hdu3488 Tour 拆点+二分图最佳匹配

    In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way r ...

  7. 【Codeforces 1137C】Museums Tour

    Codeforces 1137 C 题意:给一个有向图,一周有\(d\)天,每一个点在每一周的某些时刻会开放,现在可以在这个图上从\(1\)号点开始随意地走,问最多能走到多少个开放的点.一个点如果重复 ...

  8. [CF1137]Museums Tour

    link \(\text{Description:}\) 一个国家有 \(n\) 个城市,\(m\) 条有向道路组成.在这个国家一个星期有 \(d\) 天,每个城市有一个博物馆. 有个旅行团在城市 \ ...

  9. Codeforces 1137C Museums Tour (强连通分量, DP)

    题意和思路看这篇博客就行了:https://www.cnblogs.com/cjyyb/p/10507937.html 有个问题需要注意:对于每个scc,只需要考虑进入这个scc的时间即可,其实和从哪 ...

随机推荐

  1. 关于HTML的引入CSS文件问题

    一 html代码引用外部css文件时若css文件在本文件的父目录下的其他目录下,可使用绝对路径.此时路径要写为  “ ../ ”形式,如在tomcat下建立一个test文件,在该文件中建立两个文件 夹 ...

  2. solr 新建core

    D:\tomcat\webapps\solr\solr_home 在该路径下创建一个新的core,所需文件和层级如下 test_core |-- conf |-- schema.xml |-- sol ...

  3. Java 获取操作系统相关的内容

    package com.hikvision.discsetup.util; import java.lang.reflect.Field; import java.net.InetAddress; i ...

  4. java8中使用函数式接口

    使用函数式接口 Predicate @FunctionalInterface interface Predicate<T>{ boolean test(T t); } public sta ...

  5. 浅谈 ASCII、Unicode、UTF-8,一目了然

    对于ASCII.Unicode.UTF-8这三种编码方式我们经常用到,也经常挂到嘴边,但他们是怎么来的,为什么要存在,具体是怎么个规则,我们并没有做深入了解,下面,就带你看一下他们到底是怎么回事吧…… ...

  6. WPF 打开网页

    1.利用浏览器打开using System.Diagnostics; Process proc = new System.Diagnostics.Process(); proc.StartInfo.F ...

  7. PHP 的一些开发规范

    均需要遵守 PSR规范 变量命名 不用拼音 驼峰或下划线风格要一致 单词要有意义 不用关键字 常量全大写用下划线连接 代码注释 尽量让代码可读性提高,减少代码上的注释 函数头部可以描述参数和返回值及功 ...

  8. RE最全面的正则表达式----字符验证

    二.校验字符的表达式汉字:^[一-彪]{0,}$英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$长度为3-20的所有字符:^.{3,20}$由26个英文字母组成的字 ...

  9. 洛谷 P2158 [SDOI2008]仪仗队

    题意简述 给定一个n,求gcd(x, y) = 1(x, y <= n)的(x, y)个数 题解思路 欧拉函数, 则gcd(x, y) = 1(x <= y <= n)的个数 ans ...

  10. ASP.NET Core MVC 之依赖注入 Controller

    ASP.NET Core MVC 控制器应通过构造函数明确地请求它们地依赖关系,在某些情况下,单个控制器地操作可能需要一个服务,在控制器级别上的请求可能没有意义.在这种情况下,也可以将服务作为  Ac ...