HDU 3639 Hawk-and-Chicken(强连通)
来源:互联网 发布:淘宝的商品类目 编辑:程序博客网 时间:2024/06/06 09:09
HDU 3639 Hawk-and-Chicken
题目链接
题意:就是在一个有向图上,满足传递关系,比如a->b, b->c,那么c可以得到2的支持,问得到支持最大的是谁,并且输出这些人
思路:先强连通的缩点,然后逆向建图,对于每个出度为0的点,进行dfs求哪些点可达这个点
代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <stack>#include <map>#include <queue>using namespace std;const int N = 5005;int t, n, m;int sccn, dfs_clock, sccno[N], pre[N], dfn[N];stack<int> S;vector<int> g[N], save[N], scc[N];void dfs_scc(int u) {pre[u] = dfn[u] = ++dfs_clock;S.push(u);for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (!pre[v]) {dfs_scc(v);dfn[u] = min(dfn[u], dfn[v]);} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);}if (pre[u] == dfn[u]) {++sccn;save[sccn].clear();while (1) {int x = S.top(); S.pop();sccno[x] = sccn;save[sccn].push_back(x);if (x == u) break;}}}void find_scc() {dfs_clock = sccn = 0;memset(pre, 0, sizeof(pre));memset(sccno, 0, sizeof(sccno));for (int i = 0; i < n; i++)if (!pre[i]) dfs_scc(i);}int out[N];int ans[N], an, dp[N], vis[N];int dfs(int u) {vis[u] = 1;int ans = save[u].size();for (int i = 0; i < scc[u].size(); i++) {int v = scc[u][i];if (vis[v]) continue;ans += dfs(v);}return ans;}int main() {int cas = 0;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) g[i].clear();int u, v;while (m--) {scanf("%d%d", &u, &v);g[u].push_back(v);}find_scc();memset(out, 0, sizeof(out));for (int i = 1; i <= sccn; i++) scc[i].clear();for (int u = 0; u < n; u++) {for (int j = 0; j < g[u].size(); j++) {int v = g[u][j];if (sccno[u] != sccno[v]) {scc[sccno[v]].push_back(sccno[u]);out[sccno[u]]++;}}}int Max = 0;for (int i = 1; i <= sccn; i++)if (!out[i]) { memset(vis, 0, sizeof(vis));dp[i] = dfs(i);Max = max(Max, dp[i]);}an = 0;for (int i = 1; i <= sccn; i++) {if (!out[i] && dp[i] == Max) {for (int j = 0; j < save[i].size(); j++) {ans[an++] = save[i][j];}}}sort(ans, ans + an);printf("Case %d: %d\n", ++cas, Max - 1);for (int i = 0; i < an; i++)printf("%d%c", ans[i], i == an - 1 ? '\n' : ' ');}return 0;}
1 0
- HDU 3639 Hawk-and-Chicken(强连通)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- hdu 3639 Hawk-and-Chicken 强连通分量 targin
- HDU - 3639 Hawk-and-Chicken(dfs+强连通分量)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU 3639Hawk-and-Chicken 强连通分量分解 + dfs
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU 3639 Hawk-and-Chicken (强连通分量+树形DP)
- 【强连通分量】 HDOJ 3639 Hawk-and-Chicken
- hdu3639 Hawk-and-Chicken【强连通】
- hdu 3639 Hawk-and-Chicken 【强连通分量+反向建图dfs】
- 【HDU】3639 Hawk-and-Chicken 强连通缩点+DFS
- HDU 3639 Hawk-and-Chicken 强联通缩点 + DFS
- hdu 3639 Hawk-and-Chicken【强联通Tarjan+Dfs】
- HDU 3639 Hawk-and-Chicken 强连通缩点+搜索
- HDU 3639 Hawk-and-Chicken
- Hawk-and-Chicken HDU
- HDU3639--Hawk-and-Chicken(强连通+缩点+反向图)
- 第八周项目四个人所得税计算器
- 第8周项目本月有几天
- VGA、HDMI、DVI接口
- js实现本地图片预览
- 棱形的输出
- HDU 3639 Hawk-and-Chicken(强连通)
- 关于在接收推送消息后PendingIntent传参问题
- Daoism--2
- vs2010 视图 aspx页面创建控件时出错 未将对象引用设置到对象的实例
- AUC的解释
- 加载外部图片
- android 中Timer解析
- edit plus++User Files
- 输出到屏幕和保存文件同时进行