ZOJ 3795 Grouping(强连通+最长路)
来源:互联网 发布:淘宝卖家发货怎么打包 编辑:程序博客网 时间:2024/06/09 23:28
题意:有n个人,m个关系,每个关系表示si的体重>=ti的体重,问你最少划分多少个组,使得这些组里面的人不能直接或者间接和组里的人比体重
思路:首先要考虑到一个问题,si>=ti,那么就可能存在几个人的体重都是一样的,那么他们几个人就是等价的,所以需要先缩点,然后就显然的转化成求DAG上的最长路了
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<stack>#include<vector>using namespace std;const int maxn = 100000+7;vector<int>e[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;int cnt[maxn];stack<int>s;void dfs(int u){pre[u]=lowlink[u]=++dfs_clock;s.push(u);for(int i = 0;i<e[u].size();i++){int v = e[u][i];if(!pre[v]){dfs(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 = s.top();s.pop();sccno[x]=scc_cnt;cnt[scc_cnt]++;if(x==u)break;}}}void find_scc(int n){dfs_clock=scc_cnt=0;memset(sccno,0,sizeof(sccno));memset(cnt,0,sizeof(cnt));memset(pre,0,sizeof(pre));while(!s.empty())s.pop();for(int i = 1;i<=n;i++)if(!pre[i])dfs(i);}int in[maxn];vector<int>G[maxn];int ans = 0;int dp[maxn];void bfs(){queue<int>q;memset(dp,0,sizeof(dp));for(int i = 1;i<=scc_cnt;i++)if(!in[i]){q.push(i);dp[i]=cnt[i];}while(!q.empty()){int u = q.front();q.pop();for(int i =0;i<G[u].size();i++){ int v = G[u][i]; dp[v]=max(dp[u]+cnt[v],dp[v]);if(--in[v]==0)q.push(v);}}for(int i = 1;i<=scc_cnt;i++)ans=max(ans,dp[i]);}int main(){int n,m; while(scanf("%d%d",&n,&m)!=EOF){ans = 0;memset(in,0,sizeof(in));for(int i = 0;i<=n;i++)e[i].clear();for(int i = 0;i<=n;i++)G[i].clear();for(int i = 1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);e[u].push_back(v);}find_scc(n); for(int u=1;u<=n;u++){for(int i = 0;i<e[u].size();i++){int v = e[u][i];if(sccno[u]!=sccno[v]){in[sccno[v]]++;G[sccno[u]].push_back(sccno[v]);}}}bfs(); printf("%d\n",ans);}}
Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. Thei-th message shows that the age of person si is not smaller than the age of personti. Now we need to divide all theseN 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.
There are multiple test cases. For each test case: The first line contains two integersN(1≤ N≤ 100000), M(1≤ M≤ 300000), N is the number of people, and M is is the number of messages. Then followed byM lines, each line contain two integers si and ti. There is a blank line between every two cases. Process to the end of input.
For each the case, print the minimum number of groups that meet the requirement one line.
4 41 21 32 43 4
3
set1= {1}, set2= {2, 3}, set3= {4}
- ZOJ 3795 Grouping(强连通+最长路)
- zoj 3795 Grouping(强连通+dp)
- ZOJ 3795 - Grouping (强连通+dp)
- zoj3795 Grouping --- 强连通,求最长路
- ZOJ 3795 Grouping 强连通缩点 + DAG最长路
- ZOJ 3795 Grouping 强联通 最长路径
- zoj-3795-Grouping-tarjan缩点求最长路
- 训练赛 Grouping(强连通分量缩点 + DAG求最长路)
- zoj 3795 Grouping 强连通缩点+拓扑排序最长链
- zoj 3795 Grouping(强连通缩点)
- ZOJ 3795 Grouping 强联通+偏序集
- ZOJ 3795 Grouping (tarjan缩点求最长链)
- zoj 5303 Grouping 缩点求最长路
- zoj3795 Grouping DP+强连通
- poj 3592 Instantaneous Transference(强连通+最长路)
- POJ3160强连通+spfa最长路(不错)
- Instantaneous Transference poj3592(【强连通分量】【最长路】)
- ZOJ 3795 Grouping(强联通缩点,记忆化搜索)
- 设计模式--单例模式学习笔记
- 第四天(HttpServlet类)
- DNS正反向解析和主从同步配置
- maven常用命令
- 多项式输出
- ZOJ 3795 Grouping(强连通+最长路)
- 欢迎使用CSDN-markdown编辑器
- UVA 11149 & POJ 3233 矩阵的幂
- 使用Altium Designer 生成 Geber文件
- 信息系统项目管理教程目录
- 练习
- ubuntu RPM should not be used directly install RPM packages, use Alien instead!
- 高中JZOJ-1221-King
- Leetcode-450. Delete Node in a BST