ZOJ 3795 Grouping(scc+最长路)
Grouping
Time Limit: 2 Seconds Memory Limit: 65536 KB
Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-th message shows that the age of person si is not smaller than the age of person ti. Now we need to divide all these N people into several groups. One's age shouldn't be compared with each other in the same group, directly or indirectly. And everyone should be assigned to one and only one group. The task is to calculate the minimum number of groups that meet the requirement.
Input
There are multiple test cases. For each test case: The first line contains two integers N(1≤ N≤ 100000), M(1≤ M≤ 300000), N is the number of people, and M is is the number of messages. Then followed by M lines, each line contain two integers si and ti. There is a blank line between every two cases. Process to the end of input.
Output
For each the case, print the minimum number of groups that meet the requirement one line.
Sample Input
4 4
1 2
1 3
2 4
3 4
Sample Output
3
Hint
set1= {1}, set2= {2, 3}, set3= {4}
题目给出N个点 , M条边, 然后问一些相关联的人(存在一条路u -> v)不能分在同一组 , 最少分的组的数目 。
这样肯定要求一个 scc .. 那么在同一个scc里面的人(假设有w个人)都要拆分成w组 , 那么我们以这个w作为
scc图的点权 。 目的是所有 scc图(DAG) 的最长路所构成的权 ,取最大。
最长路用一个记忆化搜索即可 , 训练的时候不会这么用 , 卡蒙了~~ 。
#include <bits/stdc++.h>
using namespace std;
const int N = ; int pre[N] , lowlink[N] , sccno[N] , dfs1_clock , scc_cnt ;
stack<int>st;
int n , m ;
int w[N] , dep_w[N];
bool vis[N];
int in[N];
int dp[N]; int eh[N] , et[N<<] ,nxt[N<<] , tot ; void addedge( int u , int v )
{
et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot ++ ;
} struct node
{
int u , v ;
}e[N*]; void dfs1( int u )
{
pre[u] = lowlink[u] = ++dfs1_clock;
st.push(u);
for( int i = eh[u]; ~i ; i = nxt[i] ){
int v = et[i] ;
if(!pre[v]){
dfs1(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if( !sccno[v] ){
lowlink[u] = min( lowlink[u] , pre[v] );
}
}
if( lowlink[u] == pre[u] )
{
scc_cnt++;
for(;;){
int x = st.top(); st.pop();
sccno[x] = scc_cnt ;
if( x == u ) break;
}
}
} void find_scc()
{
dfs1_clock = scc_cnt = ;
memset ( pre , , sizeof pre ) ;
memset ( sccno , , sizeof sccno ) ;
while( !st.empty() ) st.pop(); for( int i = ; i <= n ; ++i ){
if( !pre[i] )dfs1(i);
}
} void init()
{ tot= ;
memset( eh , - ,sizeof eh );
memset( dp , - ,sizeof dp );
memset( w , ,sizeof w );
memset( vis ,false , sizeof vis );
memset( in , , sizeof in );
} void dfs( int u )
{
if( dp[u] == - ){
int sum_now = w[u] ;
for( int i = eh[u] ; ~i ; i = nxt[i] ){
int v = et[i];
dfs(v);
sum_now = max( sum_now , w[u] + dp[v] );
}
dp[u] = sum_now;
}
} void run()
{
int u , v ;
init();
for( int i = ; i < m ;++i ){
scanf("%d%d",&e[i].u,&e[i].v);
addedge( e[i].u , e[i].v );
}
find_scc();
tot = ;
memset(eh , - , sizeof eh ); for( int i = ;i < m ;++i ){
if( sccno[e[i].u] != sccno[e[i].v] ){
addedge( sccno[e[i].u] , sccno[e[i].v] );
in[ sccno[e[i].v] ] ++;
}
} for( int i = ; i <= n ; ++i )w[ sccno[i] ] ++ ; int ans = ;
for( int i = ; i <= scc_cnt ; ++i ) if( !in[i] ) addedge(,i);
dfs();
for( int i = ; i <= scc_cnt ; ++i ) ans = max( ans , dp[i] );
printf("%d\n",ans);
} int main()
{
#ifdef LOCAL
freopen("in","r",stdin);
#endif
ios::sync_with_stdio();
while( ~scanf("%d%d",&n,&m))run();
}
ZOJ 3795 Grouping(scc+最长路)的更多相关文章
- ZOJ 3795 Grouping 求最长链序列露点拓扑
意甲冠军:特定n积分.m向边条. 该点被划分成多个集合随机的每个集合,使得2问题的关键是无法访问(集合只能容纳一个点) 问至少需要被分成几个集合. 假设没有戒指,接着这个话题正在寻求产业链最长的一个有 ...
- zoj 3795 Grouping tarjan缩点 + DGA上的最长路
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practic ...
- ZOJ 3795 Grouping (强连通缩点+DP最长路)
<题目链接> 题目大意: n个人,m条关系,每条关系a >= b,说明a,b之间是可比较的,如果还有b >= c,则说明b,c之间,a,c之间都是可以比较的.问至少需要多少个集 ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...
- ZOJ 3795 Grouping 强连通分量-tarjan
一开始我还天真的一遍DFS求出最长链以为就可以了 不过发现存在有向环,即强连通分量SCC,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...
- ZOJ 3795 Grouping
大致题意是给n个人和m组关系,每组关系都是两个人s和t,表示s年龄不小于t的年龄,然后让你把这n个人分组,使得任何一个组里面的任意两人都不能直接或间接的得出这两个人的年龄大小关系. 思路:根据给出的关 ...
- ZOJ 3795 Grouping(Tarjan收缩点+DAG)
Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-t ...
- Grouping ZOJ - 3795 (tarjan缩点求最长路)
题目链接:https://cn.vjudge.net/problem/ZOJ-3795 题目大意:给你n个人,m个关系, 让你对这个n个人进行分组,要求:尽可能的分组最少,然后每个组里面的人都没有关系 ...
- ZOJ 3795:Grouping(缩点+最长路)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5303 题意:有n个人m条边,每条边有一个u,v,代表u的年龄大于等于v,现在要 ...
随机推荐
- iOS 自己写的对话框中加入三个输入框
-(void)dialog:(NSString*)title okTitle:(NSString*)okTitle placeholder:(NSString*)placeholder finish: ...
- Flask-Scrip
介绍及安装 Flask-Script是一个让你的命令行支持自定义命令的工具,它为Flask程序添加一个命令行解释器.可以让我们的程序从命令行直接执行相应的程序. 安装 pip install Flas ...
- 使用雪花算法为分布式下全局ID、订单号等简单解决方案考虑到时钟回拨
1.snowflake简介 互联网快速发展的今天,分布式应用系统已经见怪不怪,在分布式系统中,我们需要各种各样的ID,既然是ID那么必然是要保证全局唯一,除此之外,不同当业务还需要不同 ...
- spring3.0+jsf+ibatis整合
user.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLI ...
- myBatis配置提示xml和内部DTD
–配置环境:macOS high Sierra 10.13.6/window10–生产环境:eclipse2018.a,myeclipse2018 首先了解xml文件的参数——<!DOCT ...
- oracle创建表空间自增空间管理
表空间(tablespace).段(segment).区(extent).块(block),这些都是oracle数据库在数据文件中组织数据的基本单元 1.创建表空间create tablespace ...
- hibernate 参数一览
实现包含了Hibernate与数据库的基本连接信息的配置方式有两种方式: 第一种是使用hibernate.properties文件作为配置文件. 第二种是使用hibernate.cfg.xml文件作为 ...
- 转:动态库路径配置- /etc/ld.so.conf文件
Linux 共享库 Linux 系统上有两类根本不同的 Linux 可执行程序.第一类是静态链接的可执行程序.静态可执行程序包含执行所需的所有函数 — 换句话说,它们是“完整的”.因为这一原因,静态可 ...
- How can I check the last time stats was run on Oracle without using OEM
All of the following data dictionary tables have a LAST_ANALYZED column (replace * with USER/ALL/DBA ...
- 【软工项目Beta阶段】第10周Scrum会议博客
第十周会议记录 小组GitHub项目地址https://github.com/ouc-softwareclass/OUC-Market 小组Issue地址https://github.com/ouc- ...